4
4
5
5
namespace Doctrine \Tests \Persistence \Mapping ;
6
6
7
+ use ArrayIterator ;
7
8
use Doctrine \Persistence \Mapping \ClassMetadata ;
8
9
use Doctrine \Persistence \Mapping \Driver \ColocatedMappingDriver ;
9
10
use Doctrine \Persistence \Mapping \Driver \MappingDriver ;
12
13
use Doctrine \Tests \Persistence \Mapping \_files \colocated \TestClass ;
13
14
use Generator ;
14
15
use PHPUnit \Framework \TestCase ;
16
+ use Traversable ;
15
17
16
18
use function sort ;
17
19
18
20
class ColocatedMappingDriverTest extends TestCase
19
21
{
20
22
public function testAddGetPaths (): void
21
23
{
22
- $ driver = $ this ->createDriver (__DIR__ . '/_files/colocated ' );
24
+ $ driver = $ this ->createDirectoryPathDriver (__DIR__ . '/_files/colocated ' );
23
25
self ::assertSame ([
24
26
__DIR__ . '/_files/colocated ' ,
25
27
], $ driver ->getPaths ());
@@ -35,7 +37,7 @@ public function testAddGetPaths(): void
35
37
36
38
public function testAddGetExcludePaths (): void
37
39
{
38
- $ driver = $ this ->createDriver (__DIR__ . '/_files/colocated ' );
40
+ $ driver = $ this ->createDirectoryPathDriver (__DIR__ . '/_files/colocated ' );
39
41
self ::assertSame ([], $ driver ->getExcludePaths ());
40
42
41
43
$ driver ->addExcludePaths (['/test/path1 ' , '/test/path2 ' ]);
@@ -48,46 +50,86 @@ public function testAddGetExcludePaths(): void
48
50
49
51
public function testGetSetFileExtension (): void
50
52
{
51
- $ driver = $ this ->createDriver (__DIR__ . '/_files/colocated ' );
53
+ $ driver = $ this ->createDirectoryPathDriver (__DIR__ . '/_files/colocated ' );
52
54
self ::assertSame ('.php ' , $ driver ->getFileExtension ());
53
55
54
56
$ driver ->setFileExtension ('.php1 ' );
55
57
56
58
self ::assertSame ('.php1 ' , $ driver ->getFileExtension ());
57
59
}
58
60
59
- /** @dataProvider pathProvider */
60
- public function testGetAllClassNames (string $ path ): void
61
+ /** @dataProvider directoryPathProvider */
62
+ public function testGetAllClassNamesForDirectory (string $ dirPath ): void
61
63
{
62
- $ driver = $ this ->createDriver ( $ path );
64
+ $ driver = $ this ->createDirectoryPathDriver ( $ dirPath );
63
65
64
66
$ classes = $ driver ->getAllClassNames ();
65
67
66
68
sort ($ classes );
67
69
self ::assertSame ([Entity::class, EntityFixture::class], $ classes );
68
70
}
69
71
72
+ public function testGetAllClassNamesForFilePaths (): void
73
+ {
74
+ $ driver = $ this ->createFilePathsDriver ([
75
+ __DIR__ . '/_files/colocated/Entity.php ' ,
76
+ __DIR__ . '/_files/colocated/TestClass.php ' ,
77
+ ]);
78
+
79
+ $ classes = $ driver ->getAllClassNames ();
80
+
81
+ self ::assertSame ([Entity::class], $ classes , 'The driver should only return the class names for the provided file path names, excluding transient class names. ' );
82
+ }
83
+
84
+ public function testGetAllClassNamesWorksBothForFilePathsAndRetroactivelyAddedDirectoryPaths (): void
85
+ {
86
+ $ driver = $ this ->createFilePathsDriver ([__DIR__ . '/_files/colocated/Entity.php ' ]);
87
+
88
+ $ driver ->addPaths ([__DIR__ . '/_files/colocated/ ' ]);
89
+
90
+ $ classes = $ driver ->getAllClassNames ();
91
+ sort ($ classes );
92
+
93
+ self ::assertSame (
94
+ [Entity::class, EntityFixture::class],
95
+ $ classes ,
96
+ 'The driver should return class names from both the provided file path names and the retroactively added directory paths (these should not be ignored). ' ,
97
+ );
98
+ }
99
+
70
100
/** @return Generator<string, array{string}> */
71
- public static function pathProvider (): Generator
101
+ public static function directoryPathProvider (): Generator
72
102
{
73
103
yield 'straigthforward path ' => [__DIR__ . '/_files/colocated ' ];
74
104
yield 'winding path ' => [__DIR__ . '/../Mapping/_files/colocated ' ];
75
105
}
76
106
77
- private function createDriver (string $ path ): MyDriver
107
+ private function createDirectoryPathDriver (string $ dirPath ): MyDriver
78
108
{
79
- return new MyDriver ([$ path ]);
109
+ return new MyDriver ([$ dirPath ]);
110
+ }
111
+
112
+ /** @param list<string> $filePaths */
113
+ private function createFilePathsDriver (array $ filePaths ): MyDriver
114
+ {
115
+ return new MyDriver (new ArrayIterator ($ filePaths ));
80
116
}
81
117
}
82
118
83
119
final class MyDriver implements MappingDriver
84
120
{
85
121
use ColocatedMappingDriver;
86
122
87
- /** @param non-empty-list<string> $paths One or multiple paths where mapping classes can be found. */
88
- public function __construct (array $ paths )
123
+ /** @param non-empty-list<string>|Traversable<string> $paths One or multiple paths where mapping classes can be found. */
124
+ public function __construct (iterable $ paths )
89
125
{
90
- $ this ->addPaths ($ paths );
126
+ $ isFilePaths = $ paths instanceof Traversable;
127
+
128
+ if (! $ isFilePaths ) {
129
+ $ this ->paths = $ paths ;
130
+ } else {
131
+ $ this ->filePaths = $ paths ;
132
+ }
91
133
}
92
134
93
135
/**
0 commit comments