Skip to content

Commit 217b4cf

Browse files
committed
Add ClassStringFactoryContainer and corresponding tests for class-string<Countable> validation
1 parent dfc2fa8 commit 217b4cf

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\Fixtures\Types;
6+
7+
use Countable;
8+
9+
class ClassStringFactoryContainer
10+
{
11+
/**
12+
* @param class-string<Countable> $class
13+
*/
14+
public static function makeCountable(string $class): string
15+
{
16+
return $class;
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use TypePHP\Tests\Fixtures\Domain\Car;
6+
use TypePHP\Tests\Fixtures\Types\ClassStringFactoryContainer;
7+
use TypePHP\Tests\Fixtures\Types\CountableArrayAccess;
8+
9+
describe('class-string<Interface> Subtype Validation', function () {
10+
11+
test('accepts concrete class string implementing the target interface', function () {
12+
expect(ClassStringFactoryContainer::makeCountable(CountableArrayAccess::class))
13+
->toBe(CountableArrayAccess::class)
14+
;
15+
});
16+
17+
test('accepts the interface string itself', function () {
18+
expect(ClassStringFactoryContainer::makeCountable(Countable::class))
19+
->toBe(Countable::class)
20+
;
21+
});
22+
23+
test('throws TypeError when class string does not implement the target interface', function () {
24+
expect(fn () => ClassStringFactoryContainer::makeCountable(Car::class))
25+
->toThrow(TypeError::class, 'must be a class-string of Countable')
26+
;
27+
});
28+
});

0 commit comments

Comments
 (0)