|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ComradeReader\Test\Service; |
|
4
|
|
|
use ComradeReader\Service\ComradeReader; |
|
5
|
|
|
use ComradeReader\Test\Helpers\SimpleTestEntity; |
|
6
|
|
|
use Doctrine\Common\Cache\VoidCache; |
|
7
|
|
|
use Symfony\Component\Serializer\Encoder\JsonDecode; |
|
8
|
|
|
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; |
|
9
|
|
|
use Symfony\Component\Serializer\Serializer; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @package ComradeReader\Service |
|
13
|
|
|
*/ |
|
14
|
|
|
class ComradeReaderTest extends \PHPUnit_Framework_TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @return ComradeReader |
|
18
|
|
|
*/ |
|
19
|
|
|
private function getReader() |
|
20
|
|
|
{ |
|
21
|
|
|
$serializer = new Serializer( |
|
22
|
|
|
[new PropertyNormalizer()], [new JsonDecode()] |
|
23
|
|
|
); |
|
24
|
|
|
|
|
25
|
|
|
return new ComradeReader('http://localhost:8005', 'test', $serializer, new VoidCache()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Basic test for valid request |
|
30
|
|
|
* |
|
31
|
|
|
* @see examples/test-api-responses/colors.php |
|
32
|
|
|
*/ |
|
33
|
|
|
public function testGet() |
|
34
|
|
|
{ |
|
35
|
|
|
/** @var SimpleTestEntity $color */ |
|
36
|
|
|
$color = $this->getReader()->get('/colors.php')->decode(SimpleTestEntity::class); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertSame('red', $color->getColorName()); |
|
39
|
|
|
$this->assertSame(1, $color->getId()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Test passing POST parameters |
|
44
|
|
|
*/ |
|
45
|
|
|
public function testPassingPostAndGetParameters() |
|
46
|
|
|
{ |
|
47
|
|
|
$reader = $this->getReader(); |
|
48
|
|
|
$reader->setTokenFieldName('custom_token_field_name'); |
|
49
|
|
|
|
|
50
|
|
|
$response = $reader->post( |
|
51
|
|
|
'/post-parameters.php?this_is_a_get_parameter=true', ['integer' => 1], 500 |
|
52
|
|
|
)->decodeIntoArray(); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertSame('true', $response['GET']['this_is_a_get_parameter']); |
|
55
|
|
|
$this->assertSame('test', $response['GET']['custom_token_field_name']); |
|
56
|
|
|
$this->assertSame('1', $response['POST']['integer']); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @expectedException \ComradeReader\Exception\ResourceNotFoundException |
|
61
|
|
|
* @expectedExceptionMessage Resource "/this-should-not-be-found" was not found on remote server, got a HTTP 404 error while trying to get the resource. Please verify if the resource exists, and if the HTTP method is correct |
|
62
|
|
|
*/ |
|
63
|
|
|
public function testNotFound() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->getReader()->request('GET', '/this-should-not-be-found') |
|
66
|
|
|
->decode(SimpleTestEntity::class); |
|
67
|
|
|
} |
|
68
|
|
|
} |