2929use PHPUnit \Framework \TestCase ;
3030use Prophecy \Argument ;
3131use Prophecy \PhpUnit \ProphecyTrait ;
32+ use Prophecy \Prophecy \ObjectProphecy ;
3233use Psr \Container \ContainerInterface ;
3334use Symfony \Component \Console \Command \Command ;
3435use Symfony \Component \Finder \Finder ;
@@ -40,6 +41,31 @@ final class DevToolsCommandLoaderTest extends TestCase
4041{
4142 use ProphecyTrait;
4243
44+ /**
45+ * @var ObjectProphecy<FinderFactoryInterface>
46+ */
47+ private ObjectProphecy $ finderFactory ;
48+
49+ /**
50+ * @var ObjectProphecy<Finder>
51+ */
52+ private ObjectProphecy $ finder ;
53+
54+ /**
55+ * @var ObjectProphecy<ContainerInterface>
56+ */
57+ private ObjectProphecy $ container ;
58+
59+ /**
60+ * @return void
61+ */
62+ protected function setUp (): void
63+ {
64+ $ this ->finderFactory = $ this ->prophesize (FinderFactoryInterface::class);
65+ $ this ->finder = $ this ->prophesize (Finder::class);
66+ $ this ->container = $ this ->prophesize (ContainerInterface::class);
67+ }
68+
4369 /**
4470 * @return void
4571 */
@@ -49,30 +75,89 @@ public function constructorWillRegisterOnlyInstantiableCommands(): void
4975 $ commandDirectory = \dirname (__DIR__ , 3 ) . '/src/Console/Command ' ;
5076 $ command = $ this ->prophesize (Command::class);
5177
52- $ finderFactory = $ this ->prophesize (FinderFactoryInterface::class);
53- $ finder = $ this ->prophesize (Finder::class);
54- $ finderFactory ->create ()
55- ->willReturn ($ finder ->reveal ())
78+ $ this ->finderFactory ->create ()
79+ ->willReturn ($ this ->finder ->reveal ())
5680 ->shouldBeCalledOnce ();
57- $ finder ->files ()
58- ->willReturn ($ finder ->reveal ())
81+ $ this -> finder ->files ()
82+ ->willReturn ($ this -> finder ->reveal ())
5983 ->shouldBeCalled ();
60- $ finder ->in (Argument::type ('string ' ))->willReturn ($ finder ->reveal ())->shouldBeCalled ();
61- $ finder ->name ('*.php ' )
62- ->willReturn ($ finder ->reveal ())
84+ $ this -> finder ->in (Argument::type ('string ' ))->willReturn ($ this -> finder ->reveal ())->shouldBeCalled ();
85+ $ this -> finder ->name ('*.php ' )
86+ ->willReturn ($ this -> finder ->reveal ())
6387 ->shouldBeCalled ();
64- $ finder ->getIterator ()
88+ $ this -> finder ->getIterator ()
6589 ->willReturn (new ArrayIterator ([
6690 new SplFileInfo ($ commandDirectory . '/CodeStyleCommand.php ' , '' , 'CodeStyleCommand.php ' ),
6791 ]))->shouldBeCalled ();
6892
69- $ container = $ this ->prophesize (ContainerInterface::class);
70- $ container ->has (CodeStyleCommand::class)->willReturn (true )->shouldBeCalled ();
71- $ container ->get (CodeStyleCommand::class)->willReturn ($ command ->reveal ())->shouldBeCalled ();
93+ $ this ->container ->has (CodeStyleCommand::class)->willReturn (true )->shouldBeCalled ();
94+ $ this ->container ->get (CodeStyleCommand::class)->willReturn ($ command ->reveal ())->shouldBeCalled ();
7295
73- $ loader = new DevToolsCommandLoader ($ finderFactory ->reveal (), $ container ->reveal ());
96+ $ loader = new DevToolsCommandLoader ($ this -> finderFactory ->reveal (), $ this -> container ->reveal ());
7497
7598 self ::assertTrue ($ loader ->has ('code-style ' ));
7699 self ::assertSame ($ command ->reveal (), $ loader ->get ('code-style ' ));
77100 }
101+
102+ /**
103+ * @return void
104+ */
105+ #[Test]
106+ public function constructorWillSkipClassesWithoutAsCommandAttribute (): void
107+ {
108+ $ commandDirectory = \dirname (__DIR__ , 3 ) . '/src/Console/Command ' ;
109+
110+ $ this ->finderFactory ->create ()
111+ ->willReturn ($ this ->finder ->reveal ())
112+ ->shouldBeCalledOnce ();
113+ $ this ->finder ->files ()
114+ ->willReturn ($ this ->finder ->reveal ())
115+ ->shouldBeCalled ();
116+ $ this ->finder ->in (Argument::type ('string ' ))->willReturn ($ this ->finder ->reveal ())->shouldBeCalled ();
117+ $ this ->finder ->name ('*.php ' )
118+ ->willReturn ($ this ->finder ->reveal ())
119+ ->shouldBeCalled ();
120+ $ this ->finder ->getIterator ()
121+ ->willReturn (new ArrayIterator ([
122+ new SplFileInfo ($ commandDirectory . '/FixtureWithoutAsCommand.php ' , '' , 'FixtureWithoutAsCommand.php ' ),
123+ ]))->shouldBeCalled ();
124+
125+ $ loader = new DevToolsCommandLoader ($ this ->finderFactory ->reveal (), $ this ->container ->reveal ());
126+
127+ self ::assertFalse ($ loader ->has ('abstract ' ));
128+ }
129+
130+ /**
131+ * @return void
132+ */
133+ #[Test]
134+ public function constructorWillSkipNonInstantiableAndNonCommandClasses (): void
135+ {
136+ $ commandDirectory = \dirname (__DIR__ , 3 ) . '/src/Console/Command ' ;
137+
138+ $ this ->finderFactory ->create ()
139+ ->willReturn ($ this ->finder ->reveal ())
140+ ->shouldBeCalledOnce ();
141+ $ this ->finder ->files ()
142+ ->willReturn ($ this ->finder ->reveal ())
143+ ->shouldBeCalled ();
144+ $ this ->finder ->in (Argument::type ('string ' ))->willReturn ($ this ->finder ->reveal ())->shouldBeCalled ();
145+ $ this ->finder ->name ('*.php ' )
146+ ->willReturn ($ this ->finder ->reveal ())
147+ ->shouldBeCalled ();
148+ $ this ->finder ->getIterator ()
149+ ->willReturn (new ArrayIterator ([
150+ new SplFileInfo ($ commandDirectory . '/FixtureAbstractCommand.php ' , '' , 'FixtureAbstractCommand.php ' ),
151+ new SplFileInfo (
152+ $ commandDirectory . '/FixtureWithoutCommandParent.php ' ,
153+ '' ,
154+ 'FixtureWithoutCommandParent.php '
155+ ),
156+ ]))->shouldBeCalled ();
157+
158+ $ loader = new DevToolsCommandLoader ($ this ->finderFactory ->reveal (), $ this ->container ->reveal ());
159+
160+ self ::assertFalse ($ loader ->has ('fixture-abstract ' ));
161+ self ::assertFalse ($ loader ->has ('fixture-without-command-parent ' ));
162+ }
78163}
0 commit comments