FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Refactor autoloading and normalize test structure by fadrian06 · Pull Request #709 · flightphp/core · GitHub

/ core Public
21 changes: 8 additions & 13 deletions composer.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,16 @@
"ext-json": "*"
},
"autoload": {
"files": [
"flight/autoload.php"
]
},
"autoload-dev": {
"classmap": [
"tests/classes/"
"flight/Flight.php"
],
"psr-4": {
"Tests\\PHP8\\": [
"tests/named-arguments"
],
"Tests\\Server\\": "tests/server",
"Tests\\ServerV2\\": "tests/server-v2",
"tests\\groupcompactsyntax\\": "tests/groupcompactsyntax"
"flight\\": "flight"
}
},
"autoload-dev": {
"psr-4": {
"tests\\": "tests"
}
},
"require-dev": {
Expand Down Expand Up @@ -80,7 +75,7 @@
],
"test-server-v2": [
"echo \"Running Test Server\"",
"@php -S localhost:8000 -t tests/server-v2"
"@php -S localhost:8000 -t tests/server_v2"
],
"test-performance": [
"echo \"Running Performance Tests...\"",
Expand Down
2 changes: 0 additions & 2 deletions flight/Flight.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use flight\core\EventDispatcher;
use Psr\Container\ContainerInterface;

require_once __DIR__ . '/autoload.php';

/**
* The Flight class is a static representation of the framework.
*
Expand Down
6 changes: 5 additions & 1 deletion flight/autoload.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
require_once __DIR__ . '/Flight.php';
require_once __DIR__ . '/core/Loader.php';

Loader::autoload(true, [dirname(__DIR__)]);
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
return require_once __DIR__ . '/../vendor/autoload.php';
}

Loader::autoload(true, dirname(__DIR__));
13 changes: 7 additions & 6 deletions flight/core/Loader.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ class Loader
* Registers a class.
*
* @param string $name Registry name
* @param class-string<T>|Closure(): T $class Class name or function to instantiate class
* @param class-string<T>|(Closure(): T) $class Class name or function to instantiate class
* @param array<int, mixed> $params Class initialization parameters
* @param ?Closure(T $instance): void $callback $callback Function to call after object instantiation
*
* @param null|(Closure(T $instance): void) $callback $callback Function to call after object instantiation
* @template T of object
*/
public function register(string $name, $class, array $params = [], ?callable $callback = null): void
Expand Down Expand Up @@ -215,12 +214,14 @@ public static function loadClass(string $class): void
*/
public static function addDirectory($dir): void
{
if (\is_array($dir) || \is_object($dir)) {
if (is_iterable($dir)) {
foreach ($dir as $value) {
self::addDirectory($value);
}
} elseif (\is_string($dir)) {
if (!\in_array($dir, self::$dirs, true)) {
} elseif (is_string($dir)) {
$dir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $dir);

if (!in_array($dir, self::$dirs, true)) {
self::$dirs[] = $dir;
}
}
Expand Down
3 changes: 1 addition & 2 deletions index.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

declare(strict_types=1);

require_once 'flight/Flight.php';
// require 'flight/autoload.php';
require_once __DIR__ . '/flight/autoload.php';

Flight::route('/', function () {
echo 'hello world!';
Expand Down
6 changes: 1 addition & 5 deletions phpunit.xml.dist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="tests/phpunit_autoload.php"
executionOrder="random"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
Expand All @@ -15,14 +14,11 @@
<include>
<directory suffix=".php">flight/</directory>
</include>
<exclude>
<file>flight/autoload.php</file>
</exclude>
</coverage>
<testsuites>
<testsuite name="default">
<directory>tests/</directory>
<exclude>tests/named-arguments/</exclude>
<exclude>tests/named_arguments/</exclude>
</testsuite>
</testsuites>
<logging />
Expand Down
2 changes: 1 addition & 1 deletion tests/EventSystemTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace flight\tests;
namespace tests;

use Flight;
use PHPUnit\Framework\TestCase;
Expand Down
51 changes: 30 additions & 21 deletions tests/LoaderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace tests;

use Closure;
use flight\core\Loader;
use tests\classes\Factory;
use tests\classes\User;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use tests\classes\TesterClass;

class LoaderTest extends TestCase
Expand All @@ -17,7 +19,11 @@ class LoaderTest extends TestCase
protected function setUp(): void
{
$this->loader = new Loader();
$this->loader->autoload(true, __DIR__ . '/classes');
}

protected function tearDown(): void
{
Loader::setV2ClassLoading(true);
}

// Autoload a class
Expand Down Expand Up @@ -85,7 +91,7 @@ public function testSharedInstance(): void
// Gets an object from a factory method
public function testRegisterUsingCallable(): void
{
$this->loader->register('e', ['\tests\classes\Factory', 'create']);
$this->loader->register('e', Closure::fromCallable([Factory::class, 'create']));

$obj = $this->loader->load('e');

Expand Down Expand Up @@ -140,29 +146,32 @@ public function testNewInstance6Params(): void

public function testAddDirectoryAsArray(): void
{
$loader = new class extends Loader {
public function getDirectories()
{
return self::$dirs;
}
};
$loader->addDirectory([__DIR__ . '/classes']);
self::assertEquals([
dirname(__DIR__),
__DIR__ . '/classes'
], $loader->getDirectories());
$loader = new Loader();
$loader::addDirectory([__DIR__ . '/classes']);

$dirsProperty = (new ReflectionClass(Loader::class))->getProperty('dirs');

if (PHP_VERSION_ID < 80100) {
$dirsProperty->setAccessible(true);
}

$dirs = $dirsProperty->getValue($loader);

self::assertEquals([__DIR__ . DIRECTORY_SEPARATOR . 'classes'], $dirs);
}

public function testV2ClassLoading(): void
{
$loader = new class extends Loader {
public static function getV2ClassLoading()
{
return self::$v2ClassLoading;
}
};
$this->assertTrue($loader::getV2ClassLoading());
$loader = new Loader();

$v2ClassLoadingProperty = (new ReflectionClass(Loader::class))->getProperty('v2ClassLoading');

if (PHP_VERSION_ID < 80100) {
$v2ClassLoadingProperty->setAccessible(true);
}

$this->assertTrue($v2ClassLoadingProperty->getValue($loader));
$loader::setV2ClassLoading(false);
$this->assertFalse($loader::getV2ClassLoading());
$this->assertFalse($v2ClassLoadingProperty->getValue($loader));
Comment thread
fadrian06 marked this conversation as resolved.
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

namespace tests\named_arguments;

// phpcs:ignore PSR1.Classes.ClassDeclaration.MissingNamespace
class ExampleClass
{
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

declare(strict_types=1);

namespace Tests\PHP8;
namespace tests\named_arguments;

use DateTimeImmutable;
use ExampleClass;
use Flight;
use flight\Container;
use flight\Engine;
Expand Down
9 changes: 0 additions & 9 deletions tests/phpunit_autoload.php

This file was deleted.

Loading
Loading

Back | FazBrowse Home | New Git URL