|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Nejcc\PhpDatatypes\Tests; |
| 5 | + |
| 6 | +use InvalidArgumentException; |
| 7 | +use Nejcc\PhpDatatypes\Composite\Arrays\StringArray; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use TypeError; |
| 10 | + |
| 11 | +class StringArrayTest extends TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Test that the constructor throws an exception if any element is not a string. |
| 15 | + */ |
| 16 | + public function testConstructorThrowsExceptionForInvalidType() |
| 17 | + { |
| 18 | + $this->expectException(InvalidArgumentException::class); |
| 19 | + new StringArray(['validString', 123]); // Invalid, second element is an integer |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Test that the constructor correctly assigns the array of strings. |
| 24 | + */ |
| 25 | + public function testConstructorAssignsValidValue() |
| 26 | + { |
| 27 | + $stringArray = new StringArray(['string1', 'string2']); |
| 28 | + $this->assertEquals(['string1', 'string2'], $stringArray->getValue()); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Test that adding a valid string works. |
| 33 | + */ |
| 34 | + public function testAddString() |
| 35 | + { |
| 36 | + $stringArray = new StringArray(['string1']); |
| 37 | + $stringArray->add('string2'); |
| 38 | + $this->assertEquals(['string1', 'string2'], $stringArray->getValue()); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Test that adding a non-string value throws a TypeError. |
| 43 | + */ |
| 44 | + public function testAddNonStringThrowsException() |
| 45 | + { |
| 46 | + $stringArray = new StringArray(['string1']); |
| 47 | + |
| 48 | + $this->expectException(TypeError::class); // Expect TypeError instead of InvalidArgumentException |
| 49 | + $stringArray->add(123); // Invalid, not a string |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Test that removing an existing string works. |
| 54 | + */ |
| 55 | + public function testRemoveString() |
| 56 | + { |
| 57 | + $stringArray = new StringArray(['string1', 'string2']); |
| 58 | + $removed = $stringArray->remove('string1'); |
| 59 | + |
| 60 | + $this->assertTrue($removed); |
| 61 | + $this->assertEquals(['string2'], $stringArray->getValue()); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Test that removing a non-existing string returns false. |
| 66 | + */ |
| 67 | + public function testRemoveNonExistingString() |
| 68 | + { |
| 69 | + $stringArray = new StringArray(['string1', 'string2']); |
| 70 | + $removed = $stringArray->remove('string3'); |
| 71 | + |
| 72 | + $this->assertFalse($removed); |
| 73 | + $this->assertEquals(['string1', 'string2'], $stringArray->getValue()); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Test that contains() works correctly. |
| 78 | + */ |
| 79 | + public function testContainsString() |
| 80 | + { |
| 81 | + $stringArray = new StringArray(['string1', 'string2']); |
| 82 | + $this->assertTrue($stringArray->contains('string1')); |
| 83 | + $this->assertFalse($stringArray->contains('string3')); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Test that count() returns the correct number of elements. |
| 88 | + */ |
| 89 | + public function testCountStrings() |
| 90 | + { |
| 91 | + $stringArray = new StringArray(['string1', 'string2']); |
| 92 | + $this->assertEquals(2, $stringArray->count()); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Test that toString() returns the correct comma-separated string. |
| 97 | + */ |
| 98 | + public function testToString() |
| 99 | + { |
| 100 | + $stringArray = new StringArray(['string1', 'string2']); |
| 101 | + $this->assertEquals('string1, string2', $stringArray->toString()); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Test that clear() empties the array. |
| 106 | + */ |
| 107 | + public function testClearArray() |
| 108 | + { |
| 109 | + $stringArray = new StringArray(['string1', 'string2']); |
| 110 | + $stringArray->clear(); |
| 111 | + $this->assertEquals([], $stringArray->getValue()); |
| 112 | + } |
| 113 | +} |
0 commit comments