Completed
Pull Request — develop (#368)
by Adrian
13:00 queued 06:33
created

ApiDefinitionTest::testAddEndpoints()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 17
nc 4
nop 0
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