17
17
18
18
class TaskHandlerTest extends TestCase
19
19
{
20
- private $ taskHandler ;
21
-
22
- private $ fakeCommand ;
23
-
24
- public function setUp (): void
25
- {
26
- parent ::setUp ();
27
-
28
- $ this ->fakeCommand = Mockery::mock (Command::class)->makePartial ();
29
-
30
- config ()->set ('laravel-google-cloud-scheduler.app_url ' , 'my-application.com ' );
31
-
32
- request ()->headers ->add (['Authorization ' => 'Bearer test ' ]);
33
-
34
- $ this ->taskHandler = new TaskHandler (
35
- $ this ->fakeCommand ,
36
- app (Schedule::class),
37
- Container::getInstance ()
38
- );
39
- }
40
-
41
20
#[Test]
42
21
public function it_executes_the_incoming_command ()
43
22
{
23
+ // Arrange
44
24
OpenIdVerificator::fake ();
45
25
46
- $ this ->fakeCommand ->shouldReceive ('capture ' )->andReturn ('env ' );
47
-
48
- $ output = $ this ->taskHandler ->handle ();
26
+ // Act
27
+ $ output = $ this ->call ('POST ' , '/cloud-scheduler-job ' , content: 'php artisan env ' )->content ();
49
28
29
+ // Assert
50
30
$ this ->assertStringContainsString ('The application environment is [testing] ' , $ output );
51
31
}
52
32
53
33
#[Test]
54
34
public function it_requires_a_jwt ()
55
35
{
56
- $ this ->fakeCommand ->shouldReceive ('capture ' )->andReturn ('env ' );
57
-
58
- request ()->headers ->remove ('Authorization ' );
36
+ // Act
37
+ $ response = $ this ->call ('POST ' , '/cloud-scheduler-job ' , content: 'php artisan env ' );
59
38
60
- $ this ->expectException (CloudSchedulerException::class);
39
+ // Assert
40
+ $ this ->assertStringContainsString ('Missing [Authorization] header ' , $ response ->content ());
41
+ $ response ->assertStatus (500 );
61
42
62
- $ this ->taskHandler ->handle ();
63
43
}
64
44
65
45
#[Test]
66
46
public function it_requires_a_jwt_signed_by_google ()
67
47
{
68
- $ this ->fakeCommand ->shouldReceive ('capture ' )->andReturn ('env ' );
69
-
70
- $ this ->expectException (UnexpectedValueException::class);
71
-
72
- $ this ->taskHandler ->handle ();
48
+ // Act
49
+ $ response = $ this
50
+ ->withToken ('hey ' )
51
+ ->call ('POST ' , '/cloud-scheduler-job ' , server: ['HTTP_AUTHORIZATION ' => 'Bearer 123 ' ], content: 'php artisan env ' );
52
+
53
+ // Assert
54
+ $ this ->assertStringContainsString ('Wrong number of segments ' , $ response ->content ());
55
+ $ response ->assertStatus (500 );
73
56
}
74
57
75
58
#[Test]
@@ -78,11 +61,11 @@ public function it_prevents_overlapping_if_the_command_is_scheduled_without_over
78
61
OpenIdVerificator::fake ();
79
62
Event::fake ();
80
63
81
- $ this ->fakeCommand ->shouldReceive ('capture ' )->andReturn ('test:command ' );
82
-
83
64
cache ()->clear ();
84
65
85
- $ this ->taskHandler ->handle ();
66
+ $ this ->assertLoggedLines (0 );
67
+
68
+ $ this ->call ('POST ' , '/cloud-scheduler-job ' , content: 'php artisan test:command ' );
86
69
87
70
$ this ->assertLoggedLines (1 );
88
71
$ this ->assertLogged ('TestCommand ' );
@@ -93,13 +76,13 @@ public function it_prevents_overlapping_if_the_command_is_scheduled_without_over
93
76
94
77
cache ()->add ($ mutex , true , 60 );
95
78
96
- $ this ->taskHandler -> handle ( );
79
+ $ this ->call ( ' POST ' , ' /cloud-scheduler-job ' , content: ' php artisan test:command ' );
97
80
98
81
$ this ->assertLoggedLines (1 );
99
82
100
83
cache ()->delete ($ mutex );
101
84
102
- $ this ->taskHandler -> handle ( );
85
+ $ this ->call ( ' POST ' , ' /cloud-scheduler-job ' , content: ' php artisan test:command ' );
103
86
104
87
$ this ->assertLoggedLines (2 );
105
88
}
@@ -108,11 +91,8 @@ public function it_prevents_overlapping_if_the_command_is_scheduled_without_over
108
91
public function it_runs_the_before_and_after_callbacks ()
109
92
{
110
93
OpenIdVerificator::fake ();
111
- Event::fake ();
112
-
113
- $ this ->fakeCommand ->shouldReceive ('capture ' )->andReturn ('test:command2 ' );
114
94
115
- $ this ->taskHandler -> handle ( );
95
+ $ this ->call ( ' POST ' , ' /cloud-scheduler-job ' , content: ' php artisan test:command2 ' );
116
96
117
97
$ this ->assertLoggedLines (3 );
118
98
$ this ->assertLogged ('log after ' );
@@ -124,10 +104,8 @@ public function it_runs_the_before_and_after_callbacks()
124
104
public function it_can_run_the_schedule_run_command ()
125
105
{
126
106
OpenIdVerificator::fake ();
127
- Event::fake (TaskOutput::class);
128
- $ this ->fakeCommand ->shouldReceive ('capture ' )->andReturn ('schedule:run ' );
129
107
130
- $ this ->taskHandler -> handle ( );
108
+ $ this ->call ( ' POST ' , ' /cloud-scheduler-job ' , content: ' php artisan schedule:run ' );
131
109
132
110
$ this ->assertLoggedLines (5 );
133
111
$ this ->assertLogged ('TestCommand ' );
0 commit comments