@@ -21,42 +21,91 @@ class WorkCommandTest extends \PHPUnit_Framework_TestCase
2121 private $ command ;
2222
2323 /**
24- * @var ReflectionClass
24+ * @var string
2525 */
26- private $ reflectedCommand ;
26+ private $ defaultCommandName ;
2727
2828 /**
29- * @var string
29+ * @var array
30+ */
31+ private $ testNewCommandNames ;
32+
33+ /**
34+ * @var array
3035 */
31- private $ intendedCommandName ;
36+ private $ configStub ;
3237
3338 protected function setUp ()
3439 {
3540 $ this ->worker = m::mock (Worker::class);
36- $ this ->command = new WorkCommand ($ this ->worker );
37- $ this ->intendedCommandName = 'doctrine:queue:work ' ;
38-
39- // Use reflection to peek at the worker
40- $ this ->reflectedCommand = new ReflectionClass (get_class ($ this ->command ));
41+ $ this ->defaultCommandName = 'doctrine:queue:work ' ;
42+ $ this ->configStub = ['command_name ' => $ this ->defaultCommandName ];
43+ $ this ->command = new WorkCommand ($ this ->worker , $ this ->configStub );
44+ $ this ->testNewCommandNames = [
45+ 'queue:work ' ,
46+ 'custom-queue:work ' ,
47+ 'doctrine-queue-work ' ,
48+ 'doctrine123-queue-work ' ,
49+ ];
4150 }
4251
4352 public function testHasCorrectWorker ()
4453 {
45- $ reflectionProperty = $ this ->reflectedCommand ->getProperty ('worker ' );
46- $ reflectionProperty ->setAccessible (true );
47-
48- $ worker = $ reflectionProperty ->getValue ($ this ->command );
54+ $ worker = $ this ->getPropertyViaReflection ('worker ' );
4955
5056 $ this ->assertSame ($ this ->worker , $ worker );
5157 }
5258
53- public function testCommandNameIsCorrect ()
59+ /**
60+ * @param $propertyName
61+ * @return mixed
62+ */
63+ private function getPropertyViaReflection ($ propertyName )
5464 {
55- $ reflectionProperty = $ this ->reflectedCommand ->getProperty ('signature ' );
65+ // Use reflection to peek at the worker
66+ $ reflectedCommand = new ReflectionClass (get_class ($ this ->command ));
67+ $ reflectionProperty = $ reflectedCommand ->getProperty ($ propertyName );
5668 $ reflectionProperty ->setAccessible (true );
5769
58- $ signature = $ reflectionProperty ->getValue ($ this ->command );
70+ return $ reflectionProperty ->getValue ($ this ->command );
71+ }
72+
73+ public function testCommandNameIsCorrectAsDefault ()
74+ {
75+ $ signature = $ this ->getPropertyViaReflection ('signature ' );
76+
77+ $ this ->assertEquals ($ this ->defaultCommandName , $ this ->getCommandNameFromSignature ($ signature ));
78+ }
79+
80+ public function testCommandNameCanBeConfigured ()
81+ {
82+ foreach ($ this ->testNewCommandNames as $ newCommandName ) {
83+
84+ $ this ->command = new WorkCommand ($ this ->worker , [
85+ 'command_name ' => $ newCommandName
86+ ]);
87+
88+ $ signature = $ this ->getPropertyViaReflection ('signature ' );
89+
90+ $ this ->assertEquals ($ newCommandName , $ this ->getCommandNameFromSignature ($ signature ));
91+ }
92+ }
93+
94+ public function testCommandNameCanConfiguredToLaravelDefaultBySettingConfigValueToFalse ()
95+ {
96+ $ this ->command = new WorkCommand ($ this ->worker , [
97+ 'command_name ' => false
98+ ]);
99+
100+ $ signature = $ this ->getPropertyViaReflection ('signature ' );
101+
102+ $ this ->assertEquals ('queue:work ' , $ this ->getCommandNameFromSignature ($ signature ));
103+ }
104+
105+ private function getCommandNameFromSignature ($ signature )
106+ {
107+ preg_match (WorkCommand::SIGNATURE_REGEX_PATTERN , $ signature , $ matches );
59108
60- $ this -> assertContains ( $ this -> intendedCommandName , $ signature ) ;
109+ return $ matches [ 1 ] ;
61110 }
62111}
0 commit comments