Skip to content

Commit

Permalink
add test for class map generator
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch committed Aug 21, 2024
1 parent 4fd40d7 commit 6efb9e6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ClassScanner/ComposerMapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public function update(ClassMap $classMap, array $updatedFiles): ClassMap
$deleted = [];
$skip = [];

$generator = new ClassMapGenerator();
foreach ($classMap->getFiles() as $file) {
if (!\in_array($file, $updatedFiles, true)) {
$skip[$file] = true;
Expand All @@ -94,6 +93,8 @@ public function update(ClassMap $classMap, array $updatedFiles): ClassMap

$fileList = new FileList();
$fileList->files = $skip;

$generator = new ClassMapGenerator();
$generator->avoidDuplicateScans($fileList);

foreach ($this->paths as $path) {
Expand Down
79 changes: 79 additions & 0 deletions tests/ClassScanner/ComposerMapGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Aura\Di\ClassScanner;

use PHPUnit\Framework\TestCase;

class ComposerMapGeneratorTest extends TestCase
{
private string $dir;

protected function setUp(): void
{
parent::setUp();
$this->dir = \sys_get_temp_dir() . '/aura_di_' . \bin2hex(random_bytes(4));
\mkdir($this->dir);
}

protected function tearDown(): void
{
parent::tearDown();
$files = new \FilesystemIterator($this->dir);

foreach ($files as $fileinfo) {
\unlink($fileinfo->getPathname());
}

\rmdir($this->dir);
}

public function testAddingClass()
{
$generator = new ComposerMapGenerator([__DIR__ . '/../Fake', $this->dir]);

$classSuffix = \bin2hex(\random_bytes(4));
$newClassName = 'CacheTest\\NewFile' . $classSuffix;

$classMap = $generator->generate();
$this->assertNotContains($newClassName, $classMap->getClasses());

$newFile = $this->createRandomClassFile($newClassName);

$classMap2 = $generator->update($classMap, [$newFile]);
$this->assertContains($newClassName, $classMap2->getClasses());
$this->assertCount(1, $classMap2->getClassSpecificationFor($newClassName)->getAttributes());
}

public function testRemovingClass()
{
$generator = new ComposerMapGenerator([
__DIR__ . '/../Fake',
$this->dir
]);

$classSuffix = \bin2hex(\random_bytes(4));
$newClassName = 'CacheTest\\NewFile' . $classSuffix;
$this->createRandomClassFile($newClassName);

$classMap = $generator->generate();
$this->assertContains($newClassName, $classMap->getClasses());

\unlink($this->dir . '/NewFile' . $classSuffix . '.php');

$classMap2 = $generator->update($classMap, [$this->dir . '/NewFile' . $classSuffix . '.php']);
$this->assertNotContains($newClassName, $classMap2->getClasses());
}

private function createRandomClassFile(string $className, int $value = 0): string
{
$bareName = \array_reverse(\explode('\\', $className))[0];
$phpFile = $this->dir . '/' . $bareName . '.php';
$phpClass = "<?php\nnamespace CacheTest;\nuse Aura\Di\Attribute\Service;use Aura\Di\Fake\FakeAttribute;class {$bareName} { public function __construct(#[FakeAttribute({$value})] \$var) { } }";

file_put_contents($phpFile, $phpClass);
require_once $phpFile;
return $phpFile;
}
}

0 comments on commit 6efb9e6

Please sign in to comment.