diff --git a/FileLoader.php b/FileLoader.php new file mode 100755 index 0000000..4a7332d --- /dev/null +++ b/FileLoader.php @@ -0,0 +1,40 @@ + + * + * @package Heidelpay + * @subpackage PhpStorm + * @category ${CATEGORY} + */ + +class FileLoader +{ + const BASE_PATH = __DIR__.DIRECTORY_SEPARATOR. 'lib'; + + /** + * Loads all php files in lib directory. + * + * @param string $dir + */ + public static function requireAllLibs($dir = self::BASE_PATH) + { + /** @var array $files */ + $files = glob("$dir/*"); + foreach ($files as $file) { + if (preg_match('/\.php$/', $file)) { + require_once $file; + } elseif (is_dir($file)) { + self::requireAllLibs($file); + } + } + } +} \ No newline at end of file diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 index efad51d..b8c64cc --- a/composer.json +++ b/composer.json @@ -8,6 +8,10 @@ { "name": "Stephano Vogel", "email": "development@heidelpay.de" + }, + { + "name": "Simon Gabriel", + "email": "development@heidelpay.de" } ], "support" : { diff --git a/lib/Helpers/FileSystem.php b/lib/Helpers/FileSystem.php old mode 100644 new mode 100755 index 94a9801..2b962ef --- a/lib/Helpers/FileSystem.php +++ b/lib/Helpers/FileSystem.php @@ -29,7 +29,7 @@ class FileSystem public function __construct($path) { // we just want to read files, so mode 'r' will be fine at all. - $this->handle = fopen($path, 'r'); + $this->handle = fopen($path, 'rb'); } /** @@ -56,7 +56,7 @@ public function getCsvContent() // an array with the error-code as key and the message as value. while ($content = fgetcsv($this->handle)) { // 0 = HPError-Code, 1 = Message - if (isset($content[0]) && isset($content[1])) { + if (isset($content[0], $content[1])) { $ret[$content[0]] = $content[1]; } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist old mode 100644 new mode 100755 index 5d54a39..6a864fc --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -19,6 +19,7 @@ ./lib/ + ./FileLoader.php \ No newline at end of file diff --git a/tests/unit/FileLoaderTest.php b/tests/unit/FileLoaderTest.php new file mode 100755 index 0000000..5dc53ff --- /dev/null +++ b/tests/unit/FileLoaderTest.php @@ -0,0 +1,89 @@ + + * + * @package Heidelpay + * @subpackage PhpStorm + * @category ${CATEGORY} + */ +namespace Tests\Unit; + +use FileLoader as MessageMapperFileLoader; + +/** @noinspection PhpIncludeInspection */ +require realpath(__DIR__ . '/../../FileLoader.php'); + +class FileLoaderTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test that file loader includes all php files. + * + * @test + */ + public function autoloaderShouldLoadAllPhpFilesInLibDirectory() + { + $path = realpath(__DIR__. '/../../lib/'); + $regex = '/^' . str_replace(DIRECTORY_SEPARATOR, '\\' . DIRECTORY_SEPARATOR, $path) . '.*\.php$/'; + + $filesIncludedBefore = preg_grep($regex, get_included_files()); + MessageMapperFileLoader::requireAllLibs(); + $filesIncludedAfter = preg_grep($regex, get_included_files()); + + // files that should be loaded + $phpFilesInLib = $this->getPhpFilesInLib($path); + + fwrite(STDOUT, 'Loaded Files before: ' . print_r($filesIncludedBefore, 1)); + fwrite(STDOUT, 'Loaded Files after: ' . print_r($filesIncludedAfter, 1)); + fwrite(STDOUT, 'Files that should have been loaded: ' . print_r($phpFilesInLib, 1)); + + $this->assertGreaterThan(0, count($filesIncludedAfter), 'Error: There are no files loaded at all!'); + + foreach ($phpFilesInLib as $item) { + $this->assertArrayNotHasKey($item, array_flip($filesIncludedBefore)); + $this->assertArrayHasKey($item, array_flip($filesIncludedAfter)); + } + } + + private function getPhpFilesInLib($path) + { + if (!is_dir($path)) { + return []; + } + + // iterate through all items in the given path + $items = scandir($path, SCANDIR_SORT_DESCENDING); + + $files = []; + $fileArrays = []; + foreach ($items as $item) { + // leave current and previous path out + if ($item === '.' || $item === '..') { + continue; + } + + // prepare complete path of the current item + $itemPath = $path . DIRECTORY_SEPARATOR . $item; + if (is_dir($itemPath)) { + // scan subdirectories as well + $fileArrays[] = $this->getPhpFilesInLib($itemPath); + continue; + } + + if (pathinfo($itemPath, PATHINFO_EXTENSION) === 'php') { + $files[] = $itemPath; + } + } + $fileArrays[] = $files; + + return array_merge(...$fileArrays); + } +} diff --git a/tests/unit/MessageCodeMapperTest.php b/tests/unit/MessageCodeMapperTest.php old mode 100644 new mode 100755 index d93e619..0feedd7 --- a/tests/unit/MessageCodeMapperTest.php +++ b/tests/unit/MessageCodeMapperTest.php @@ -2,8 +2,8 @@ namespace Tests\Unit; -use Heidelpay\MessageCodeMapper\MessageCodeMapper; use Heidelpay\MessageCodeMapper\Exceptions\MissingLocaleFileException; +use Heidelpay\MessageCodeMapper\MessageCodeMapper; use PHPUnit\Framework\TestCase; /** @@ -26,6 +26,8 @@ class MessageCodeMapperTest extends TestCase * Unit test for the correct locale after object initialization. * * @test + * + * @throws MissingLocaleFileException */ public function displayCorrectLocale() { @@ -39,6 +41,8 @@ public function displayCorrectLocale() * string or not. * * @test + * + * @throws MissingLocaleFileException */ public function displayCorrectMessageOutputEn() { @@ -59,6 +63,8 @@ public function displayCorrectMessageOutputEn() * string or not. * * @test + * + * @throws MissingLocaleFileException */ public function displayCorrectMessageOutputDe() { @@ -79,6 +85,8 @@ public function displayCorrectMessageOutputDe() * is defined in the en_US locale file. * * @test + * + * @throws MissingLocaleFileException */ public function displayDefaultMessageOutputEn() { @@ -99,6 +107,8 @@ public function displayDefaultMessageOutputEn() * is defined in the de_DE locale file. * * @test + * + * @throws MissingLocaleFileException */ public function displayDefaultMessageOutputDe() { @@ -118,6 +128,8 @@ public function displayDefaultMessageOutputDe() * not present (e.g. because the selected language does not exist). * * @test + * + * @throws MissingLocaleFileException */ public function throwMissingLocaleFileException() {