Skip to content

Commit fc5e495

Browse files
committed
Add database to Github Actions and add fixes for other Laravel versions
1 parent 213024f commit fc5e495

File tree

7 files changed

+88
-24
lines changed

7 files changed

+88
-24
lines changed

.github/workflows/run-tests.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,22 @@ jobs:
4444
extensions: mbstring, dom, fileinfo
4545
coverage: none
4646

47+
- name: Set up MySQL
48+
run: |
49+
sudo /etc/init.d/mysql start
50+
mysql -e 'CREATE DATABASE test;' -uroot -proot
51+
4752
- name: Install dependencies
4853
run: |
4954
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
5055
composer update --prefer-stable --prefer-dist --no-interaction --no-suggest
5156
- name: Execute tests
52-
run: vendor/bin/phpunit
57+
env:
58+
CI_DB_DRIVER: mysql
59+
CI_DB_HOST: 127.0.0.1
60+
CI_DB_PORT: 3306
61+
CI_DB_DATABASE: test
62+
CI_DB_USERNAME: root
63+
CI_DB_PASSWORD: root
64+
run: |
65+
vendor/bin/phpunit

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<env name="CACHE_DRIVER" value="array"/>
2222
<env name="SESSION_DRIVER" value="array"/>
2323
<env name="MAIL_DRIVER" value="log"/>
24+
<env name="GOOGLE_APPLICATION_CREDENTIALS" value="./tests/Support/gcloud-key-valid.json" />
2425
<env name="QUEUE_DRIVER" value="cloudtasks"/>
2526
<env name="DB_CONNECTION" value="mysql" />
2627
<env name="DB_HOST" value="127.0.0.1" />

src/CloudTasksJob.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public function getJobId()
3333
return $this->job['uuid'];
3434
}
3535

36+
public function uuid(): string
37+
{
38+
return $this->job['uuid'];
39+
}
40+
3641
public function getRawBody()
3742
{
3843
return json_encode($this->job);

src/CloudTasksQueue.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Contracts\Queue\Queue as QueueContract;
1212
use Illuminate\Queue\Queue as LaravelQueue;
1313
use Illuminate\Support\InteractsWithTime;
14+
use Illuminate\Support\Str;
1415

1516
class CloudTasksQueue extends LaravelQueue implements QueueContract
1617
{
@@ -60,7 +61,12 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0, $attempts = 0)
6061
$httpRequest = $this->createHttpRequest();
6162
$httpRequest->setUrl($this->config['handler']);
6263
$httpRequest->setHttpMethod(HttpMethod::POST);
63-
$httpRequest->setBody($payload);
64+
$httpRequest->setBody(
65+
// Laravel 7+ jobs have a uuid, but Laravel 6 doesn't have it.
66+
// Since we are using and expecting the uuid in some places
67+
// we will add it manually here if it's not present yet.
68+
$this->withUuid($payload)
69+
);
6470

6571
$task = $this->createTask();
6672
$task->setHttpRequest($httpRequest);
@@ -80,6 +86,17 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0, $attempts = 0)
8086
event(new TaskCreated($createdTask));
8187
}
8288

89+
private function withUuid(string $payload): string
90+
{
91+
$decoded = json_decode($payload, true);
92+
93+
if (!isset($decoded['uuid'])) {
94+
$decoded['uuid'] = (string) Str::uuid();
95+
}
96+
97+
return json_encode($decoded);
98+
}
99+
83100
public function pop($queue = null)
84101
{
85102
// TODO: Implement pop() method.

src/LogFake.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,51 @@
77
use PHPUnit\Framework\Assert as PHPUnit;
88
use Psr\Log\LoggerInterface;
99

10-
class LogFake implements LoggerInterface
10+
class LogFake
1111
{
1212
private array $loggedMessages = [];
1313

14-
public function emergency(\Stringable|string $message, array $context = []): void
14+
public function emergency(string $message, array $context = []): void
1515
{
1616
$this->loggedMessages[] = $message;
1717
}
1818

19-
public function alert(\Stringable|string $message, array $context = []): void
19+
public function alert(string $message, array $context = []): void
2020
{
2121
$this->loggedMessages[] = $message;
2222
}
2323

24-
public function critical(\Stringable|string $message, array $context = []): void
24+
public function critical(string $message, array $context = []): void
2525
{
2626
$this->loggedMessages[] = $message;
2727
}
2828

29-
public function error(\Stringable|string $message, array $context = []): void
29+
public function error(string $message, array $context = []): void
3030
{
3131
$this->loggedMessages[] = $message;
3232
}
3333

34-
public function warning(\Stringable|string $message, array $context = []): void
34+
public function warning(string $message, array $context = []): void
3535
{
3636
$this->loggedMessages[] = $message;
3737
}
3838

39-
public function notice(\Stringable|string $message, array $context = []): void
39+
public function notice(string $message, array $context = []): void
4040
{
4141
$this->loggedMessages[] = $message;
4242
}
4343

44-
public function info(\Stringable|string $message, array $context = []): void
44+
public function info(string $message, array $context = []): void
4545
{
4646
$this->loggedMessages[] = $message;
4747
}
4848

49-
public function debug(\Stringable|string $message, array $context = []): void
49+
public function debug(string $message, array $context = []): void
5050
{
5151
$this->loggedMessages[] = $message;
5252
}
5353

54-
public function log($level, \Stringable|string $message, array $context = []): void
54+
public function log($level, string $message, array $context = []): void
5555
{
5656
$this->loggedMessages[] = $message;
5757
}

tests/TaskHandlerTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
use Firebase\JWT\ExpiredException;
66
use Google\Cloud\Tasks\V2\RetryConfig;
77
use Google\Protobuf\Duration;
8+
use Illuminate\Database\Eloquent\Model;
89
use Illuminate\Queue\Events\JobExceptionOccurred;
910
use Illuminate\Queue\Events\JobFailed;
1011
use Illuminate\Queue\Events\JobProcessed;
1112
use Illuminate\Queue\Events\JobProcessing;
13+
use Illuminate\Support\Facades\DB;
1214
use Illuminate\Support\Facades\Event;
1315
use Illuminate\Support\Facades\Log;
16+
use Illuminate\Support\Facades\Queue;
1417
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
1518
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksException;
1619
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksJob;
1720
use Stackkit\LaravelGoogleCloudTasksQueue\LogFake;
1821
use Stackkit\LaravelGoogleCloudTasksQueue\OpenIdVerificator;
22+
use Stackkit\LaravelGoogleCloudTasksQueue\StackkitCloudTask;
1923
use Tests\Support\FailingJob;
2024
use Tests\Support\SimpleJob;
2125
use UnexpectedValueException;
@@ -107,14 +111,9 @@ public function it_can_run_a_task()
107111
Event::fake([JobProcessing::class, JobProcessed::class]);
108112

109113
// Act
110-
$this->dispatch(new SimpleJob())->run();
114+
$this->dispatch(new SimpleJob())->runWithoutExceptionHandler();
111115

112116
// Assert
113-
Event::assertDispatchedTimes(JobProcessing::class, 1);
114-
Event::assertDispatchedTimes(JobProcessed::class, 1);
115-
Event::assertDispatched(JobProcessed::class, function (JobProcessed $event) {
116-
return $event->job->resolveName() === 'Tests\\Support\\SimpleJob';
117-
});
118117
Log::assertLogged('SimpleJob:success');
119118
}
120119

@@ -243,16 +242,17 @@ public function test_max_attempts_in_combination_with_retry_until()
243242
->setMaxRetryDuration(new Duration(['seconds' => 3]))
244243
);
245244
CloudTasksApi::partialMock()->shouldReceive('getRetryUntilTimestamp')->andReturn(time() + 1)->byDefault();
246-
Event::fake([JobExceptionOccurred::class, JobFailed::class]);
245+
247246
$job = $this->dispatch(new FailingJob());
248247

249248
// Act & Assert
250249
$job->run();
251250
$job->run();
252251

253252
# After 2 attempts both Laravel versions should report the same: 2 errors and 0 failures.
254-
Event::assertDispatchedTimes(JobExceptionOccurred::class, 2);
255-
Event::assertNotDispatched(JobFailed::class);
253+
$task = StackkitCloudTask::whereTaskUuid($job->payload['uuid'])->firstOrFail();
254+
$this->assertEquals(2, $task->getNumberOfAttempts());
255+
$this->assertEquals('error', $task->status);
256256

257257
$job->run();
258258

@@ -261,15 +261,15 @@ public function test_max_attempts_in_combination_with_retry_until()
261261
# Laravel 8+: don't fail because retryUntil has not yet passed.
262262

263263
if (version_compare(app()->version(), '8.0.0', '<')) {
264-
Event::assertDispatched(JobFailed::class);
264+
$this->assertEquals('failed', $task->fresh()->status);
265265
return;
266266
} else {
267-
Event::assertNotDispatched(JobFailed::class);
267+
$this->assertEquals('error', $task->fresh()->status);
268268
}
269269

270270
CloudTasksApi::shouldReceive('getRetryUntilTimestamp')->andReturn(time() - 1);
271271
$job->run();
272272

273-
Event::assertDispatched(JobFailed::class);
273+
$this->assertEquals('failed', $task->fresh()->status);
274274
}
275275
}

tests/TestCase.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ protected function getPackageProviders($app)
4242
];
4343
}
4444

45+
/**
46+
* Define database migrations.
47+
*
48+
* @return void
49+
*/
50+
protected function defineDatabaseMigrations()
51+
{
52+
$this->loadMigrationsFrom(__DIR__ . '/../migrations');
53+
$this->loadMigrationsFrom(__DIR__ . '/../vendor/orchestra/testbench-core/laravel/migrations');
54+
}
55+
4556
/**
4657
* Define environment setup.
4758
*
@@ -54,6 +65,17 @@ protected function getEnvironmentSetUp($app)
5465
unlink($file);
5566
}
5667

68+
$app['config']->set('database.default', 'testbench');
69+
$app['config']->set('database.connections.testbench', [
70+
'driver' => 'mysql',
71+
'host' => env('CI_DB_HOST', env('DB_HOST')),
72+
'port' => env('CI_DB_PORT', env('DB_PORT')),
73+
'database' => env('CI_DB_DATABASE', env('DB_DATABASE')),
74+
'username' => env('CI_DB_USERNAME', env('DB_USERNAME')),
75+
'password' => env('CI_DB_PASSWORD', env('DB_PASSWORD')),
76+
'prefix' => '',
77+
]);
78+
5779
$app['config']->set('cache.default', 'file');
5880
$app['config']->set('queue.default', 'my-cloudtasks-connection');
5981
$app['config']->set('queue.connections.my-cloudtasks-connection', [
@@ -65,6 +87,7 @@ protected function getEnvironmentSetUp($app)
6587
'service_account_email' => 'info@stackkit.io',
6688
]);
6789
$app['config']->set('queue.failed.driver', 'database-uuids');
90+
$app['config']->set('queue.failed.database', 'testbench');
6891
}
6992

7093
protected function setConfigValue($key, $value)
@@ -168,4 +191,9 @@ protected function addIdTokenToHeader(?Closure $closure = null): void
168191

169192
request()->headers->set('Authorization', 'Bearer ' . $token);
170193
}
194+
195+
protected function assertDatabaseCount($table, int $count, $connection = null)
196+
{
197+
$this->assertEquals($count, DB::connection($connection)->table($table)->count());
198+
}
171199
}

0 commit comments

Comments
 (0)