Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

PHPLIB-27/change/addautoloader_and_unit_tests #5

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions FileLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Short Summary
*
* Description
*
* @license Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
* @copyright Copyright © 2016-present Heidelberger Payment GmbH. All rights reserved.
*
* @link http://dev.heidelpay.com/heidelpay-php-api/
*
* @author Simon Gabriel <simon.gabriel@heidelpay.de>
*
* @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);
}
}
}
}
4 changes: 4 additions & 0 deletions composer.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
{
"name": "Stephano Vogel",
"email": "development@heidelpay.de"
},
{
"name": "Simon Gabriel",
"email": "development@heidelpay.de"
}
],
"support" : {
Expand Down
4 changes: 2 additions & 2 deletions lib/Helpers/FileSystem.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand All @@ -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];
}
}
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<filter>
<whitelist>
<directory>./lib/</directory>
<file>./FileLoader.php</file>
</whitelist>
</filter>
</phpunit>
89 changes: 89 additions & 0 deletions tests/unit/FileLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Short Summary
*
* Description
*
* @license Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
* @copyright Copyright © 2016-present Heidelberger Payment GmbH. All rights reserved.
*
* @link http://dev.heidelpay.com/heidelpay-php-api/
*
* @author Simon Gabriel <simon.gabriel@heidelpay.de>
*
* @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);
}
}
14 changes: 13 additions & 1 deletion tests/unit/MessageCodeMapperTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Tests\Unit;

use Heidelpay\MessageCodeMapper\MessageCodeMapper;
use Heidelpay\MessageCodeMapper\Exceptions\MissingLocaleFileException;
use Heidelpay\MessageCodeMapper\MessageCodeMapper;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -26,6 +26,8 @@ class MessageCodeMapperTest extends TestCase
* Unit test for the correct locale after object initialization.
*
* @test
*
* @throws MissingLocaleFileException
*/
public function displayCorrectLocale()
{
Expand All @@ -39,6 +41,8 @@ public function displayCorrectLocale()
* string or not.
*
* @test
*
* @throws MissingLocaleFileException
*/
public function displayCorrectMessageOutputEn()
{
Expand All @@ -59,6 +63,8 @@ public function displayCorrectMessageOutputEn()
* string or not.
*
* @test
*
* @throws MissingLocaleFileException
*/
public function displayCorrectMessageOutputDe()
{
Expand All @@ -79,6 +85,8 @@ public function displayCorrectMessageOutputDe()
* is defined in the en_US locale file.
*
* @test
*
* @throws MissingLocaleFileException
*/
public function displayDefaultMessageOutputEn()
{
Expand All @@ -99,6 +107,8 @@ public function displayDefaultMessageOutputEn()
* is defined in the de_DE locale file.
*
* @test
*
* @throws MissingLocaleFileException
*/
public function displayDefaultMessageOutputDe()
{
Expand All @@ -118,6 +128,8 @@ public function displayDefaultMessageOutputDe()
* not present (e.g. because the selected language does not exist).
*
* @test
*
* @throws MissingLocaleFileException
*/
public function throwMissingLocaleFileException()
{
Expand Down