Skip to content
Merged
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
10 changes: 5 additions & 5 deletions src/Override.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static function apply(
$autoloadCollection->addFile($path);
}

// Load the classes that are affected by the fqfc-override converter.
// Load the classes that are affected by the FQFC-override converter.
\stream_wrapper_unregister('file');
\stream_wrapper_register('file', FileStreamWrapper::class);
foreach ($autoloadCollection->getFilePaths() as $file) {
Expand All @@ -167,6 +167,7 @@ public static function apply(
/**
* @param string[]|Closure[] $mappings
* @param string $namespace
*
* @return array
*/
private static function buildMappings(array $mappings, string $namespace): array
Expand Down Expand Up @@ -217,12 +218,11 @@ private static function addNamespaceData(array $directories, array $functionMapp
public static function getFunctionMappings(string $filePath): array
{
$filePath = \realpath($filePath);
$dirPath = dirname($filePath);

$mappings = [];
foreach (self::$dirFunctionCallMappings as $dir => $functionMappings) {
if (\substr($filePath, 0, \strlen($dir)) === $dir) {
$mappings = \array_merge($mappings, $functionMappings);
}
if (isset(self::$dirFunctionCallMappings[$dirPath])) {
$mappings = \array_merge($mappings, self::$dirFunctionCallMappings[$dirPath]);
}

if (isset(self::$fileFunctionCallMappings[$filePath])) {
Expand Down
35 changes: 35 additions & 0 deletions tests/AbstractIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* PHP Autoload Override (https://github.com/adriansuter/php-autoload-override)
*
* @license https://github.com/adriansuter/php-autoload-override/blob/master/LICENSE.md (MIT License)
*/

declare(strict_types=1);

use AdrianSuter\Autoload\Override\Override;
use PHPUnit\Framework\TestCase;

abstract class AbstractIntegrationTest extends TestCase
{
private $overrideApplied = false;

private static $classLoader;

public static function setUpBeforeClass()
{
self::$classLoader = require(__DIR__ . '/../vendor/autoload.php');
}

protected function setUp()
{
parent::setUp();

if (!$this->overrideApplied) {
$this->overrideApplied = true;
Override::apply(self::$classLoader, $this->getOverrideDeclarations());
}
}

abstract protected function getOverrideDeclarations(): array;
}
51 changes: 51 additions & 0 deletions tests/IntegrationClassMapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* PHP Autoload Override (https://github.com/adriansuter/php-autoload-override)
*
* @license https://github.com/adriansuter/php-autoload-override/blob/master/LICENSE.md (MIT License)
*/

declare(strict_types=1);

class IntegrationClassMapTest extends AbstractIntegrationTest
{
protected function getOverrideDeclarations(): array
{
return [
\My\Integration\TestClassMapOverride\Calculator::class => [
'cos' => function (float $arg): float {
return \sin($arg);
},
],
'My\\Integration\\TestClassMapOverride\\' => [
'cos' => function (float $arg): float {
return $arg * 2;
},
]
];
}

public function testCalculator()
{
$calculator = new \My\Integration\TestClassMapOverride\Calculator();

// Calls \cos() > Overridden by FQCN-declaration.
$this->assertEquals(\sin(\pi() / 2), $calculator->cos(\pi() / 2));
}

public function testGeometry()
{
$geometry = new \My\Integration\TestClassMapOverride\SubNamespace\Geometry();

// Calls \cos() > Overridden by FQNS-declaration.
$this->assertEquals(1, $geometry->cos(0.5));
}

public function testOtherCalculator()
{
$otherCalculator = new \My\Integration\TestClassMapOverride\OtherCalculator();

// Calls \cos() > Overridden by FQNS-declaration.
$this->assertEquals(2, $otherCalculator->cos(1));
}
}
143 changes: 143 additions & 0 deletions tests/IntegrationClosureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php
/**
* PHP Autoload Override (https://github.com/adriansuter/php-autoload-override)
*
* @license https://github.com/adriansuter/php-autoload-override/blob/master/LICENSE.md (MIT License)
*/

declare(strict_types=1);

class IntegrationClosureTest extends AbstractIntegrationTest
{
protected function getOverrideDeclarations(): array
{
return [
// Test that the class loader can find the file to that class.
\My\Integration\TestClosureOverride\Clock::class => [
'time' => function () {
return 100;
},
'rand' => function (int $min, int $max): int {
return $min + $max;
}
],
\My\Integration\TestClosureOverride\SubSpace\Digital::class => [
'rand' => function (int $min, int $max): int {
return 2 * ($min + $max);
}
],
// Test that Override finds PSR-4 directory belonging to the namespace "My\Integration" and from there the
// directory to the "TestClosureOverride" sub namespace.
'My\\Integration\\TestClosureOverride\\' => [
'rand' => function (int $min, int $max): int {
return 3 * ($min + $max);
}
],
// Test that Override finds PSR-4 directory belonging to the namespace "My\Integration" and from there the
// directory to the "TestClosureOverride\SubSpace" sub namespace.
'My\\Integration\\TestClosureOverride\\SubSpace\\' => [
'time' => function () {
return 101;
}
],
\My\Integration\TestClosureOverride\OtherSpace\Other::class => [
'time' => function (): int {
return 102;
}
]
];
}

public function testClock()
{
$clock = new \My\Integration\TestClosureOverride\Clock();

// Calls \time() > Overridden by FQCN-declaration.
$this->assertEquals(100, $clock->time());

// Calls \time()-alias > Overridden by FQCN-declaration.
$this->assertEquals(100, $clock->timeWithAlias());

// Calls \rand() > Overridden by FQCN-declaration.
$this->assertEquals(11, $clock->rand(1, 10));
}

public function testSubClock()
{
$subClock = new \My\Integration\TestClosureOverride\SubClock();

// Calls \time() > No override.
$this->assertGreaterThanOrEqual(\time(), $subClock->time());

// Parent > Calls \time()-alias > Overridden by FQCN-declaration.
$this->assertEquals(100, $subClock->timeWithAlias());

// Calls parent > Calls \rand() > Overridden by FQCN-declaration.
$this->assertEquals(9, $subClock->rand(3, 6));
}

public function testDigital()
{
$digital = new \My\Integration\TestClosureOverride\SubSpace\Digital();

// Calls \time() > Overridden by FQNS-declaration.
$this->assertEquals(101, $digital->time());

// Calls \rand() > Overridden by FQCN-declaration.
$this->assertEquals(22, $digital->rand(1, 10));
}

public function testSubDigital()
{
$subDigital = new \My\Integration\TestClosureOverride\SubDigital();

// Parent > Calls \time() > Overridden by FQNS-declaration.
$this->assertEquals(101, $subDigital->time());

// Calls \time() > No override.
$this->assertGreaterThanOrEqual(\time(), $subDigital->subTime());

// Calls \rand() > Overridden by FQNS.
$this->assertEquals(33, $subDigital->rand(1, 10));
}

public function testSubSpaceClock()
{
$subSpaceClock = new \My\Integration\TestClosureOverride\SubSpace\SubSpaceClock();

// Calls \time() > Overridden by FQNS.
$this->assertEquals(101, $subSpaceClock->time());

// Parent > Calls \time()-alias > Overridden by FQCN-declaration.
$this->assertEquals(100, $subSpaceClock->timeWithAlias());

// Calls \rand() > No override.
$rand = $subSpaceClock->rand(1, 10);
$this->assertGreaterThanOrEqual(1, $rand);
$this->assertLessThanOrEqual(10, $rand);
}

public function testOther()
{
$other = new \My\Integration\TestClosureOverride\OtherSpace\Other();

// Calls \time() > Overridden by FQCN-declaration.
$this->assertEquals(102, $other->time());

// Calls \rand() > No override.
$rand = $other->rand(1, 10);
$this->assertGreaterThanOrEqual(1, $rand);
$this->assertLessThanOrEqual(10, $rand);
}

public function testSpace()
{
$space = new \My\Integration\TestClosureOverride\OtherSpace\Space();

// Calls \time() > No override.
$this->assertGreaterThanOrEqual(\time(), $space->time());

// Calls time() which is a local function in the namespace.
$this->assertEquals(105, $space->timeLocal());
}
}
34 changes: 34 additions & 0 deletions tests/IntegrationCustomNamespaceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* PHP Autoload Override (https://github.com/adriansuter/php-autoload-override)
*
* @license https://github.com/adriansuter/php-autoload-override/blob/master/LICENSE.md (MIT License)
*/

declare(strict_types=1);

class IntegrationCustomNamespaceTest extends AbstractIntegrationTest
{
public static function setUpBeforeClass()
{
require_once(__DIR__ . '/assets/PHPCustomAutoloadOverride.php');
}

protected function getOverrideDeclarations(): array
{
return [
\My\Integration\TestCustomNamespaceOverride\Hash::class => [
'md5' => 'PHPCustomAutoloadOverride'
]
];
}

public function testHash()
{
$hash = new \My\Integration\TestCustomNamespaceOverride\Hash();

// Calls \md5() > Overridden by FQCN-declaration.
$GLOBALS['md5_return'] = '---';
$this->assertEquals('---', $hash->hash('1'));
}
}
61 changes: 61 additions & 0 deletions tests/IntegrationNamespaceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* PHP Autoload Override (https://github.com/adriansuter/php-autoload-override)
*
* @license https://github.com/adriansuter/php-autoload-override/blob/master/LICENSE.md (MIT License)
*/

declare(strict_types=1);

class IntegrationNamespaceTest extends AbstractIntegrationTest
{
public static function setUpBeforeClass()
{
require_once(__DIR__ . '/assets/PHPAutoloadOverride.php');
}

protected function getOverrideDeclarations(): array
{
return [
\My\Integration\TestNamespaceOverride\Moon::class => [
'time'
],
'My\\Integration\\TestNamespaceOverride\\' => [
'substr'
],
];
}

public function testEarth()
{
$earth = new \My\Integration\TestNamespaceOverride\Earth();

// Calls substr() which is a local function in the namespace.
$this->assertEquals('GFE', $earth->substrLocal());

// Calls \substr() > Overridden by FQNS-declaration.
$GLOBALS['substr_return'] = 'XYZ';
$this->assertEquals('XYZ', $earth->substrGlobal());

// Calls \time() > No override.
$GLOBALS['time_return'] = 3;
$this->assertGreaterThanOrEqual(\time(), $earth->time());
}

public function testMoon()
{
$moon = new \My\Integration\TestNamespaceOverride\Moon();

// Calls \time() > Overridden by FQCN.
$GLOBALS['time_return'] = 1;
$this->assertEquals(1, $moon->time());

// Calls \time()-alias > Overridden by FQCN.
$GLOBALS['time_return'] = 2;
$this->assertEquals(2, $moon->timeUseAlias());

// Calls \substr() > Overridden by FQNS-declaration.
$GLOBALS['substr_return'] = 'ZZZ';
$this->assertEquals('ZZZ', $moon->substr('AAA', 0, 2));
}
}
Loading