Skip to content

Commit 8f2fff5

Browse files
committed
Empty collection not working - #58
1 parent 8dc5cd9 commit 8f2fff5

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

src/Builder/ClassBuilderCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public static function emptyList(): self
3535

3636
private function __construct(ClassBuilder ...$classBuilders)
3737
{
38+
$this->items = [];
39+
3840
foreach ($classBuilders as $classBuilder) {
3941
$this->items[$this->identifier($classBuilder)] = $classBuilder;
4042
}

src/Builder/FileCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public static function emptyList(): self
3232

3333
private function __construct(File ...$files)
3434
{
35+
$this->items = [];
36+
3537
foreach ($files as $file) {
3638
$this->items[$this->identifier($file)] = $file;
3739
}

tests/Builder/ClassBuilderCollectionTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,15 @@ public function it_adds_anonymous_class_builder(): void
8484
$items = $cut->items();
8585
$this->assertArrayHasKey(\spl_object_hash($classBuilder), $items);
8686
}
87+
88+
/**
89+
* @test
90+
*/
91+
public function it_can_be_empty(): void
92+
{
93+
$cut = ClassBuilderCollection::emptyList();
94+
$this->assertCount(0, $cut);
95+
96+
$this->assertSame([], $cut->items());
97+
}
8798
}

tests/Builder/FileCollectionTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModelingTest\CodeAst\Builder;
12+
13+
use OpenCodeModeling\CodeAst\Builder\ClassBuilder;
14+
use OpenCodeModeling\CodeAst\Builder\FileCollection;
15+
use PHPUnit\Framework\TestCase;
16+
17+
final class FileCollectionTest extends TestCase
18+
{
19+
/**
20+
* @test
21+
*/
22+
public function it_adds_same_class_builder_not_twice(): void
23+
{
24+
$classBuilder = ClassBuilder::fromScratch('MyClass');
25+
$cut = FileCollection::fromItems($classBuilder);
26+
$this->assertCount(1, $cut);
27+
28+
$cut->add($classBuilder);
29+
$this->assertCount(1, $cut);
30+
31+
$items = $cut->items();
32+
$this->assertArrayHasKey('\\MyClass', $items);
33+
}
34+
35+
/**
36+
* @test
37+
*/
38+
public function it_adds_and_removes_class_builder(): void
39+
{
40+
$classBuilderWithoutNamespace = ClassBuilder::fromScratch('MyClass');
41+
$classBuilderNamespace = ClassBuilder::fromScratch('MyClass', 'MyNamespace');
42+
43+
$cut = FileCollection::fromItems($classBuilderWithoutNamespace);
44+
$this->assertCount(1, $cut);
45+
46+
$cut->add($classBuilderNamespace);
47+
$this->assertCount(2, $cut);
48+
49+
$items = $cut->items();
50+
$this->assertArrayHasKey('\\MyClass', $items);
51+
$this->assertArrayHasKey('\\MyNamespace\\MyClass', $items);
52+
53+
$i = 0;
54+
55+
foreach ($cut as $item) {
56+
$this->assertInstanceOf(ClassBuilder::class, $item);
57+
if ($i === 0) {
58+
$this->assertSame('MyClass', $item->getName());
59+
$this->assertNull($item->getNamespace());
60+
} else {
61+
$this->assertSame('MyClass', $item->getName());
62+
$this->assertSame('MyNamespace', $item->getNamespace());
63+
}
64+
$i++;
65+
}
66+
67+
$cut->remove($classBuilderNamespace);
68+
$this->assertCount(1, $cut);
69+
70+
$items = $cut->items();
71+
$this->assertArrayHasKey('\\MyClass', $items);
72+
}
73+
74+
/**
75+
* @test
76+
*/
77+
public function it_adds_anonymous_class_builder(): void
78+
{
79+
$classBuilder = ClassBuilder::fromScratch(null);
80+
81+
$cut = FileCollection::fromItems($classBuilder);
82+
$this->assertCount(1, $cut);
83+
84+
$items = $cut->items();
85+
$this->assertArrayHasKey(\spl_object_hash($classBuilder), $items);
86+
}
87+
88+
/**
89+
* @test
90+
*/
91+
public function it_can_be_empty(): void
92+
{
93+
$cut = FileCollection::emptyList();
94+
$this->assertCount(0, $cut);
95+
96+
$this->assertSame([], $cut->items());
97+
}
98+
}

0 commit comments

Comments
 (0)