Skip to content

Commit

Permalink
Code coverage 100%
Browse files Browse the repository at this point in the history
This commit encompasses the removal of the unused 'FakeFooProvider.php' test file. Additionally, annotations in 'Compiler.php' have been renamed and changes are implemented in 'CompilerTest.php' to reflect these changes. Also, refactor 'Scripts.php' class by removing redundant countable interface and its related methods. Fix indentation error in 'InstanceScript.php'.
  • Loading branch information
koriym committed May 24, 2024
1 parent 8d90cc2 commit 06497ea
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function compile(AbstractModule $module, string $scriptDir): Scripts
// Lock
$fp = fopen($scriptDir . '/compile.lock', 'a+');
if ($fp === false || ! flock($fp, LOCK_EX)) {
// @CoverageIgnoreStart
// @codeCoverageIgnoreStart
throw new CompileLockFailed($scriptDir);
// @CoverageIgnoreEnd
// @codeCoverageIgnoreEnd
}

$scripts = new Scripts();
Expand Down
2 changes: 1 addition & 1 deletion src/InstanceScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private function addDependencyArg(bool $isSingleton, string $index, ReflectionPa
/** @psalm-suppress PossiblyNullReference / The $parameter here can never be null */
$ip = sprintf("['%s', '%s', '%s']", $parameter->getDeclaringClass()->getName(), $parameter->getDeclaringFunction()->getName(), $parameter->name); //@phpstan-ignore-line
$func = $isSingleton ? '$singleton' : '$prototype';
$arg = sprintf("%s('%s', %s)", $func, $index, $ip);
$arg = sprintf("%s('%s', %s)", $func, $index, $ip);
$this->args[] = $arg;
}

Expand Down
10 changes: 1 addition & 9 deletions src/Scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

namespace Ray\Compiler;

use Countable;

use function count;
use function sprintf;
use function str_replace;

final class Scripts implements Countable
final class Scripts
{
/** @var array<string, string> */
private $scripts = [];
Expand All @@ -20,11 +17,6 @@ public function add(string $index, string $script): void
$this->scripts[$index] = $script;
}

public function count(): int
{
return count($this->scripts);
}

public function save(string $scriptDir): void
{
$template = <<<'EOL'
Expand Down
40 changes: 35 additions & 5 deletions tests/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
use Ray\Compiler\CompileVisitor\FakeBazProvider;
use Ray\Compiler\CompileVisitor\FakeFoo;
use Ray\Compiler\CompileVisitor\FakeFooInterface;
use Ray\Compiler\CompileVisitor\FakeQux;
use Ray\Compiler\Exception\InjectionPointUnbound;
use Ray\Compiler\Exception\Unbound;
use Ray\Di\AbstractModule;
use Ray\Di\Exception\Unbound;
use Ray\Di\Scope;

use function get_class;
Expand Down Expand Up @@ -88,13 +89,13 @@ public function testCompileProvider(): void
$module = new class () extends AbstractModule{
protected function configure()
{
$this->bind(FakeFooInterface::class)->toProvider(FakeFooProvider::class);
$this->bind(FakeBazInterface::class)->toProvider(FakeBazProvider::class);
}
};

$this->compiler->compile($module, $this->scriptDir);
$instance = $this->injector->getInstance(FakeFooInterface::class);
$this->assertInstanceOf(FakeFoo::class, $instance);
$instance = $this->injector->getInstance(FakeBazInterface::class);
$this->assertInstanceOf(FakeBaz::class, $instance);
}

public function testCompileComplex(): void
Expand Down Expand Up @@ -197,7 +198,19 @@ protected function configure()
$this->assertIsString(get_class($nullInstance));
}

public function testNoInjectionPointInVeryFirstInject()
public function testUnbound(): void
{
$this->expectException(Unbound::class);
$module = new class () extends AbstractModule{
protected function configure()
{
$this->bind(FakeBar::class);
}
};
$this->compiler->compile($module, $this->scriptDir);
}

public function testNoInjectionPointInVeryFirstInject(): void
{
$this->expectException(InjectionPointUnbound::class);
$module = new class () extends AbstractModule {
Expand All @@ -209,4 +222,21 @@ protected function configure()
$this->compiler->compile($module, $this->scriptDir);
$this->injector->getInstance(FakeBazInterface::class);
}

public function testNullQualifer(): void
{
$module = new class () extends AbstractModule{
protected function configure()
{
$this->bind(FakeQux::class);
$this->bind(FakeBazInterface::class)->toProvider(FakeBazProvider::class)->in(Scope::SINGLETON);
}
};
$this->compiler->compile($module, $this->scriptDir);
$qux = $this->injector->getInstance(FakeQux::class);
$this->assertInstanceOf(FakeQux::class, $qux);
$this->assertSame([null], $qux->baz->qualifiers);
$this->assertInstanceOf(AirInjector::class, $qux->injector);
$this->assertInstanceOf(FakeBazInterface::class, $this->injector->getInstance(FakeBazInterface::class));
}
}
10 changes: 10 additions & 0 deletions tests/Fake/CompileVisitor/FakeBar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Ray\Compiler\CompileVisitor;

class FakeBar
{
public function __construct(FakeFooInterface $foo)
{
}
}
6 changes: 3 additions & 3 deletions tests/Fake/CompileVisitor/FakeBaz.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

class FakeBaz implements FakeBazInterface
{
public $qualifers;
public $qualifiers;

public function __construct(?array $qualifers)
public function __construct(?array $qualifiers)
{
$this->qualifers = $qualifers;
$this->qualifiers = $qualifiers;
}
}
13 changes: 0 additions & 13 deletions tests/Fake/CompileVisitor/FakeFooProvider.php

This file was deleted.

24 changes: 24 additions & 0 deletions tests/Fake/CompileVisitor/FakeQux.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Ray\Compiler\CompileVisitor;

use Ray\Di\InjectorInterface;

final class FakeQux
{
/**
* @var FakeBaz
*/
public $baz;

/**
* @var InjectorInterface
*/
public $injector;

public function __construct(FakeBazInterface $baz, InjectorInterface $injector)
{
$this->baz = $baz;
$this->injector = $injector;
}
}

0 comments on commit 06497ea

Please sign in to comment.