Skip to content

Commit

Permalink
add test for classmap
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch committed Aug 21, 2024
1 parent a9ca138 commit 4fd40d7
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/ClassScanner/ClassMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,16 @@ public static function fromFile(string $filename): self
*/
public static function fromFileHandle($fileHandle): ClassMap
{
\fseek($fileHandle, 0);
$cacheContents = \stream_get_contents($fileHandle);
if ($cacheContents === '') {
throw new \InvalidArgumentException('Cannot read empty file handle');
}

$cacheContentsJson = \json_decode($cacheContents, true, 512, \JSON_THROW_ON_ERROR);
if (!$cacheContentsJson) {
throw new \InvalidArgumentException('Cannot parse json from file handle');
}

$classMap = new ClassMap($cacheContentsJson['scanPaths'], $cacheContentsJson['basePath']);

Expand Down
82 changes: 82 additions & 0 deletions tests/ClassScanner/ClassMapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Aura\Di\ClassScanner;

use Aura\Di\Fake\FakeAllAttributes;
use Aura\Di\Fake\FakeWorkerAttribute;
use Aura\Di\Resolver\Reflector;
use PHPUnit\Framework\TestCase;

final class ClassMapTest extends TestCase
{
public function testAddRemove(): void
{
$reflector = new Reflector();
$attributes = [...$reflector->yieldAttributes(FakeAllAttributes::class)];
$spec = new ClassSpecification(FakeAllAttributes::class, FakeAllAttributes::FILE, $attributes);

$map = new ClassMap([], '/');
$map->addClass($spec);

$this->assertContains(FakeAllAttributes::class, $map->getClasses());
$this->assertContains(FakeAllAttributes::FILE, $map->getFiles());
$this->assertNotNull($map->getClassSpecificationFor(FakeAllAttributes::class));
$this->assertSame($spec, $map->getClassSpecificationFor(FakeAllAttributes::class));
$this->assertCount(\count($attributes), $map->getAttributeSpecifications());
}

public function testAttributeClass(): void
{
$reflector = new Reflector();
$spec1 = new ClassSpecification(
FakeAllAttributes::class,
FakeAllAttributes::FILE,
[...$reflector->yieldAttributes(FakeAllAttributes::class)]
);
$spec2 = new ClassSpecification(
FakeWorkerAttribute::class,
FakeWorkerAttribute::FILE,
[...$reflector->yieldAttributes(FakeWorkerAttribute::class)]
);

$map = new ClassMap([], '/');
$map->addClass($spec1);
$map->addClass($spec2);

$this->assertFalse($map->isAttributeClassFile(FakeAllAttributes::FILE));
$this->assertTrue($map->isAttributeClassFile(FakeWorkerAttribute::FILE));
}

public function testReconstituteFromFileHandle(): void
{
$reflector = new Reflector();
$spec1 = new ClassSpecification(
FakeAllAttributes::class,
FakeAllAttributes::FILE,
[...$reflector->yieldAttributes(FakeAllAttributes::class)]
);
$spec2 = new ClassSpecification(
FakeWorkerAttribute::class,
FakeWorkerAttribute::FILE,
[...$reflector->yieldAttributes(FakeWorkerAttribute::class)]
);

$stream = fopen('php://temp', 'w');

$map1 = new ClassMap([], '/');
$map1->addClass($spec1);
$map1->addClass($spec2);
$map1->saveToFileHandle($stream);

$map2 = ClassMap::fromFileHandle($stream);

$this->assertContains(FakeAllAttributes::class, $map2->getClasses());
$this->assertContains(FakeAllAttributes::FILE, $map2->getFiles());
$this->assertContains(FakeWorkerAttribute::class, $map2->getClasses());
$this->assertContains(FakeWorkerAttribute::FILE, $map2->getFiles());
$this->assertCount(\count($spec1->getAttributes()), $map2->getClassSpecificationFor(FakeAllAttributes::class)->getAttributes());
$this->assertCount(\count($spec2->getAttributes()), $map2->getClassSpecificationFor(FakeWorkerAttribute::class)->getAttributes());
}
}
2 changes: 2 additions & 0 deletions tests/Fake/FakeAllAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class FakeAllAttributes
#[FakeAttribute(\Attribute::TARGET_CLASS_CONSTANT)]
public const CONSTANT = 1;

public const FILE = __FILE__;

public function __construct(
#[FakeAttribute(\Attribute::TARGET_PARAMETER)]
$parameter,
Expand Down
2 changes: 2 additions & 0 deletions tests/Fake/FakeWorkerAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#[BlueprintNamespace(__NAMESPACE__)]
class FakeWorkerAttribute implements AttributeConfigInterface
{
public const FILE = __FILE__;

private int $someSetting;

public function __construct(int $someSetting = 1)
Expand Down

0 comments on commit 4fd40d7

Please sign in to comment.