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 eb7acc0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 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
36 changes: 33 additions & 3 deletions tests/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
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,6 +198,18 @@ protected function configure()
$this->assertIsString(get_class($nullInstance));
}

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

Check failure on line 207 in tests/CompilerTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Class Ray\Compiler\CompileVisitor\FakeBar not found.
}
};
$this->compiler->compile($module, $this->scriptDir);
}

public function testNoInjectionPointInVeryFirstInject()

Check failure on line 213 in tests/CompilerTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Method Ray\Compiler\CompilerTest::testNoInjectionPointInVeryFirstInject() has no return type specified.
{
$this->expectException(InjectionPointUnbound::class);
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);

Check failure on line 231 in tests/CompilerTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Class Ray\Compiler\CompileVisitor\FakeQux not found.
$this->bind(FakeBazInterface::class)->toProvider(FakeBazProvider::class)->in(Scope::SINGLETON);
}
};
$this->compiler->compile($module, $this->scriptDir);
$qux = $this->injector->getInstance(FakeQux::class);

Check failure on line 236 in tests/CompilerTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Class Ray\Compiler\CompileVisitor\FakeQux not found.
$this->assertInstanceOf(FakeQux::class, $qux);

Check failure on line 237 in tests/CompilerTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Class Ray\Compiler\CompileVisitor\FakeQux not found.
$this->assertSame([null], $qux->baz->qualifers);

Check failure on line 238 in tests/CompilerTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Access to property $baz on an unknown class Ray\Compiler\CompileVisitor\FakeQux.
$this->assertInstanceOf(AirInjector::class, $qux->injector);

Check failure on line 239 in tests/CompilerTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Access to property $injector on an unknown class Ray\Compiler\CompileVisitor\FakeQux.
$this->assertInstanceOf(FakeBazInterface::class, $this->injector->getInstance(FakeBazInterface::class));
}
}
13 changes: 0 additions & 13 deletions tests/Fake/CompileVisitor/FakeFooProvider.php

This file was deleted.

0 comments on commit eb7acc0

Please sign in to comment.