1
+ <?php
2
+
3
+ namespace Buki \Tests \Commands ;
4
+
5
+ use Buki \Commands \CreateControllerCommand ;
6
+ use Buki \Commands \Exception ;
7
+ use PHPUnit \Framework \TestCase ;
8
+ use Symfony \Component \Console \Application ;
9
+ use Symfony \Component \Console \Command \Command ;
10
+ use Symfony \Component \Console \Exception \RuntimeException ;
11
+ use Symfony \Component \Console \Tester \CommandTester ;
12
+
13
+ /**
14
+ * Class CreateControllerCommandTest
15
+ *
16
+ * @package Buki\Tests\Commands
17
+ */
18
+ class CreateControllerCommandTest extends TestCase
19
+ {
20
+ /** @var CommandTester */
21
+ private $ commandTester ;
22
+
23
+ protected function setUp ()
24
+ {
25
+ $ application = new Application ();
26
+ $ application ->add (new CreateControllerCommand ());
27
+ $ command = $ application ->find ('make:controller ' );
28
+ $ this ->commandTester = new CommandTester ($ command );
29
+ }
30
+
31
+ public function testExecute ()
32
+ {
33
+ $ this ->commandTester ->execute ([
34
+ 'name ' => 'TestController ' ,
35
+ 'path ' => __DIR__ . '/Controllers ' ,
36
+ 'namespace ' => 'Controllers ' ,
37
+ ]);
38
+
39
+ $ this ->assertEquals (Command::SUCCESS , $ this ->commandTester ->getStatusCode ());
40
+ }
41
+
42
+ public function testExecuteShouldThrowExceptionForEmptyName ()
43
+ {
44
+ $ this ->expectException (RuntimeException::class);
45
+ $ this ->expectExceptionMessage ('Not enough arguments (missing: "name") ' );
46
+
47
+ $ this ->commandTester ->execute ([
48
+ // name is required
49
+ ]);
50
+ }
51
+
52
+ public function testExecuteShouldThrowExceptionForExistentController ()
53
+ {
54
+ $ this ->expectException (\Exception::class);
55
+ $ this ->expectExceptionMessage ('The controller already exists! ' );
56
+
57
+ $ this ->commandTester ->execute ([
58
+ 'name ' => 'TestController ' ,
59
+ 'path ' => __DIR__ . '/Controllers ' ,
60
+ 'namespace ' => 'Controllers ' ,
61
+ ]);
62
+ }
63
+
64
+ public function testExecuteShouldThrowExceptionForMkdirPermissionDenied ()
65
+ {
66
+ $ this ->expectException (\Exception::class);
67
+ $ this ->expectExceptionMessage ('mkdir(): Permission denied ' );
68
+
69
+ $ this ->commandTester ->execute ([
70
+ 'name ' => 'TestController ' ,
71
+ 'path ' => '/SomeDirectory ' ,
72
+ 'namespace ' => 'Controllers ' ,
73
+ ]);
74
+ }
75
+ }
0 commit comments