Completed
Push — feature/testing-cleanup ( 4dd1da )
by Narcotic
63:55
created

RestTestCase::getPrivateClassMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * REST test case
4
 *
5
 * Contains additional helpers for testing RESTful servers
6
 */
7
8
namespace Graviton\TestBundle\Test;
9
10
use Graviton\TestBundle\Client;
11
use Symfony\Component\HttpFoundation\Response;
12
13
/**
14
 * REST test case
15
 *
16
 * Contains additional helpers for testing RESTful servers
17
 *
18
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
19
 * @license  https://opensource.org/licenses/MIT MIT License
20
 * @link     http://swisscom.ch
21
 */
22
class RestTestCase extends GravitonTestCase
23
{
24
25
    /**
26
     * Create a REST Client.
27
     *
28
     * Creates a regular client first so we can profit from the bootstrapping code
29
     * in parent::createClient and is otherwise API compatible with said method.
30
     *
31
     * @param array $options An array of options to pass to the createKernel class
32
     * @param array $server  An array of server parameters
33
     *
34
     * @return \Graviton\TestBundle\Client A Client instance
35
     */
36
    protected static function createRestClient(array $options = [], array $server = array())
37
    {
38
        parent::createClient($options, $server);
39
40
        $client = static::$kernel->getContainer()->get('graviton.test.rest.client');
41
        $client->setServerParameters($server);
42
43
        return $client;
44
    }
45
46
    /**
47
     * load fixtures
48
     *
49
     * @return void
50
     */
51
    public function setUp()
52
    {
53
        $this->loadFixtures(
54
            array(
55
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData',
56
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadTranslatableData'
57
            ),
58
            null,
59
            'doctrine_mongodb'
60
        );
61
    }
62
63
    /**
64
     * test for content type based on classname based mapping
65
     *
66
     * @param string   $contentType Expected Content-Type
67
     * @param Response $response    Response from client
68
     *
69
     * @return void
70
     */
71
    public function assertResponseContentType($contentType, Response $response)
72
    {
73
        $this->assertEquals(
74
            $contentType,
75
            $response->headers->get('Content-Type'),
76
            'Content-Type mismatch in response'
77
        );
78
    }
79
80
    /**
81
     * assertion for checking cors headers
82
     *
83
     * @param string $methods  methods to check for
84
     * @param object $response response to load headers from
85
     *
86
     * @return void
87
     */
88
    public function assertCorsHeaders($methods, $response)
89
    {
90
        $this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin'));
91
        $this->assertEquals($methods, $response->headers->get('Access-Control-Allow-Methods'));
92
    }
93
94
    /**
95
     * assert that putting a fetched resource fails
96
     *
97
     * @param string $url    url
98
     * @param Client $client client to use
99
     *
100
     * @return void
101
     */
102
    public function assertPutFails($url, $client)
103
    {
104
        $client->request('GET', $url);
105
        $client->put($url, $client->getResults());
106
107
        $response = $client->getResponse();
108
        $this->assertEquals(405, $response->getStatusCode());
109
        $this->assertEquals('GET, HEAD, OPTIONS', $response->headers->get('Allow'));
110
    }
111
}
112