Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
koriym committed Nov 28, 2024
1 parent 544cc7f commit e68d3e1
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 65 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"autoload": {
"psr-4": {
"Ray\\Compiler\\": ["src", "src-deprecated"]
"Ray\\Compiler\\": ["src"]
}
},
"autoload-dev": {
Expand Down
2 changes: 0 additions & 2 deletions docs/exmaple/DevInjectorContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Doctrine\Common\Cache\ApcuCache;
use Doctrine\Common\Cache\CacheProvider;
use Ray\Compiler\AbstractInjectorContext;
use Ray\Compiler\DiCompileModule;
use Ray\Compiler\FakeCarModule;
use Ray\Di\AbstractModule;
use Ray\Di\NullCache;

Expand Down
16 changes: 14 additions & 2 deletions tests/DiCompilerTest.php → test-deprecated/DiCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

declare(strict_types=1);

namespace Ray\Compiler;
namespace Ray\test;

use DateTime;
use PHPUnit\Framework\TestCase;
use Ray\Aop\ReflectionMethod;
use Ray\Aop\WeavedInterface;
use Ray\Compiler\DiCompiler;
use Ray\Compiler\Exception\Unbound;
use Ray\Compiler\FakeAop;
use Ray\Compiler\FakeAopInterface;
use Ray\Compiler\FakeAopModule;
use Ray\Compiler\FakeCar;
use Ray\Compiler\FakeCarInterface;
use Ray\Compiler\FakeCarModule;
use Ray\Compiler\FakeInstanceModule;
use Ray\Compiler\FakeLoggerConsumer;
use Ray\Compiler\FakeLoggerInject;
use Ray\Compiler\FakeLoggerModule;
use Ray\Compiler\FakeNullBindingModule;
use Ray\Compiler\ScriptInjector;
use Ray\Di\Name;
use ReflectionParameter;

use function assert;

class DiCompilerTest extends TestCase
Expand Down
1 change: 1 addition & 0 deletions tests/Fake/FakeCarModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protected function configure()
[FakeInterceptor::class]
);
$this->bind(FakeCar::class);
$this->bind(FakeCar2::class);
$this->bind(FakeRobot::class);
}
}
1 change: 1 addition & 0 deletions tests/Fake/FakeToBindSingletonModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ class FakeToBindSingletonModule extends AbstractModule
protected function configure()
{
$this->bind(FakeRobotInterface::class)->to(FakeRobot::class)->in(Scope::SINGLETON);
$this->bind(FakeDependSingleton::class);
}
}
124 changes: 82 additions & 42 deletions tests/ScriptInjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,40 @@

use PHPUnit\Framework\TestCase;
use Ray\Aop\WeavedInterface;
use Ray\Di\AbstractModule;
use Ray\Di\Exception\Unbound;
use Ray\Di\InjectorInterface;
use Ray\Di\NullModule;

use function assert;
use function count;
use function glob;
use function mkdir;
use function serialize;
use function spl_object_hash;
use function unserialize;

class ScriptInjectorTest extends TestCase
/**

Check failure on line 22 in tests/ScriptInjectorTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Empty comment

Check failure on line 22 in tests/ScriptInjectorTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Found multi-line doc comment with single line content, use one-line doc comment instead.
*
*/
class CompileInjectorExtendedScriptInjectorTest extends TestCase

Check failure on line 25 in tests/ScriptInjectorTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Class name doesn't match filename; expected "class ScriptInjectorTest"
{
/** @var ScriptInjector */
/** @var CompileInjector */
private $injector;

protected function setUp(): void
{
deleteFiles(__DIR__ . '/tmp');
$this->injector = new ScriptInjector(__DIR__ . '/tmp');
@mkdir(__DIR__ . '/tmp/com');
$this->injector = new CompileInjector(
__DIR__ . '/tmp',
LazyModule::getInstance(function (): AbstractModule {

Check failure on line 35 in tests/ScriptInjectorTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Closure not using "$this" should be declared static.
return new FakeCarModule();
})
);
}

public function testGetInstance(): FakeCar
{
$diCompiler = new DiCompiler(new FakeCarModule(), __DIR__ . '/tmp');
$diCompiler->compile();
/** @var FakeCar $car */
$car = $this->injector->getInstance(FakeCarInterface::class);
$this->assertInstanceOf(FakeCar::class, $car);

Expand All @@ -48,63 +55,97 @@ public function testDefaultValueInjected(FakeCar $car): void
public function testCompileException(): void
{
$this->expectException(Unbound::class);
$script = new ScriptInjector(__DIR__ . '/tmp');
$script->getInstance('invalid-class'); // @phpstan-ignore-line
$this->injector->getInstance('invalid-class'); // @phpstan-ignore-line
}

public function testToPrototype(): void
{
(new DiCompiler(new FakeToBindPrototypeModule(), __DIR__ . '/tmp'))->compile();
$instance1 = $this->injector->getInstance(FakeRobotInterface::class);
$instance2 = $this->injector->getInstance(FakeRobotInterface::class);
$injector = new CompileInjector(
__DIR__ . '/tmp',
LazyModule::getInstance(function (): AbstractModule {

Check failure on line 65 in tests/ScriptInjectorTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Closure not using "$this" should be declared static.
return new FakeToBindPrototypeModule();
})
);
$instance1 = $injector->getInstance(FakeRobotInterface::class);
$instance2 = $injector->getInstance(FakeRobotInterface::class);
$this->assertNotSame(spl_object_hash($instance1), spl_object_hash($instance2));
}

public function testToSingleton(): void
{
(new DiCompiler(new FakeToBindSingletonModule(), __DIR__ . '/tmp'))->compile();
$instance1 = $this->injector->getInstance(FakeRobotInterface::class);
$instance2 = $this->injector->getInstance(FakeRobotInterface::class);
$injector = new CompileInjector(
__DIR__ . '/tmp',
LazyModule::getInstance(function (): AbstractModule {

Check failure on line 78 in tests/ScriptInjectorTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Closure not using "$this" should be declared static.
return new FakeToBindSingletonModule();
})
);
$instance1 = $injector->getInstance(FakeRobotInterface::class);
$instance2 = $injector->getInstance(FakeRobotInterface::class);
$this->assertSame($instance1, $instance2);
}

public function testToProviderPrototype(): void
{
(new DiCompiler(new FakeToProviderPrototypeModule(), __DIR__ . '/tmp'))->compile();
$instance1 = $this->injector->getInstance(FakeRobotInterface::class);
$instance2 = $this->injector->getInstance(FakeRobotInterface::class);
$injector = new CompileInjector(
__DIR__ . '/tmp',
LazyModule::getInstance(function (): AbstractModule {

Check failure on line 91 in tests/ScriptInjectorTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Closure not using "$this" should be declared static.
return new FakeToProviderPrototypeModule();
})
);
$instance1 = $injector->getInstance(FakeRobotInterface::class);
$instance2 = $injector->getInstance(FakeRobotInterface::class);
$this->assertNotSame($instance1, $instance2);
}

public function testToProviderSingleton(): void
{
(new DiCompiler(new FakeToProviderSingletonModule(), __DIR__ . '/tmp'))->compile();
$instance1 = $this->injector->getInstance(FakeRobotInterface::class);
$instance2 = $this->injector->getInstance(FakeRobotInterface::class);
$injector = new CompileInjector(
__DIR__ . '/tmp',
LazyModule::getInstance(function (): AbstractModule {

Check failure on line 104 in tests/ScriptInjectorTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Closure not using "$this" should be declared static.
return new FakeToProviderSingletonModule();
})
);
$instance1 = $injector->getInstance(FakeRobotInterface::class);
$instance2 = $injector->getInstance(FakeRobotInterface::class);
$this->assertSame($instance1, $instance2);
}

public function testToInstancePrototype(): void
{
(new DiCompiler(new FakeToInstancePrototypeModule(), __DIR__ . '/tmp'))->compile();
$instance1 = $this->injector->getInstance(FakeRobotInterface::class);
$instance2 = $this->injector->getInstance(FakeRobotInterface::class);
$injector = new CompileInjector(
__DIR__ . '/tmp',
LazyModule::getInstance(function (): AbstractModule {
return new FakeToInstancePrototypeModule();
})
);
$instance1 = $injector->getInstance(FakeRobotInterface::class);
$instance2 = $injector->getInstance(FakeRobotInterface::class);
$this->assertNotSame($instance1, $instance2);
}

public function testToInstanceSingleton(): void
{
(new DiCompiler(new FakeToInstanceSingletonModule(), __DIR__ . '/tmp'))->compile();
$instance1 = $this->injector->getInstance(FakeRobotInterface::class);
$instance2 = $this->injector->getInstance(FakeRobotInterface::class);
$injector = new CompileInjector(
__DIR__ . '/tmp',
LazyModule::getInstance(function (): AbstractModule {
return new FakeToInstanceSingletonModule();
})
);
$instance1 = $injector->getInstance(FakeRobotInterface::class);
$instance2 = $injector->getInstance(FakeRobotInterface::class);
$this->assertSame($instance1, $instance2);
}

public function testSerializable(): void
{
$diCompiler = new DiCompiler(new FakeCarModule(), __DIR__ . '/tmp');
$diCompiler->compile();
$injector = unserialize(serialize($this->injector));
$originalInjector = new CompileInjector(
__DIR__ . '/tmp',
LazyModule::getInstance(function (): AbstractModule {
return new FakeToInstanceSingletonModule();
})
);

$injector = unserialize(serialize($originalInjector));
assert($injector instanceof InjectorInterface);
$car = $injector->getInstance(FakeCarInterface::class);
$this->assertInstanceOf(ScriptInjector::class, $injector);
Expand All @@ -113,9 +154,7 @@ public function testSerializable(): void

public function testAop(): void
{
$compiler = new DiCompiler(new FakeCarModule(), __DIR__ . '/tmp');
$compiler->compile();
$injector = new ScriptInjector(__DIR__ . '/tmp');
$injector = $this->injector;
$instance1 = $injector->getInstance(FakeCarInterface::class);
$instance2 = $injector->getInstance(FakeCar::class);
$instance3 = $injector->getInstance(FakeCar2::class);
Expand All @@ -127,22 +166,23 @@ public function testAop(): void

public function testOnDemandSingleton(): void
{
(new DiCompiler(new FakeToBindSingletonModule(), __DIR__ . '/tmp'))->compile();
$dependSingleton1 = $this->injector->getInstance(FakeDependSingleton::class);
$dependSingleton2 = $this->injector->getInstance(FakeDependSingleton::class);
$injector = new CompileInjector(
__DIR__ . '/tmp',
LazyModule::getInstance(function (): AbstractModule {
return new FakeToBindSingletonModule();
})
);
$dependSingleton1 = $injector->getInstance(FakeDependSingleton::class);
$dependSingleton2 = $injector->getInstance(FakeDependSingleton::class);
$hash1 = spl_object_hash($dependSingleton1->robot);
$hash2 = spl_object_hash($dependSingleton2->robot);
$this->assertSame($hash1, $hash2);
}

public function testOnDemandPrototype(): void
{
(new DiCompiler(new FakeCarModule(), __DIR__ . '/tmp'))->compile();
$fakeDependPrototype1 = $this->injector->getInstance(FakeDependPrototype::class);
$fakeDependPrototype2 = $this->injector->getInstance(FakeDependPrototype::class);
$hash1 = spl_object_hash($fakeDependPrototype1->car);
$hash2 = spl_object_hash($fakeDependPrototype2->car);
$this->assertNotSame($hash1, $hash2);
$this->expectException(Unbound::class); // CompileInjector does not support on-demand prototype
$this->injector->getInstance(FakeDependPrototype::class);
}

public function testOptional(): void
Expand Down
36 changes: 18 additions & 18 deletions vendor-bin/tools/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e68d3e1

Please sign in to comment.