Skip to content

Commit 820c4ad

Browse files
committed
everything besides Assertions
1 parent 59da2a4 commit 820c4ad

20 files changed

+341
-153
lines changed

Factory/RequestFactory.php

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,43 @@
1818

1919
namespace Circle\DoctrineRestDriver\Factory;
2020

21-
use Circle\DoctrineRestDriver\Annotations\RoutingTable;
22-
use Circle\DoctrineRestDriver\Enums\HttpMethods;
23-
use Circle\DoctrineRestDriver\Types\Annotation;
21+
use Circle\DoctrineRestDriver\Annotations\DataSource;
2422
use Circle\DoctrineRestDriver\Types\HttpHeader;
2523
use Circle\DoctrineRestDriver\Types\CurlOptions;
2624
use Circle\DoctrineRestDriver\Types\HttpMethod;
2725
use Circle\DoctrineRestDriver\Types\Payload;
2826
use Circle\DoctrineRestDriver\Types\HttpQuery;
2927
use Circle\DoctrineRestDriver\Types\Request;
30-
use Circle\DoctrineRestDriver\Types\SqlOperation;
3128
use Circle\DoctrineRestDriver\Types\StatusCode;
32-
use Circle\DoctrineRestDriver\Types\Table;
3329
use Circle\DoctrineRestDriver\Types\Url;
3430

3531
/**
3632
* Factory for requests
3733
*
3834
* @author Tobias Hauck <tobias@circle.ai>
3935
* @copyright 2015 TeeAge-Beatz UG
40-
*
41-
* @SuppressWarnings("PHPMD.CouplingBetweenObjects")
4236
*/
4337
class RequestFactory {
4438

4539
/**
4640
* Creates a new Request with the given options
4741
*
48-
* @param array $tokens
49-
* @param string $apiUrl
50-
* @param array $options
51-
* @param RoutingTable $routings
42+
* @param string $method
43+
* @param array $tokens
44+
* @param array $options
45+
* @param DataSource $annotation
5246
* @return Request
5347
*
5448
* @SuppressWarnings("PHPMD.StaticAccess")
5549
*/
56-
public function createOne(array $tokens, $apiUrl, array $options, RoutingTable $routings = null) {
57-
$operation = SqlOperation::create($tokens);
58-
$annotation = Annotation::get($routings, Table::create($tokens), HttpMethods::ofSqlOperation($operation));
59-
60-
$method = HttpMethod::create($operation, $annotation);
61-
$url = Url::createFromTokens($tokens, $apiUrl, $annotation);
62-
$options = CurlOptions::create(array_merge($options, HttpHeader::create($options, $tokens)));
63-
$query = HttpQuery::create($tokens);
64-
$payload = Payload::create($tokens, $options);
65-
$statusCode = StatusCode::create($operation, $annotation);
66-
67-
return new Request($method, $url, $options, $query, $payload, $statusCode);
50+
public function createOne($method, array $tokens, array $options, DataSource $annotation = null) {
51+
return new Request([
52+
'method' => HttpMethod::create($method, $annotation),
53+
'url' => Url::createFromTokens($tokens, $options['host'], $annotation),
54+
'curlOptions' => CurlOptions::create(array_merge($options['driverOptions'], HttpHeader::create($options['driverOptions'], $tokens))),
55+
'query' => HttpQuery::create($tokens),
56+
'payload' => Payload::create($tokens, $options),
57+
'expectedStatusCode' => StatusCode::create($method, $annotation)
58+
]);
6859
}
6960
}

MetaData.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
namespace Circle\DoctrineRestDriver;
2020

21-
use Circle\DoctrineRestDriver\Annotations\Loader;
22-
use Doctrine\Common\Annotations\AnnotationRegistry;
2321
use Doctrine\Common\Persistence\ObjectManager;
2422

2523
/**

RestClient.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* This file is part of DoctrineRestDriver.
4+
*
5+
* DoctrineRestDriver is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* DoctrineRestDriver is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with DoctrineRestDriver. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
namespace Circle\DoctrineRestDriver;
20+
21+
use Circle\DoctrineRestDriver\Enums\HttpMethods;
22+
use Circle\DoctrineRestDriver\Exceptions\Exceptions;
23+
use Circle\DoctrineRestDriver\Factory\RestClientFactory;
24+
use Circle\DoctrineRestDriver\Types\Request;
25+
use Symfony\Component\HttpFoundation\Response;
26+
use Circle\RestClientBundle\Services\RestClient as CiRestClient;
27+
use Circle\DoctrineRestDriver\Exceptions\RequestFailedException;
28+
29+
/**
30+
* Rest client to send requests and map responses
31+
*
32+
* @author Tobias Hauck <tobias@circle.ai>
33+
* @copyright 2015 TeeAge-Beatz UG
34+
*/
35+
class RestClient {
36+
37+
/**
38+
* @var CiRestClient
39+
*/
40+
private $restClient;
41+
42+
/**
43+
* RestClient constructor
44+
*/
45+
public function __construct() {
46+
$this->restClient = (new RestClientFactory())->createOne([]);
47+
}
48+
49+
/**
50+
* sends the request
51+
*
52+
* @param Request $request
53+
* @return Response
54+
* @throws RequestFailedException
55+
*
56+
* @SuppressWarnings("PHPMD.StaticAccess")
57+
*/
58+
public function send(Request $request) {
59+
$method = strtolower($request->getMethod());
60+
$response = $method === HttpMethods::GET || $method === HttpMethods::DELETE ? $this->restClient->$method($request->getUrlAndQuery(), $request->getCurlOptions()) : $this->restClient->$method($request->getUrlAndQuery(), $request->getPayload(), $request->getCurlOptions());
61+
62+
return $response->getStatusCode() === $request->getExpectedStatusCode() ? $response : Exceptions::RequestFailedException($request, $response->getStatusCode(), $response->getContent());
63+
}
64+
}

Security/HttpAuthentication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ public function transformRequest(Request $request) {
5252
array_push($headers, 'Authorization: Basic ' . base64_encode($this->config['user'] . ':' . $this->config['password']));
5353
$options[CURLOPT_HTTPHEADER] = $headers;
5454

55-
return new Request($request->getMethod(), $request->getUrl(), $options, $request->getQuery(), $request->getPayload(), $request->getExpectedStatusCode());
55+
return $request->setCurlOptions($options);
5656
}
5757
}

Statement.php

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Circle\DoctrineRestDriver\Annotations\RoutingTable;
2222
use Circle\DoctrineRestDriver\Enums\HttpMethods;
2323
use Circle\DoctrineRestDriver\Exceptions\Exceptions;
24-
use Circle\DoctrineRestDriver\Factory\RestClientFactory;
2524
use Circle\DoctrineRestDriver\Security\AuthStrategy;
2625
use Circle\DoctrineRestDriver\Transformers\MysqlToRequest;
2726
use Circle\DoctrineRestDriver\Types\Authentication;
@@ -40,7 +39,6 @@
4039
* @copyright 2015 TeeAge-Beatz UG
4140
*
4241
* @SuppressWarnings("PHPMD.TooManyPublicMethods")
43-
* @SuppressWarnings("PHPMD.CouplingBetweenObjects")
4442
*/
4543
class Statement implements \IteratorAggregate, StatementInterface {
4644

@@ -60,9 +58,9 @@ class Statement implements \IteratorAggregate, StatementInterface {
6058
private $params = [];
6159

6260
/**
63-
* @var \Circle\DoctrineRestDriver\Factory\RestClientFactory
61+
* @var RestClient
6462
*/
65-
private $restClientFactory;
63+
private $restClient;
6664

6765
/**
6866
* @var array
@@ -118,7 +116,7 @@ public function __construct($query, array $options, RoutingTable $routings) {
118116
$this->query = $query;
119117
$this->routings = $routings;
120118
$this->mysqlToRequest = new MysqlToRequest($options, $this->routings);
121-
$this->restClientFactory = new RestClientFactory();
119+
$this->restClient = new RestClient();
122120

123121
$this->authStrategy = Authentication::create($options);
124122
$this->formatter = Format::create($options);
@@ -157,17 +155,20 @@ public function errorInfo() {
157155

158156
/**
159157
* {@inheritdoc}
158+
*
159+
* @SuppressWarnings("PHPMD.StaticAccess")
160160
*/
161161
public function execute($params = null) {
162162
$rawRequest = $this->mysqlToRequest->transform($this->query, $this->params);
163163
$request = $this->authStrategy->transformRequest($rawRequest);
164-
$restClient = $this->restClientFactory->createOne($request->getCurlOptions());
165164

166-
$method = strtolower($request->getMethod());
167-
$response = $method === HttpMethods::GET || $method === HttpMethods::DELETE ? $restClient->$method($request->getUrlAndQuery()) : $restClient->$method($request->getUrlAndQuery(), $request->getPayload());
168-
$statusCode = $response->getStatusCode();
165+
$response = $this->restClient->send($request);
169166

170-
return $statusCode === $request->getExpectedStatusCode() ? $this->onSuccess($response, $method) : $this->onError($request, $response);
167+
$this->result = Result::create($this->query, $this->formatter->decode($response->getContent()));
168+
$this->id = !empty($this->result['id']) ? $this->result['id'] : null;
169+
krsort($this->result);
170+
171+
return true;
171172
}
172173

173174
/**
@@ -248,37 +249,4 @@ public function getIterator() {
248249
public function getId() {
249250
return $this->id;
250251
}
251-
252-
/**
253-
* Handles the statement if the execution succeeded
254-
*
255-
* @param Response $response
256-
* @param string $method
257-
* @return bool
258-
*
259-
* @SuppressWarnings("PHPMD.StaticAccess")
260-
*/
261-
private function onSuccess(Response $response, $method) {
262-
$this->result = Result::create($this->query, $this->formatter->decode($response->getContent()));
263-
$this->id = $method === HttpMethods::POST ? $this->result['id'] : null;
264-
krsort($this->result);
265-
266-
return true;
267-
}
268-
269-
/**
270-
* Handles the statement if the execution failed
271-
*
272-
* @param Request $request
273-
* @param Response $response
274-
* @throws \Exception
275-
*
276-
* @SuppressWarnings("PHPMD.StaticAccess")
277-
*/
278-
private function onError(Request $request, Response $response) {
279-
$this->errorCode = $response->getStatusCode();
280-
$this->errorMessage = $response->getContent();
281-
282-
return Exceptions::RequestFailedException($request, $response->getStatusCode(), $response->getContent());
283-
}
284252
}

Tests/Exceptions/ExceptionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function methodNotImplementedExceptionTest() {
8989
* @SuppressWarnings("PHPMD.StaticAccess")
9090
*/
9191
public function requestFailedExceptionTest() {
92-
Exceptions::requestFailedException(new Request('get', 'url', []), 1, 'errorMessage');
92+
Exceptions::requestFailedException(new Request(['method' => 'get', 'url' => 'url']), 1, 'errorMessage');
9393
}
9494

9595
/**

Tests/Factory/RequestFactoryTest.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@ class RequestFactoryTest extends \PHPUnit_Framework_TestCase {
4949
* @var array
5050
*/
5151
private $factoryOptions = [
52-
'CURLOPT_HTTPHEADER' => 'Content-Type: application/json',
53-
'CURLOPT_MAXREDIRS' => 25,
54-
'CURLOPT_TIMEOUT' => 25,
55-
'CURLOPT_CONNECTTIMEOUT' => 25,
56-
'CURLOPT_CRLF' => true,
57-
'CURLOPT_SSLVERSION' => 3,
58-
'CURLOPT_FOLLOWLOCATION' => true,
52+
'host' => 'http://circle.ai',
53+
'driverOptions' => [
54+
'CURLOPT_HTTPHEADER' => 'Content-Type: application/json',
55+
'CURLOPT_MAXREDIRS' => 25,
56+
'CURLOPT_TIMEOUT' => 25,
57+
'CURLOPT_CONNECTTIMEOUT' => 25,
58+
'CURLOPT_CRLF' => true,
59+
'CURLOPT_SSLVERSION' => 3,
60+
'CURLOPT_FOLLOWLOCATION' => true,
61+
]
5962
];
6063

6164
/**
@@ -67,14 +70,14 @@ public function createOne() {
6770
$query = 'SELECT name FROM products WHERE id=1';
6871
$parser = new PHPSQLParser();
6972
$factory = new RequestFactory();
70-
$expected = new Request('get', 'http://circle.ai/products/1', $this->requestOptions);
73+
$expected = new Request([
74+
'method' => 'get',
75+
'url' => 'http://circle.ai/products/1',
76+
'curlOptions' => $this->requestOptions
77+
]);
7178

72-
$routings = $this->getMockBuilder('Circle\DoctrineRestDriver\Annotations\RoutingTable')->disableOriginalConstructor()->getMock();
73-
$routings
74-
->expects($this->any())
75-
->method('get')
76-
->will($this->returnValue(null));
79+
$routings = $this->getMockBuilder('Circle\DoctrineRestDriver\Annotations\DataSource')->getMock();
7780

78-
$this->assertEquals($expected, $factory->createOne($parser->parse($query), 'http://circle.ai', $this->factoryOptions, $routings));
81+
$this->assertEquals($expected, $factory->createOne('get', $parser->parse($query), $this->factoryOptions, $routings));
7982
}
8083
}

Tests/RestClientTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* This file is part of DoctrineRestDriver.
4+
*
5+
* DoctrineRestDriver is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* DoctrineRestDriver is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with DoctrineRestDriver. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
namespace Circle\DoctrineRestDriver\Tests;
20+
21+
use Circle\DoctrineRestDriver\Enums\HttpMethods;
22+
use Circle\DoctrineRestDriver\RestClient;
23+
use Circle\DoctrineRestDriver\Types\Request;
24+
use Symfony\Component\HttpFoundation\Response;
25+
26+
/**
27+
* Tests the rest client
28+
*
29+
* @author Tobias Hauck <tobias@circle.ai>
30+
* @copyright 2015 TeeAge-Beatz UG
31+
*
32+
* @coversDefaultClass Circle\DoctrineRestDriver\RestClient
33+
*/
34+
class RestClientTest extends \PHPUnit_Framework_TestCase {
35+
36+
/**
37+
* @var RestClient
38+
*/
39+
private $restClient;
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function setUp() {
45+
$this->restClient = new RestClient();
46+
}
47+
48+
/**
49+
* @test
50+
* @group unit
51+
* @covers ::__construct
52+
* @covers ::send
53+
*/
54+
public function send() {
55+
$request = new Request([
56+
'method' => HttpMethods::GET,
57+
'url' => 'http://127.0.0.1:3000/app_dev.php/mockapi/products/1'
58+
]);
59+
60+
$response = new Response(json_encode([
61+
'id' => 1,
62+
'name' => 'MyName',
63+
'value' => 'MyValue'
64+
]));
65+
66+
$this->assertEquals($response->getContent(), $this->restClient->send($request)->getContent());
67+
}
68+
}

Tests/Security/HttpAuthenticationTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,16 @@ public function transformRequest() {
6262
]
6363
];
6464

65-
$request = new Request(HttpMethods::GET, 'http://circle.ai', []);
66-
$expected = new Request(HttpMethods::GET, 'http://circle.ai', $expectedOptions);
65+
$request = new Request([
66+
'method' => HttpMethods::GET,
67+
'url' => 'http://circle.ai'
68+
]);
69+
70+
$expected = new Request([
71+
'method' => HttpMethods::GET,
72+
'url' => 'http://circle.ai',
73+
'curlOptions' => $expectedOptions
74+
]);
6775

6876
$this->assertEquals($expected, $this->authentication->transformRequest($request));
6977
}

0 commit comments

Comments
 (0)