Skip to content

Commit b1badec

Browse files
author
Tom Walder
committed
Initial test framework for REST Gateway
1 parent e61f795 commit b1badec

File tree

3 files changed

+197
-1
lines changed

3 files changed

+197
-1
lines changed

tests/RESTv1GatewayTest.php

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Tom Walder
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+
/**
19+
* Tests for REST API v1 Gateway
20+
*
21+
* @author Tom Walder <[email protected]>
22+
*/
23+
class RESTv1GatewayTest extends \PHPUnit_Framework_TestCase
24+
{
25+
26+
private $str_expected_url;
27+
private $arr_expected_payload;
28+
29+
private function initTestHttpClient($str_expected_url, $arr_expected_payload)
30+
{
31+
$this->str_expected_url = $str_expected_url;
32+
$this->arr_expected_payload = $arr_expected_payload;
33+
return new FakeGuzzleClient();
34+
}
35+
36+
private function initTestGateway()
37+
{
38+
return $this->getMockBuilder('\\GDS\\Gateway\\RESTv1')->setMethods(['initHttpClient'])->setConstructorArgs(['DatasetTest'])->getMock();
39+
}
40+
41+
/**
42+
* @todo Validate the payloads
43+
*
44+
* @param FakeGuzzleClient $obj_http
45+
*/
46+
private function validateHttpClient(\FakeGuzzleClient $obj_http)
47+
{
48+
$this->assertEquals($this->str_expected_url, $obj_http->getPostedUrl());
49+
// $this->assertEquals($this->arr_expected_payload, $obj_http->getPostedParams());
50+
}
51+
52+
public function testTransaction()
53+
{
54+
$obj_http = $this->initTestHttpClient('https://datastore.googleapis.com/v1/projects/DatasetTest:beginTransaction', []);
55+
$obj_gateway = $this->initTestGateway()->setHttpClient($obj_http);
56+
57+
$obj_gateway->beginTransaction();
58+
59+
$this->validateHttpClient($obj_http);
60+
}
61+
62+
63+
public function testDelete()
64+
{
65+
$obj_http = $this->initTestHttpClient('https://datastore.googleapis.com/v1/projects/DatasetTest:commit', ['json' => (object)[]]);
66+
$obj_gateway = $this->initTestGateway()->setHttpClient($obj_http);
67+
68+
$obj_store = new \GDS\Store('Test', $obj_gateway);
69+
70+
$obj_entity = (new GDS\Entity())->setKeyId('123456789');
71+
$obj_store->delete([$obj_entity]);
72+
73+
$this->validateHttpClient($obj_http);
74+
}
75+
76+
77+
}

tests/base/FakeGuzzleClient.php

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
class FakeGuzzleClient implements \GuzzleHttp\ClientInterface
4+
{
5+
6+
private $str_url;
7+
8+
private $arr_params;
9+
10+
/**
11+
* Pretend to do a POST request
12+
*
13+
* @param $str_url
14+
* @param array $arr_params
15+
* @return \GuzzleHttp\Psr7\Response
16+
*/
17+
public function post($str_url, array $arr_params)
18+
{
19+
// echo $str_url, '::', print_r($arr_params, true), PHP_EOL;
20+
$this->str_url = $str_url;
21+
$this->arr_params = $arr_params;
22+
return new \GuzzleHttp\Psr7\Response();
23+
}
24+
25+
public function getPostedUrl()
26+
{
27+
return $this->str_url;
28+
}
29+
30+
public function getPostedParams()
31+
{
32+
return $this->arr_params;
33+
}
34+
35+
/**
36+
* Send an HTTP request.
37+
*
38+
* @param \Psr\Http\Message\RequestInterface $request Request to send
39+
* @param array $options Request options to apply to the given
40+
* request and to the transfer.
41+
*
42+
* @return \Psr\Http\Message\ResponseInterface
43+
* @throws \GuzzleHttp\Exception\GuzzleException
44+
*/
45+
public function send(\Psr\Http\Message\RequestInterface $request, array $options = [])
46+
{
47+
// TODO: Implement send() method.
48+
}
49+
50+
/**
51+
* Asynchronously send an HTTP request.
52+
*
53+
* @param \Psr\Http\Message\RequestInterface $request Request to send
54+
* @param array $options Request options to apply to the given
55+
* request and to the transfer.
56+
*
57+
* @return \GuzzleHttp\Promise\PromiseInterface
58+
*/
59+
public function sendAsync(\Psr\Http\Message\RequestInterface $request, array $options = [])
60+
{
61+
// TODO: Implement sendAsync() method.
62+
}
63+
64+
/**
65+
* Create and send an HTTP request.
66+
*
67+
* Use an absolute path to override the base path of the client, or a
68+
* relative path to append to the base path of the client. The URL can
69+
* contain the query string as well.
70+
*
71+
* @param string $method HTTP method.
72+
* @param string|\Psr\Http\Message\UriInterface $uri URI object or string.
73+
* @param array $options Request options to apply.
74+
*
75+
* @return \Psr\Http\Message\ResponseInterface
76+
* @throws \GuzzleHttp\Exception\GuzzleException
77+
*/
78+
public function request($method, $uri, array $options = [])
79+
{
80+
// TODO: Implement request() method.
81+
}
82+
83+
/**
84+
* Create and send an asynchronous HTTP request.
85+
*
86+
* Use an absolute path to override the base path of the client, or a
87+
* relative path to append to the base path of the client. The URL can
88+
* contain the query string as well. Use an array to provide a URL
89+
* template and additional variables to use in the URL template expansion.
90+
*
91+
* @param string $method HTTP method
92+
* @param string|\Psr\Http\Message\UriInterface $uri URI object or string.
93+
* @param array $options Request options to apply.
94+
*
95+
* @return \GuzzleHttp\Promise\PromiseInterface
96+
*/
97+
public function requestAsync($method, $uri, array $options = [])
98+
{
99+
// TODO: Implement requestAsync() method.
100+
}
101+
102+
/**
103+
* Get a client configuration option.
104+
*
105+
* These options include default request options of the client, a "handler"
106+
* (if utilized by the concrete client), and a "base_uri" if utilized by
107+
* the concrete client.
108+
*
109+
* @param string|null $option The config option to retrieve.
110+
*
111+
* @return mixed
112+
*/
113+
public function getConfig($option = null)
114+
{
115+
// TODO: Implement getConfig() method.
116+
}
117+
118+
}

tests/bootstrap.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
require_once(dirname(__FILE__) . '/base/GDSTest.php');
1616
require_once(dirname(__FILE__) . '/base/Simple.php');
1717
require_once(dirname(__FILE__) . '/base/Book.php');
18-
require_once(dirname(__FILE__) . '/base/DenyGQLProxyMock.php');
18+
require_once(dirname(__FILE__) . '/base/DenyGQLProxyMock.php');
19+
require_once(dirname(__FILE__) . '/base/FakeGuzzleClient.php');

0 commit comments

Comments
 (0)