Skip to content

Commit d63bf09

Browse files
committed
Redo tests
1 parent 1b11e20 commit d63bf09

28 files changed

+811
-804
lines changed

README.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,3 @@ This package verifies that the token is digitally signed by Google. Only Google
195195
More information about OpenID Connect:
196196

197197
https://developers.google.com/identity/protocols/oauth2/openid-connect
198-
199-
# Running tests
200-
201-
The test suite uses a emulated version of Google Tasks, thanks to aertje/cloud-tasks-emulator.
202-
203-
To start running the tests locally, first start all Docker services:
204-
205-
```
206-
docker compose up -d
207-
```
208-
209-
This will start the emulator, MySQL and Postgres.

docker-compose.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
services:
2-
gcloud-tasks-emulator:
3-
image: ghcr.io/aertje/cloud-tasks-emulator:latest
4-
command: -host 0.0.0.0 -openid-issuer http://localhost:8980 -port 8123 -queue "projects/my-test-project/locations/europe-west6/queues/barbequeue"
5-
ports:
6-
- "${TASKS_PORT:-8123}:8123"
7-
- 8980:8980
82
mysql:
93
image: mysql:8
104
ports:
@@ -18,4 +12,4 @@ services:
1812
- 5432:5432
1913
environment:
2014
POSTGRES_PASSWORD: 'my-secret-pw'
21-
POSTGRES_DB: 'cloudtasks'
15+
POSTGRES_DB: 'cloudtasks'

phpunit.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
stopOnFailure="false">
1111
<testsuites>
1212
<testsuite name="Testsuite">
13-
<file>tests/ConfigTest.php</file>
14-
<file>tests/GooglePublicKeyTest.php</file>
15-
<file>tests/QueueTest.php</file>
16-
<file>tests/TaskHandlerTest.php</file>
13+
<file>./tests/ConfigTest.php</file>
14+
<file>./tests/TaskHandlerTest.php</file>
1715
</testsuite>
1816
</testsuites>
1917
<php>
@@ -24,7 +22,6 @@
2422
<env name="SESSION_DRIVER" value="array"/>
2523
<env name="MAIL_DRIVER" value="log"/>
2624
<env name="QUEUE_DRIVER" value="cloudtasks"/>
27-
<env name="GOOGLE_APPLICATION_CREDENTIALS" value="./tests/Support/gcloud-key-valid.json" />
2825
<env name="DB_CONNECTION" value="mysql" />
2926
<env name="DB_HOST" value="127.0.0.1" />
3027
<env name="DB_PORT" value="3306" />

setup-test-env.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,20 @@ function env() {
5757
'handler' => 'http://docker.for.mac.localhost:8080/handle-task',
5858
'service_account_email' => 'info@stackkit.io',
5959
];
60+
$queue['failed']['driver'] = 'database-uuids';
6061
file_put_contents('./tests/laravel/config/queue.php', '<?php return ' . var_export($queue, true) . ';');
6162
}
6263

6364
exec('
6465
cd ./tests/laravel &&
6566
mkdir -p tests/Support &&
6667
cp ../../tests/support/SimpleJob.php tests/support/SimpleJob.php &&
68+
cp ../../tests/support/FailingJob.php tests/support/FailingJob.php &&
69+
cp ../../tests/support/FailingJobWithDelay.php tests/support/FailingJobWithDelay.php &&
6770
composer require stackkit/laravel-google-cloud-tasks-queue &&
6871
php artisan migrate
6972
');
7073
}
7174

7275
echo "Started dev server on port 8080!\n";
73-
exec('cd ./tests/laravel && php artisan serve --port=8080 --no-ansi');
76+
exec('cd ./tests/laravel && php artisan serve --host=0.0.0.0 --port=8080 --no-ansi');

src/CloudTasksApi.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stackkit\LaravelGoogleCloudTasksQueue;
6+
7+
use Illuminate\Support\Facades\Facade;
8+
9+
class CloudTasksApi extends Facade
10+
{
11+
protected static function getFacadeAccessor()
12+
{
13+
return 'cloud-tasks-api';
14+
}
15+
16+
public static function fake(): void
17+
{
18+
self::swap(new CloudTasksApiFake());
19+
}
20+
}

src/CloudTasksApiConcrete.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stackkit\LaravelGoogleCloudTasksQueue;
6+
7+
use Google\Cloud\Tasks\V2\CloudTasksClient;
8+
use Google\Cloud\Tasks\V2\RetryConfig;
9+
use Google\Cloud\Tasks\V2\Task;
10+
11+
class CloudTasksApiConcrete implements CloudTasksApiContract
12+
{
13+
/**
14+
* @var CloudTasksClient $client
15+
*/
16+
private $client;
17+
18+
public function __construct(CloudTasksClient $client)
19+
{
20+
$this->client = $client;
21+
}
22+
23+
public function getRetryConfig(string $queueName): RetryConfig
24+
{
25+
return $this->client->getQueue($queueName)->getRetryConfig();
26+
}
27+
28+
public function createTask(string $queueName, Task $task): Task
29+
{
30+
// TODO: Implement createTask() method.
31+
}
32+
33+
public function deleteTask(string $taskName): void
34+
{
35+
// TODO: Implement deleteTask() method.
36+
}
37+
38+
public function getRetryUntilTimestamp(CloudTasksJob $job): ?int
39+
{
40+
// TODO: Implement getRetryUntilTimestamp() method.
41+
}
42+
}

src/CloudTasksApiContract.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stackkit\LaravelGoogleCloudTasksQueue;
6+
7+
use Google\Cloud\Tasks\V2\RetryConfig;
8+
use Google\Cloud\Tasks\V2\Task;
9+
10+
interface CloudTasksApiContract
11+
{
12+
public function getRetryConfig(string $queueName): RetryConfig;
13+
public function createTask(string $queueName, Task $task): Task;
14+
public function deleteTask(string $taskName): void;
15+
public function getRetryUntilTimestamp(CloudTasksJob $job): ?int;
16+
}

src/CloudTasksApiFake.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stackkit\LaravelGoogleCloudTasksQueue;
6+
7+
use Closure;
8+
use Google\Cloud\Tasks\V2\RetryConfig;
9+
use Google\Cloud\Tasks\V2\Task;
10+
use Google\Protobuf\Duration;
11+
use Illuminate\Support\Arr;
12+
use Illuminate\Support\Str;
13+
use PHPUnit\Framework\Assert;
14+
15+
class CloudTasksApiFake implements CloudTasksApiContract
16+
{
17+
public array $createdTasks = [];
18+
public array $deletedTasks = [];
19+
20+
public function getRetryConfig(string $queueName): RetryConfig
21+
{
22+
$retryConfig = new RetryConfig();
23+
24+
$retryConfig
25+
->setMinBackoff((new Duration(['seconds' => 0])))
26+
->setMaxBackoff((new Duration(['seconds' => 0])));
27+
28+
return $retryConfig;
29+
}
30+
31+
public function createTask(string $queueName, Task $task): Task
32+
{
33+
$task->setName(Str::uuid()->toString());
34+
35+
$this->createdTasks[] = compact('queueName', 'task');
36+
37+
return $task;
38+
}
39+
40+
public function deleteTask(string $taskName): void
41+
{
42+
$this->deletedTasks[] = $taskName;
43+
}
44+
45+
public function getRetryUntilTimestamp(CloudTasksJob $job): ?int
46+
{
47+
return null;
48+
}
49+
50+
public function assertTaskDeleted(string $taskName): void
51+
{
52+
$taskUuids = array_map(function ($fullTaskName) {
53+
return Arr::last(explode('/', $fullTaskName));
54+
}, $this->deletedTasks);
55+
56+
Assert::assertTrue(
57+
in_array($taskName, $taskUuids),
58+
'The task [' . $taskName . '] should have been deleted but it is not.'
59+
);
60+
}
61+
62+
public function assertTaskNotDeleted(string $taskName): void
63+
{
64+
$taskUuids = array_map(function ($fullTaskName) {
65+
return Arr::last(explode('/', $fullTaskName));
66+
}, $this->deletedTasks);
67+
68+
Assert::assertTrue(
69+
! in_array($taskName, $taskUuids),
70+
'The task [' . $taskName . '] should not have been deleted but it was.'
71+
);
72+
}
73+
74+
public function assertDeletedTaskCount(int $count): void
75+
{
76+
Assert::assertCount($count, $this->deletedTasks);
77+
}
78+
79+
public function assertTaskCreated(Closure $closure): void
80+
{
81+
$count = count(array_filter($this->createdTasks, function ($createdTask) use ($closure) {
82+
return $closure($createdTask['task'], $createdTask['queueName']);
83+
}));
84+
85+
Assert::assertTrue($count > 0, 'Task was not created.');
86+
}
87+
}

src/CloudTasksJob.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ class CloudTasksJob extends LaravelJob implements JobContract
1717
/**
1818
* @var CloudTasksQueue
1919
*/
20-
private $cloudTasksQueue;
20+
public $cloudTasksQueue;
2121

2222
public function __construct($job, CloudTasksQueue $cloudTasksQueue)
2323
{
2424
$this->job = $job;
2525
$this->container = Container::getInstance();
2626
$this->cloudTasksQueue = $cloudTasksQueue;
27+
$command = unserialize($job['data']['command']);
28+
$this->queue = $command->queue;
2729
}
2830

2931
public function getJobId()
@@ -87,4 +89,11 @@ public function delete()
8789

8890
$this->cloudTasksQueue->delete($this);
8991
}
92+
93+
public function fire()
94+
{
95+
$this->attempts++;
96+
97+
parent::fire();
98+
}
9099
}

src/CloudTasksQueue.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CloudTasksQueue extends LaravelQueue implements QueueContract
1818

1919
private $client;
2020
private $default;
21-
private $config;
21+
public $config;
2222

2323
public function __construct(array $config, CloudTasksClient $client)
2424
{
@@ -75,7 +75,9 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0, $attempts = 0)
7575

7676
MonitoringService::make()->addToMonitor($queue, $task);
7777

78-
$this->client->createTask($queueName, $task);
78+
$createdTask = CloudTasksApi::createTask($queueName, $task);
79+
80+
event(new TaskCreated($createdTask));
7981
}
8082

8183
public function pop($queue = null)
@@ -100,14 +102,16 @@ public function delete(CloudTasksJob $job)
100102
{
101103
$config = $this->config;
102104

105+
$queue = $job->getQueue() ?: $this->config['queue']; // @todo: make this a helper method somewhere.
106+
103107
$taskName = $this->client->taskName(
104108
$config['project'],
105109
$config['location'],
106-
$job->getQueue(),
110+
$queue,
107111
request()->header('X-Cloudtasks-Taskname')
108112
);
109113

110-
$this->client->deleteTask($taskName);
114+
CloudTasksApi::deleteTask($taskName);
111115
}
112116

113117
/**

0 commit comments

Comments
 (0)