Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 22 additions & 31 deletions appengine/php72/tasks/snippets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP.

`src/create_task.php` is a simple function to create app engine queue tasks.
`src/create_task.php` is a simple function to create tasks with App Engine routing.

## Setup:

Expand All @@ -23,21 +23,34 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
Run `php composer.phar install` (if composer is installed locally) or `composer install`
(if composer is installed globally).

5. Create a Queue
To create a queue using the Cloud SDK, use the following gcloud command:
```sh
gcloud beta tasks queues create-app-engine-queue my-appengine-queue
```
6. Identify the Location
6. Set environment variables:

First, your project ID:

export PROJECT_ID=my-project-id

Then the queue ID, as specified at queue creation time. Queue IDs already
created can be listed with `gcloud alpha tasks queues list`.

export QUEUE_ID=my-appengine-queue

Determine the location ID, which can be discovered with
`gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in
the "name" value (for instance, if the name is
"projects/my-project/locations/us-central1/queues/my-pull-queue", then the
location is "us-central1").
Then, identify the queue location

7. Run `php src/SNIPPET_NAME.php`. The usage will print for each if no arguments are provided:
Determine the location ID, which can be discovered with
`gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in
the "name" value (for instance, if the name is
"projects/my-project/locations/us-central1/queues/my-pull-queue", then the
location is "us-central1").

export LOCATION_ID=us-central1

## Using App Engine Routing
1. Run `php src/create_task.php`. The usage will print for each if no arguments are provided:

```
$> php src/create_task.php
Expand All @@ -51,25 +64,3 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP
## Licensing

* See [LICENSE](../../LICENSE)




## Creating a queue


## Running the Samples

Set the environment variables:

Set environment variables:

First, your project ID:

export PROJECT_ID=my-project-id

Then the queue ID, as specified at queue creation time. Queue IDs already
created can be listed with `gcloud alpha tasks queues list`.

export QUEUE_ID=my-appengine-queue

2 changes: 1 addition & 1 deletion appengine/php72/tasks/snippets/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"google/cloud-tasks": "^0.8.0"
"google/cloud-tasks": "^0.9.0"
},
"require-dev": {
"phpunit/phpunit": "^5",
Expand Down
58 changes: 58 additions & 0 deletions tasks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Google Cloud Tasks Samples

## Description

Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP.

`src/create_http_task.php` is a simple function to create tasks with an HTTP target.

## Setup:

1. **Enable APIs** - [Enable the Cloud Tasks API](https://console.cloud.google.com/flows/enableapi?apiid=cloudtasks)
and create a new project or select an existing project.
2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click "New Credentials"
and select "Service Account Key". Create a new service account, use the JSON key type, and
select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
to the path of the JSON key that was downloaded.
3. **Clone the repo** and cd into this directory

```sh
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
$ cd php-docs-samples/tasks
```
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
Run `php composer.phar install` (if composer is installed locally) or `composer install`
(if composer is installed globally).
5. Create a Queue
To create a queue using the Cloud SDK, use the following gcloud command:
```sh
gcloud beta tasks queues create-app-engine-queue my-appengine-queue
```

## Using an HTTP Target
1. Run `php src/create_http_task.php`. The usage will print for each if no arguments are provided:

```
$> php src/create_http_task.php
Usage: php src/create_http_task.php PROJECT_ID LOCATION_ID QUEUE_ID URL [PAYLOAD]
```

where:
* `PROJECT_ID` is your Google Cloud Project id.
* `QUEUE_ID` is your queue id (ie my-appengine-queue).
Queue IDs already created can be listed with `gcloud alpha tasks queues list`.
* `LOCATION_ID` is the location of your queue.
Determine the location ID, which can be discovered with
`gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in
the "name" value (for instance, if the name is
"projects/my-project/locations/us-central1/queues/my-pull-queue", then the
location is "us-central1").
* `URL` is the full URL to your target endpoint.

## Contributing changes

* See [CONTRIBUTING.md](../../CONTRIBUTING.md)

## Licensing

* See [LICENSE](../../LICENSE)
9 changes: 9 additions & 0 deletions tasks/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"require": {
"google/cloud-tasks": "^0.9.0"
},
"require-dev": {
"phpunit/phpunit": "^5",
"google/cloud-tools": "^0.8.5"
}
}
31 changes: 31 additions & 0 deletions tasks/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2019 Google LLC.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="Cloud Tasks test">
<directory>test</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
68 changes: 68 additions & 0 deletions tasks/src/create_http_task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Copyright 2019 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/tasks/README.md
*/

// Include Google Cloud dependendencies using Composer
require_once __DIR__ . '/../vendor/autoload.php';

if ($argc < 5 || $argc > 6) {
return printf("Usage: php %s PROJECT_ID LOCATION_ID QUEUE_ID URL [PAYLOAD]\n", __FILE__);
}
list($_, $projectId, $locationId, $queueId, $url, $payload) = $argv;

# [START cloud_tasks_create_http_task]
use Google\Cloud\Tasks\V2beta3\CloudTasksClient;
use Google\Cloud\Tasks\V2beta3\HttpMethod;
use Google\Cloud\Tasks\V2beta3\HttpRequest;
use Google\Cloud\Tasks\V2beta3\Task;

/** Uncomment and populate these variables in your code */
// $projectId = 'The Google project ID';
// $locationId = 'The Location ID';
// $queueId = 'The Cloud Tasks Queue ID';
// $url = 'The full url path that the task request will be sent to.'
// $payload = 'The payload your task should carry to the task handler. Optional';

// Instantiate the client and queue name.
$client = new CloudTasksClient();
$queueName = $client->queueName($projectId, $locationId, $queueId);

// Create an Http Request Object.
$httpRequest = new HttpRequest();
// The full url path that the task request will be sent to.
$httpRequest->setUrl($url);
// POST is the default HTTP method, but any HTTP method can be used.
$httpRequest->setHttpMethod(HttpMethod::POST);
// Setting a body value is only compatible with HTTP POST and PUT requests.
if (isset($payload)) {
$httpRequest->setBody($payload);
}

// Create a Cloud Task object.
$task = new Task();
$task->setHttpRequest($httpRequest);

// Send request and print the task name.
$response = $client->createTask($queueName, $task);
printf('Created task %s' . PHP_EOL, $response->getName());

# [END cloud_tasks_create_http_task]
59 changes: 59 additions & 0 deletions tasks/test/tasksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright 2019 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Samples\Tasks\Tests;

use Google\Cloud\TestUtils\TestTrait;
use PHPUnit\Framework\TestCase;

/**
* Unit Tests for tasks commands.
*/
class TasksTest extends TestCase
{
use TestTrait;

public function testCreateHttpTask()
{
$queue = $this->requireEnv('CLOUD_TASKS_APPENGINE_QUEUE');
$location = $this->requireEnv('CLOUD_TASKS_LOCATION');

$output = $this->runSnippet('create_http_task', [
$location,
$queue,
'http://example.com',
'Task Details',
]);
$taskNamePrefix = sprintf('projects/%s/locations/%s/queues/%s/tasks/',
self::$projectId,
$location,
$queue
);

$expectedOutput = sprintf('Created task %s', $taskNamePrefix);
$this->assertContains($expectedOutput, $output);
}

private function runSnippet($sampleName, $params = [])
{
$argv = array_merge([0, self::$projectId], array_values($params));
$argc = count($argv);
ob_start();
require __DIR__ . "/../src/$sampleName.php";
return ob_get_clean();
}
}