88
99class CreateHookCommandTest extends ConsoleTestCase
1010{
11+ protected $ mock ;
12+
13+ protected $ supportedHooks = [
14+ 'applypatch-msg ' ,
15+ 'pre-applypatch ' ,
16+ 'post-applypatch ' ,
17+ 'pre-commit ' ,
18+ 'pre-merge-commit ' ,
19+ 'prepare-commit-msg ' ,
20+ 'commit-msg ' ,
21+ 'post-commit ' ,
22+ 'pre-rebase ' ,
23+ 'post-checkout ' ,
24+ 'post-merge ' ,
25+ 'pre-push ' ,
26+ 'pre-receive ' ,
27+ 'update ' ,
28+ 'proc-receive ' ,
29+ 'post-receive ' ,
30+ 'post-update ' ,
31+ 'reference-transaction ' ,
32+ 'push-to-checkout ' ,
33+ 'pre-auto-gc ' ,
34+ 'post-rewrite ' ,
35+ 'sendemail-validate ' ,
36+ 'fsmonitor-watchman ' ,
37+ 'p4-changelist ' ,
38+ 'p4-prepare-changelist ' ,
39+ 'p4-post-changelist ' ,
40+ 'p4-pre-submit ' ,
41+ 'post-index-change ' ,
42+ ];
43+
44+ /**
45+ * Creates the temporal filesystem structure for the tests and mocks the 'getcwd' method for return this path.
46+ *
47+ * @return void
48+ */
1149 protected function setUp (): void
1250 {
1351 parent ::setUp ();
1452 $ this ->deleteDirStructure ();
1553
1654 $ this ->copyDefaultPrecommitToTestDirectory ();
1755 mkdir ($ this ->path . '/.git/hooks ' , 0777 , true );
18- }
1956
20- protected function copyDefaultPrecommitToTestDirectory ()
21- {
22- mkdir ($ this ->path . '/hooks ' , 0777 , true );
23- shell_exec ("cp -r hooks " . $ this ->path );
57+ $ this ->mock = $ this ->getMockRootDirectory ();
58+ $ this ->mock ->enable ();
2459 }
2560
2661 protected function tearDown (): void
2762 {
63+ $ this ->mock ->disable ();
2864 $ this ->deleteDirStructure ();
2965 }
3066
3167 /**
68+ * Copy the 'hooks' directory of the application with de default script for the hooks ('hooks/precommit.php') to the root of the temporal
69+ * directory for tests.
70+ *
71+ * @return void
72+ */
73+ protected function copyDefaultPrecommitToTestDirectory ()
74+ {
75+ mkdir ($ this ->path . '/hooks ' , 0777 , true );
76+ shell_exec ('cp -r hooks ' . $ this ->path );
77+ }
78+
79+ /**
80+ * Mocks the 'getcwd' method for return the root of the filesystem for this tests.
81+ *
3282 * @return PhpmockMock
3383 */
3484 public function getMockRootDirectory (): PhpmockMock
@@ -38,7 +88,7 @@ public function getMockRootDirectory(): PhpmockMock
3888 ->setName ('getcwd ' )
3989 ->setFunction (
4090 function () {
41- return $ this ->getPath () ;
91+ return $ this ->path ;
4292 }
4393 );
4494
@@ -48,14 +98,10 @@ function () {
4898 /** @test */
4999 function it_creates_default_script_for_precommit_when_is_called_without_arguments ()
50100 {
51- $ mock = $ this ->getMockRootDirectory ();
52- $ mock ->enable ();
53-
54101 $ this ->artisan ('hook ' )
55102 ->containsStringInOutput ('Hook pre-commit created ' );
56103
57104 $ this ->assertFileExists ($ this ->path . '/.git/hooks/pre-commit ' , file_get_contents ('hooks/pre-commit.php ' ));
58- $ mock ->disable ();
59105 }
60106
61107 /**
@@ -64,37 +110,54 @@ function it_creates_default_script_for_precommit_when_is_called_without_argument
64110 */
65111 function it_creates_default_script_in_the_hook_passed_as_argument ()
66112 {
67- $ hooks = [
68- 'applypatch-msg ' => 'applypatch-msg ' ,
69- 'commit-msg ' => 'commit-msg ' ,
70- 'fsmonitor-watchman ' => 'fsmonitor-watchman ' ,
71- 'post-update ' => 'post-update ' ,
72- 'pre-applypatch ' => 'pre-applypatch ' ,
73- 'pre-commit ' => 'pre-commit ' ,
74- 'prepare-commit-msg ' => 'prepare-commit-msg ' ,
75- 'pre-push ' => 'pre-push ' ,
76- 'pre-rebase ' => 'pre-rebase ' ,
77- 'pre-receive ' => 'pre-receive ' ,
78- 'update ' => 'update ' ,
79- ];
80-
81- $ mock = $ this ->getMockRootDirectory ();
82- $ mock ->enable ();
83-
84- foreach ($ hooks as $ hook ) {
113+ foreach ($ this ->supportedHooks as $ hook ) {
85114 $ this ->artisan ("hook $ hook " )
86115 ->containsStringInOutput ("Hook $ hook created " );
87116
88117 $ this ->assertFileExists ($ this ->path . "/.git/hooks/ $ hook " , file_get_contents ('hooks/pre-commit.php ' ));
89118 }
119+ }
90120
121+ /**
122+ * @test
123+ * Only is tested pre-push hook but it could be any hook.
124+ */
125+ function it_sets_a_custom_script_as_some_hook ()
126+ {
127+ $ hookContent = 'my custom script ' ;
128+ $ scriptFilePath = $ this ->path . '/MyScript.php ' ;
129+ file_put_contents ($ scriptFilePath , $ hookContent );
91130
92- $ mock ->disable ();
131+ $ this ->artisan ("hook pre-push $ scriptFilePath " )
132+ ->containsStringInOutput ("Hook pre-push created " );
133+
134+ $ this ->assertFileExists ($ this ->path . "/.git/hooks/pre-push " , $ scriptFilePath );
93135 }
94136
95- //Tests
96- // Establezco pre-commit por defecto
97- // Establezco cualquier otro hook (o todos)
98- // Establezco un script personalizado en algún hook
99- // Si el hook no valida lanzo mensaje de error igual que en CleanHookCommand con los hooks soportadosdcon
137+ /** @test */
138+ function it_shows_an_error_message_when_is_setted_a_custom_script_without_specifying_the_hook ()
139+ {
140+ $ hookContent = 'my custom script ' ;
141+ $ scriptFilePath = $ this ->path . '/MyScript.php ' ;
142+ file_put_contents ($ scriptFilePath , $ hookContent );
143+
144+ $ supportedHooks2String = implode (', ' , $ this ->supportedHooks );
145+ $ this ->artisan ("hook $ scriptFilePath " )
146+ ->containsStringInOutput ("' $ scriptFilePath' is not a valid git hook. Avaliable hooks are: " )
147+ ->containsStringInOutput ($ supportedHooks2String );
148+
149+ $ assertFileDoesNotExist = $ this ->assertFileDoesNotExist ;
150+ $ this ->$ assertFileDoesNotExist ($ this ->path . "/.git/hooks/pre-commit " , $ scriptFilePath );
151+ }
152+
153+ /** @test */
154+ function it_shows_an_error_message_when_is_setted_an_invalid_hook ()
155+ {
156+ $ noSupportedHook = 'no-valid ' ;
157+
158+ $ supportedHooks2String = implode (', ' , $ this ->supportedHooks );
159+ $ this ->artisan ("hook $ noSupportedHook " )
160+ ->containsStringInOutput ("' $ noSupportedHook' is not a valid git hook. Avaliable hooks are: " )
161+ ->containsStringInOutput ($ supportedHooks2String );
162+ }
100163}
0 commit comments