Skip to content

Commit e02b166

Browse files
committed
Add email alert search sample and its test
1 parent b9c3ec5 commit e02b166

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

jobs/cjd_sample/cjd_sample.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
$application->add(new BasicJobSample());
2929
$application->add(new BatchOperationSample());
3030
$application->add(new CommuteSearchSample());
31+
$application->add(new EmailAlertSearchSample());
3132
$application->add(new FeaturedJobsSearchSample());
3233
$application->add(new GeneralSearchSample());
3334
$application->add(new LocationSearchSample());

jobs/cjd_sample/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"src/basic_job_sample.php",
1818
"src/batch_operation_sample.php",
1919
"src/commute_search_sample.php",
20+
"src/email_alert_search_sample.php",
2021
"src/featured_jobs_search_sample.php",
2122
"src/general_search_sample.php",
2223
"src/job_service_connector.php",
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Jobs;
19+
20+
21+
use Google_Service_JobService_JobQuery;
22+
use Google_Service_JobService_RequestMetadata;
23+
use Google_Service_JobService_SearchJobsRequest;
24+
use Google_Service_JobService_SearchJobsResponse;
25+
use Symfony\Component\Console\Command\Command;
26+
use Symfony\Component\Console\Input\InputInterface;
27+
use Symfony\Component\Console\Output\OutputInterface;
28+
29+
/**
30+
* The sample in this file introduce how to do a email alert search.
31+
*/
32+
final class EmailAlertSearchSample extends Command
33+
{
34+
# [START search_for_alerts]
35+
/**
36+
* Search jobs for alert.
37+
*
38+
* @param string|null $companyName
39+
* @return Google_Service_JobService_SearchJobsResponse
40+
*/
41+
public static function search_for_alerts(string $companyName = null)
42+
{
43+
// Make sure to set the requestMetadata the same as the associated search request
44+
$requestMetadata = new Google_Service_JobService_RequestMetadata();
45+
// Make sure to hash your userID
46+
$requestMetadata->setUserId('HashedUserId');
47+
// Make sure to hash the sessionID
48+
$requestMetadata->setSessionId('HashedSessionId');
49+
// Domain of the website where the search is conducted
50+
$requestMetadata->setDomain('www.google.com');
51+
52+
$request = new Google_Service_JobService_SearchJobsRequest();
53+
$request->setRequestMetadata($requestMetadata);
54+
$request->setMode('JOB_SEARCH');
55+
if (isset($companyName)) {
56+
$jobQuery = new Google_Service_JobService_JobQuery();
57+
$jobQuery->setCompanyNames(array($companyName));
58+
$request->setQuery($jobQuery);
59+
}
60+
61+
$response = JobServiceConnector::get_job_service()->jobs->searchForAlert($request);
62+
var_export($response);
63+
return $response;
64+
}
65+
66+
# [END search_for_alerts]
67+
68+
protected function configure()
69+
{
70+
$this
71+
->setName('email-alert-search')
72+
->setDescription('Run email alert search sample script.');
73+
}
74+
75+
protected function execute(InputInterface $input, OutputInterface $output)
76+
{
77+
$companyToBeCreated = BasicCompanySample::generate_company();
78+
$companyName = BasicCompanySample::create_company($companyToBeCreated)->getName();
79+
80+
$jobToBeCreated = BasicJobSample::generate_job_with_required_fields($companyName);
81+
$jobName = BasicJobSample::create_job($jobToBeCreated)->getName();
82+
83+
// Wait several seconds for post processing.
84+
sleep(10);
85+
self::search_for_alerts($companyName);
86+
87+
BasicJobSample::delete_job($jobName);
88+
BasicCompanySample::delete_company($companyName);
89+
}
90+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Jobs;
19+
20+
21+
use Symfony\Component\Console\Tester\CommandTester;
22+
23+
class EmailAlertSearchSampleTest extends \PHPUnit_Framework_TestCase
24+
{
25+
private $commandTester;
26+
27+
public function setUp()
28+
{
29+
if (!getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
30+
return $this->markTestSkipped("Set the GOOGLE_APPLICATION_CREDENTIALS environment variable.");
31+
}
32+
33+
$application = require __DIR__ . '/../cjd_sample.php';
34+
$this->commandTester = new CommandTester($application->get('email-alert-search'));
35+
}
36+
37+
public function testFeaturedJobsSearchSample()
38+
{
39+
$this->commandTester->execute([], ['interactive' => false]);
40+
$this->expectOutputRegex('/matchingJobs/');
41+
}
42+
}

0 commit comments

Comments
 (0)