Skip to content

Commit

Permalink
Adds domain specific http tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfox committed Mar 7, 2021
1 parent f138834 commit ad556ee
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 13 deletions.
1 change: 1 addition & 0 deletions config/queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
'credentials' => 'path/to/your/keyfile',
'transport' => 'rest',
],
'domain' => env('GOOGLE_CLOUD_TASKS_DOMAIN'),
],
],

Expand Down
35 changes: 31 additions & 4 deletions src/RequestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@
use Google\Cloud\Tasks\V2beta3\HttpMethod;
use Google\Cloud\Tasks\V2beta3\HttpRequest;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Http\Request;

class RequestGenerator
{
use ConnectionRetrieval;

/**
* @var UrlGenerator
*/
private $generator;
/**
* @var Request
*/
private $request;

public function __construct(UrlGenerator $generator)
public function __construct(UrlGenerator $generator, Request $request)
{
$this->generator = $generator;
$this->request = $request;
}

public function forAppEngine(string $payload, string $connection) : AppEngineHttpRequest
Expand All @@ -39,11 +47,30 @@ public function forHttpHandler(string $payload, string $connection) : HttpReques
->setHttpMethod(HttpMethod::POST)
->setBody($payload)
->setUrl(
//TODO generate domain for url or use current host
$this->cloudTasksUrl($connection)
);
}

protected function cloudTasksUrl($connection) : string
{
$config = $this->getConfig($connection);

if ($config['domain'] ?? false) {
return 'https://' . $config['domain'] .
$this->generator->route(
'google.tasks',
['connection' => $connection],
true
)
);
false
);
}

return $this->generator->secure(
$this->generator->route(
'google.tasks',
['connection' => $connection],
false
)
);
}
}
62 changes: 53 additions & 9 deletions tests/JobDispatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Google\Cloud\Tasks\V2beta3\CloudTasksClient;
use Google\Cloud\Tasks\V2beta3\Task;
use Illuminate\Support\Facades\Config;
use Mockery\MockInterface;
use Orchestra\Testbench\TestCase as Orchestra;
use TradeCoverExchange\GoogleCloudTaskLaravel\CloudTaskServiceProvider;
Expand All @@ -12,43 +13,86 @@

class JobDispatchTest extends Orchestra
{
protected $client;

public function setUp(): void
{
parent::setUp();

$this->mock(CloudTaskClientFactory::class, function (MockInterface $factory) {
$client = \Mockery::mock(CloudTasksClient::class);

$client->shouldReceive('createTask')
->with(
'projects/test/locations/europe-west1/queues/default',
\Mockery::type(Task::class)
)
->once();
$this->client = \Mockery::mock(CloudTasksClient::class);

$factory->shouldReceive('make')
->withAnyArgs()
->once()
->andReturn($client);
->andReturn($this->client);
});

$this->withFactories(__DIR__.'/database/factories');
}

public function testCanDispatchToHttpQueue()
{
$this->client->shouldReceive('createTask')
->withArgs(function (string $project, Task $task) {
$this->assertSame('projects/test/locations/europe-west1/queues/default', $project);
$this->assertInstanceOf(Task::class, $task);
$this->assertSame(
'https://localhost/_/google-tasks/http_cloud_tasks',
$task->getHttpRequest()->getUrl()
);

return true;
})
->once();

dispatch(new JobDummy())
->onConnection('http_cloud_tasks');
}

public function testCanDispatchToHttpQueueToConfiguredDomain()
{
Config::set('queue.connections.http_cloud_tasks.domain', 'test.tradecoverexchange.com');

$this->client->shouldReceive('createTask')
->withArgs(function (string $project, Task $task) {
$this->assertSame('projects/test/locations/europe-west1/queues/default', $project);
$this->assertInstanceOf(Task::class, $task);
$this->assertSame(
'https://test.tradecoverexchange.com/_/google-tasks/http_cloud_tasks',
$task->getHttpRequest()->getUrl()
);

return true;
})
->once();

dispatch(new JobDummy())
->onConnection('http_cloud_tasks');
}

public function testCanDispatchToAppEngineQueue()
{
$this->client->shouldReceive('createTask')
->with(
'projects/test/locations/europe-west1/queues/default',
\Mockery::type(Task::class)
)
->once();

dispatch(new JobDummy())
->onConnection('app_engine_tasks');
}

public function testCanDispatchWithDelay()
{
$this->client->shouldReceive('createTask')
->with(
'projects/test/locations/europe-west1/queues/default',
\Mockery::type(Task::class)
)
->once();

dispatch(new JobDummy())
->onConnection('app_engine_tasks')
->delay(3000);
Expand Down

0 comments on commit ad556ee

Please sign in to comment.