1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ApiDefinitionTest |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\ProxyBundle\Tests\Definition; |
7
|
|
|
|
8
|
|
|
use Graviton\ProxyBundle\Definition\ApiDefinition; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* test api definition |
12
|
|
|
* |
13
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
14
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
15
|
|
|
* @link http://swisscom.ch |
16
|
|
|
*/ |
17
|
|
|
class ApiDefinitionTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* test add endpoint |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function testAddEndpoints() |
25
|
|
|
{ |
26
|
|
|
$host = "localhost:8000"; |
27
|
|
|
$basePath = "/v2/default/path"; |
28
|
|
|
$endpoints = ["/user", "/group", "/user/uploadImage"]; |
29
|
|
|
|
30
|
|
|
$sut = new ApiDefinition(); |
31
|
|
|
$sut->setBasePath($basePath); |
32
|
|
|
$sut->setHost($host); |
33
|
|
|
foreach ($endpoints as $endpoint) { |
34
|
|
|
$sut->addEndpoint($endpoint); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$apiEndpoints = $sut->getEndpoints(false, null); |
38
|
|
|
$apiEndpointsWithoutBase = $sut->getEndpoints(false, null, '', false); |
39
|
|
|
|
40
|
|
|
$this->assertEquals($host, $sut->getHost()); |
41
|
|
|
$this->assertCount(3, $apiEndpoints); |
42
|
|
|
foreach ($endpoints as $id => $endpoint) { |
43
|
|
|
$this->assertTrue($sut->hasEndpoint($endpoint)); |
44
|
|
|
$this->assertEquals($basePath.$endpoint, $apiEndpoints[$id]); |
45
|
|
|
$this->assertEquals($endpoint, $apiEndpointsWithoutBase[$id]); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* test endpoint definition without base path,but with host |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function testGetEntpointWithoutPath() |
55
|
|
|
{ |
56
|
|
|
$endpoint = "/this/is/an/endpoint"; |
57
|
|
|
$prefix = "/testapi"; |
58
|
|
|
$host = "blabla.talk:8080"; |
59
|
|
|
|
60
|
|
|
$sut = new ApiDefinition(); |
61
|
|
|
$sut->addEndpoint($endpoint); |
62
|
|
|
|
63
|
|
|
$this->assertEquals($endpoint, $sut->getEndpoints(false)[0]); |
64
|
|
|
$this->assertEquals($endpoint, $sut->getEndpoints(true)[0]); |
65
|
|
|
|
66
|
|
|
$sut->setHost($host); |
67
|
|
|
$this->assertEquals($endpoint, $sut->getEndpoints(false)[0]); |
68
|
|
|
$this->assertEquals($host.$endpoint, $sut->getEndpoints(true)[0]); |
69
|
|
|
$this->assertEquals($prefix.$endpoint, $sut->getEndpoints(false, $prefix)[0]); |
70
|
|
|
$this->assertEquals($host.$prefix.$endpoint, $sut->getEndpoints(true, $prefix)[0]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* test endpoint definition with a configured host |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function testGetEndPointsWithDefinedHost() |
79
|
|
|
{ |
80
|
|
|
$endpoint = "/this/is/an/endpoint"; |
81
|
|
|
$host = "blabla.talk:8080"; |
82
|
|
|
$preferedHost = "someHost.talk:8000"; |
83
|
|
|
|
84
|
|
|
$sut = new ApiDefinition(); |
85
|
|
|
$sut->addEndpoint($endpoint); |
86
|
|
|
$sut->setHost($host); |
87
|
|
|
|
88
|
|
|
$this->assertEquals($preferedHost.$endpoint, $sut->getEndpoints(true, null, $preferedHost)[0]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* test schema |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function testAddSchema() |
97
|
|
|
{ |
98
|
|
|
$endpoint = "test/schema/endpoint"; |
99
|
|
|
$testschema = new \stdClass(); |
100
|
|
|
$testschema->name = "test123"; |
101
|
|
|
$testschema->description = "This is a description"; |
102
|
|
|
|
103
|
|
|
$sut = new ApiDefinition(); |
104
|
|
|
$sut->addSchema($endpoint, $testschema); |
105
|
|
|
$schema = $sut->getSchema($endpoint); |
106
|
|
|
|
107
|
|
|
$this->assertInstanceOf("\stdClass", $schema); |
108
|
|
|
$this->assertEquals($testschema->name, $schema->name); |
109
|
|
|
$this->assertEquals($testschema->description, $schema->description); |
110
|
|
|
$this->assertInstanceOf("\stdClass", $sut->getSchema("blablabla/endpoint")); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|