Skip to content

Commit 619c2f4

Browse files
authored
Refactor and Normalize Collection Interface and Implementation (#16)
* add size method * feat: Normalize Interfaces - Refined and standardized interface definitions across the project - Applied Single Responsibility Principle (SRP) to ensure each interface has a clear, singular purpose - Consistent naming conventions were implemented for better readability and maintainability - Added thorough documentation for each interface, including method descriptions and expected behaviors - Organized interfaces within appropriate namespaces to prevent naming collisions and maintain a logical structure - Ensured parameter names in interface methods are consistent with implementing classes - Avoided including constructors in interfaces to maintain flexibility These changes enhance the overall clarity, maintainability, and professional quality of the codebase. * Refactor and Normalize Collection Interface and Implementation - Refactored `Collection` interface for consistency and adherence to best practices. - Ensured all tests are comprehensive and validate the expected behavior.
1 parent 4581aca commit 619c2f4

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

src/DataStructure/Structural/Collection.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ interface Collection extends Countable, Indexable, Modifiable, Searchable
2929
* @param Collection $collection The collection whose elements are to be added
3030
*/
3131
public function addAll(Collection $collection): void;
32+
33+
/**
34+
* Returns an array containing all elements in the collection.
35+
*
36+
* @return array The elements in the collection
37+
*/
38+
public function getItems(): array;
3239
}

tests/DataStructure/Structural/CollectionTest.php

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ final class CollectionTest extends TestCase
1212
public function testAdd(): void
1313
{
1414
$mock = $this->createMock(Collection::class);
15-
$mock->expects($this->once())->method('add')->with('element');
15+
$mock->expects($this->once())
16+
->method('add')
17+
->with('element');
1618

1719
$mock->add('element');
1820
}
@@ -21,32 +23,77 @@ public function testAddAll(): void
2123
{
2224
$collectionMock = $this->createMock(Collection::class);
2325
$mock = $this->createMock(Collection::class);
24-
$mock->expects($this->once())->method('addAll')->with($collectionMock);
26+
$mock->expects($this->once())
27+
->method('addAll')
28+
->with($collectionMock);
2529

2630
$mock->addAll($collectionMock);
2731
}
2832

2933
public function testRemove(): void
3034
{
3135
$mock = $this->createMock(Collection::class);
32-
$mock->method('remove')->with('element')->willReturn(true);
36+
$mock->method('remove')
37+
->with('element')
38+
->willReturn(true);
3339

3440
$this->assertTrue($mock->remove('element'));
3541
}
3642

3743
public function testContains(): void
3844
{
3945
$mock = $this->createMock(Collection::class);
40-
$mock->method('contains')->with('element')->willReturn(true);
46+
$mock->method('contains')
47+
->with('element')
48+
->willReturn(true);
4149

4250
$this->assertTrue($mock->contains('element'));
4351
}
4452

4553
public function testClear(): void
4654
{
4755
$mock = $this->createMock(Collection::class);
48-
$mock->expects($this->once())->method('clear');
56+
$mock->expects($this->once())
57+
->method('clear');
4958

5059
$mock->clear();
5160
}
61+
62+
public function testSize(): void
63+
{
64+
$mock = $this->createMock(Collection::class);
65+
$mock->method('size')
66+
->willReturn(3);
67+
68+
$this->assertSame(3, $mock->size());
69+
}
70+
71+
public function testGetItems(): void
72+
{
73+
$mock = $this->createMock(Collection::class);
74+
$mock->method('getItems')
75+
->willReturn(['item1', 'item2']);
76+
77+
$this->assertSame(['item1', 'item2'], $mock->getItems());
78+
}
79+
80+
public function testGet(): void
81+
{
82+
$mock = $this->createMock(Collection::class);
83+
$mock->method('get')
84+
->with(0)
85+
->willReturn('element');
86+
87+
$this->assertSame('element', $mock->get(0));
88+
}
89+
90+
public function testSet(): void
91+
{
92+
$mock = $this->createMock(Collection::class);
93+
$mock->expects($this->once())
94+
->method('set')
95+
->with(0, 'element');
96+
97+
$mock->set(0, 'element');
98+
}
5299
}

0 commit comments

Comments
 (0)