|
7 | 7 | use Aternos\IO\Exception\GetTargetException;
|
8 | 8 | use Aternos\IO\Exception\IOException;
|
9 | 9 | use Aternos\IO\Exception\MissingPermissionsException;
|
| 10 | +use Aternos\IO\Interfaces\Features\GetChildrenInterface; |
| 11 | +use Aternos\IO\Interfaces\Features\ReadInterface; |
| 12 | +use Aternos\IO\Interfaces\Features\WriteInterface; |
10 | 13 | use Aternos\IO\Interfaces\IOElementInterface;
|
11 | 14 | use Aternos\IO\Interfaces\Types\DirectoryInterface;
|
12 | 15 | use Aternos\IO\Interfaces\Types\FileInterface;
|
13 | 16 | use Aternos\IO\Interfaces\Types\Link\LinkInterface;
|
14 | 17 | use Aternos\IO\System\Directory\Directory;
|
| 18 | +use Aternos\IO\System\File\File; |
| 19 | +use Aternos\IO\System\FilesystemElement; |
15 | 20 | use Aternos\IO\System\Link\DirectoryLink;
|
16 | 21 | use Aternos\IO\System\Link\FileLink;
|
17 | 22 | use Aternos\IO\System\Link\Link;
|
18 | 23 | use Aternos\IO\Test\Unit\System\FilesystemTestCase;
|
19 | 24 | use Generator;
|
| 25 | +use InvalidArgumentException; |
| 26 | +use PHPUnit\Framework\Attributes\TestWith; |
20 | 27 |
|
21 | 28 | class DirectoryTest extends FilesystemTestCase
|
22 | 29 | {
|
@@ -120,6 +127,52 @@ public function testThrowsExceptionOnGetChildrenRecursiveWithMissingPermissions(
|
120 | 127 | chmod($path, 0777);
|
121 | 128 | }
|
122 | 129 |
|
| 130 | + /** |
| 131 | + * @param class-string[] $features |
| 132 | + * @param class-string $expected |
| 133 | + * @return void |
| 134 | + * @throws IOException |
| 135 | + */ |
| 136 | + #[TestWith([FileInterface::class])] |
| 137 | + #[TestWith([File::class])] |
| 138 | + #[TestWith([DirectoryInterface::class])] |
| 139 | + #[TestWith([Directory::class])] |
| 140 | + #[TestWith([LinkInterface::class])] |
| 141 | + #[TestWith([FileLink::class])] |
| 142 | + #[TestWith([DirectoryLink::class])] |
| 143 | + #[TestWith([Link::class])] |
| 144 | + #[TestWith([[WriteInterface::class, ReadInterface::class], File::class])] |
| 145 | + #[TestWith([[GetChildrenInterface::class], Directory::class])] |
| 146 | + public function testGetChild(array|string $features, ?string $expected = null): void |
| 147 | + { |
| 148 | + if (is_string($features)) { |
| 149 | + $features = [$features]; |
| 150 | + } |
| 151 | + |
| 152 | + if ($expected === null) { |
| 153 | + $expected = $features[0]; |
| 154 | + } |
| 155 | + |
| 156 | + $path = $this->getTmpPath(); |
| 157 | + $element = $this->createElement($path); |
| 158 | + |
| 159 | + $child = $element->getChild("test", ...$features); |
| 160 | + $this->assertInstanceOf($expected, $child); |
| 161 | + $this->assertEquals($path . "/test", $child->getPath()); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @return void |
| 166 | + * @throws IOException |
| 167 | + */ |
| 168 | + public function testGetChildThrowsExceptionOnInvalidFeatureCombination(): void |
| 169 | + { |
| 170 | + $this->expectException(InvalidArgumentException::class); |
| 171 | + $this->expectExceptionMessage("No supported child class found for features: Aternos\IO\Interfaces\Features\WriteInterface, Aternos\IO\Interfaces\Features\GetChildrenInterface"); |
| 172 | + $element = $this->createElement($this->getTmpPath()); |
| 173 | + $element->getChild("test", WriteInterface::class, GetChildrenInterface::class); |
| 174 | + } |
| 175 | + |
123 | 176 | /**
|
124 | 177 | * @throws GetTargetException
|
125 | 178 | * @throws MissingPermissionsException
|
|
0 commit comments