Completed
Push — feature/EVO-7278-security-and-... ( 19a53c...ae4d63 )
by
unknown
17:24 queued 11:57
created

RestTestCase::createRestClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 2
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  http://opensource.org/licenses/gpl-license.php GNU Public License
20
 * @link     http://swisscom.ch
21
 */
22
class RestTestCase extends GravitonTestCase
23
{
24
    /**
25
     * Create a REST Client.
26
     *
27
     * Creates a regular client first so we can profit from the bootstrapping code
28
     * in parent::createClient and is otherwise API compatible with said method.
29
     *
30
     * @param array $options An array of options to pass to the createKernel class
31
     * @param array $server  An array of server parameters
32
     *
33
     * @return \Graviton\TestBundle\Client A Client instance
34
     */
35
    protected static function createRestClient(array $options = array(), array $server = array())
36
    {
37
        parent::createClient($options, $server);
38
39
        $client = static::$kernel->getContainer()->get('graviton.test.rest.client');
40
        $client->setServerParameters($server);
41
42
        return $client;
43
    }
44
45
    /**
46
     * load fixtures
47
     *
48
     * @return void
49
     */
50
    public function setUp()
51
    {
52
        $this->loadFixtures(
53
            array(
54
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData',
55
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadTranslatableData'
56
            ),
57
            null,
58
            'doctrine_mongodb'
59
        );
60
    }
61
62
    /**
63
     * test for content type based on classname based mapping
64
     *
65
     * @param string   $contentType Expected Content-Type
66
     * @param Response $response    Response from client
67
     *
68
     * @return void
69
     */
70
    public function assertResponseContentType($contentType, Response $response)
71
    {
72
        $this->assertEquals(
73
            $contentType,
74
            $response->headers->get('Content-Type'),
75
            'Content-Type mismatch in response'
76
        );
77
    }
78
79
    /**
80
     * assertion for checking cors headers
81
     *
82
     * @param string $methods  methods to check for
83
     * @param object $response response to load headers from
84
     *
85
     * @return void
86
     */
87
    public function assertCorsHeaders($methods, $response)
88
    {
89
        $this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin'));
90
        $this->assertEquals($methods, $response->headers->get('Access-Control-Allow-Methods'));
91
    }
92
93
    /**
94
     * assert that putting a fetched resource fails
95
     *
96
     * @param string $url    url
97
     * @param Client $client client to use
98
     *
99
     * @return void
100
     */
101
    public function assertPutFails($url, $client)
102
    {
103
        $client->request('GET', $url);
104
        $client->put($url, $client->getResults());
105
106
        $response = $client->getResponse();
107
        $this->assertEquals(405, $response->getStatusCode());
108
        $this->assertEquals('GET, HEAD, OPTIONS', $response->headers->get('Allow'));
109
    }
110
111
    /**
112
     * Testing private methods for a class.
113
     *
114
     * $class = new YourClass(); or service...
115
     * $method = $this->getPrivateClassMethod(get_class($class), 'getPrivateFunction');
116
     * $result = $method->invokeArgs( $this->activityManager, [argument1, argument2, ...]);
117
     *
118
     * @param string $className  String name for class, full namespace.
119
     * @param string $methodName Method name to be used
120
     * @return \ReflectionMethod
121
     */
122 2
    public function getPrivateClassMethod($className, $methodName)
123
    {
124 2
        $reflector = new \ReflectionClass($className);
125 2
        $method = $reflector->getMethod($methodName);
126 2
        $method->setAccessible(true);
127 2
        return $method;
128
    }
129
}
130