Completed
Push — develop ( e2b887...c09155 )
by
unknown
12:13
created

AbstractControllerTestCase   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8
Metric Value
wmc 19
lcom 2
cbo 8
dl 0
loc 197
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 19 1
A assertResponseStatusCode() 0 6 1
A getResponseHeader() 0 8 1
A assertRedirect() 0 10 2
A assertNotRedirect() 0 13 2
A assertRedirectTo() 0 19 3
A assertNotRedirectTo() 0 18 3
A assertRedirectRegex() 0 19 3
A assertNotRedirectRegex() 0 18 3
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de)
7
 * @license       MIT
8
 */
9
10
namespace CoreTest\Controller;
11
12
use Test\Bootstrap;
13
use Zend\Http\Response;
14
use Zend\Mvc\Controller\AbstractActionController;
15
use Zend\Mvc\MvcEvent;
16
use Zend\Mvc\Router\RouteMatch;
17
use Zend\Mvc\Router\SimpleRouteStack;
18
use Zend\Mvc\Service\RouterFactory;
19
use PHPUnit_Framework_ExpectationFailedException;
20
21
abstract class AbstractControllerTestCase extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @var RouteMatch
25
     */
26
    protected $routeMatch;
27
28
    /**
29
     * @var MvcEvent
30
     */
31
    protected $event;
32
33
    /**
34
     * @var AbstractActionController
35
     */
36
    protected $controller;
37
38
    public function init($controllerName, $actionName = 'index', $lang = 'en')
39
    {
40
        $this->routeMatch = new RouteMatch(
41
            array(
42
                'controller' => $controllerName,
43
                'action' => $actionName,
44
                'lang' => $lang
45
            )
46
        );
47
        $this->event = new MvcEvent();
48
        $this->event->setRouteMatch($this->routeMatch);
49
50
        /** @var SimpleRouteStack $router */
51
        $routerFactory = new RouterFactory();
52
        $router        = $routerFactory->createService(clone Bootstrap::getServiceManager());
53
        $router->setDefaultParam('lang', $lang);
54
55
        $this->event->setRouter($router);
56
    }
57
58
    protected function assertResponseStatusCode($code)
59
    {
60
        /** @var Response $response */
61
        $response = $this->controller->getResponse();
62
        $this->assertSame($code, $response->getStatusCode());
63
    }
64
65
    /**
66
     * Get response header by key
67
     *
68
     * @param string $header
69
     *
70
     * @return \Zend\Http\Header\HeaderInterface|false
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|\Zend\Http\Heade...nterface|\ArrayIterator?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
71
     */
72
    protected function getResponseHeader($header)
73
    {
74
        /** @var Response $response */
75
        $response = $this->controller->getResponse();
76
        $headers = $response->getHeaders();
77
        $responseHeader = $headers->get($header, false);
0 ignored issues
show
Unused Code introduced by
The call to Headers::get() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
78
        return $responseHeader;
79
    }
80
81
    /**
82
     * Assert that response is a redirect
83
     */
84
    protected function assertRedirect()
85
    {
86
        $responseHeader = $this->getResponseHeader('Location');
87
        if (false === $responseHeader) {
88
            throw new PHPUnit_Framework_ExpectationFailedException(
89
                'Failed asserting response is a redirect'
90
            );
91
        }
92
        $this->assertNotEquals(false, $responseHeader);
93
    }
94
95
    /**
96
     * Assert that response is NOT a redirect
97
     */
98
    protected function assertNotRedirect()
99
    {
100
        $responseHeader = $this->getResponseHeader('Location');
101
        if (false !== $responseHeader) {
102
            throw new PHPUnit_Framework_ExpectationFailedException(
103
                sprintf(
104
                    'Failed asserting response is NOT a redirect, actual redirection is "%s"',
105
                    $responseHeader->getFieldValue()
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
106
                )
107
            );
108
        }
109
        $this->assertFalse($responseHeader);
110
    }
111
112
    /**
113
     * Assert that response redirects to given URL
114
     *
115
     * @param string $url
116
     *
117
     * @throws \PHPUnit_Framework_ExpectationFailedException
118
     */
119
    protected function assertRedirectTo($url)
120
    {
121
        $responseHeader = $this->getResponseHeader('Location');
122
        if (!$responseHeader) {
123
            throw new PHPUnit_Framework_ExpectationFailedException(
124
                'Failed asserting response is a redirect'
125
            );
126
        }
127
        if ($url != $responseHeader->getFieldValue()) {
128
            throw new PHPUnit_Framework_ExpectationFailedException(
129
                sprintf(
130
                    'Failed asserting response redirects to "%s", actual redirection is "%s"',
131
                    $url,
132
                    $responseHeader->getFieldValue()
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
133
                )
134
            );
135
        }
136
        $this->assertEquals($url, $responseHeader->getFieldValue());
137
    }
138
139
    /**
140
     * Assert that response does not redirect to given URL
141
     *
142
     * @param string $url
143
     *
144
     * @throws \PHPUnit_Framework_ExpectationFailedException
145
     */
146
    protected function assertNotRedirectTo($url)
147
    {
148
        $responseHeader = $this->getResponseHeader('Location');
149
        if (!$responseHeader) {
150
            throw new PHPUnit_Framework_ExpectationFailedException(
151
                'Failed asserting response is a redirect'
152
            );
153
        }
154
        if ($url == $responseHeader->getFieldValue()) {
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
155
            throw new PHPUnit_Framework_ExpectationFailedException(
156
                sprintf(
157
                    'Failed asserting response redirects to "%s"',
158
                    $url
159
                )
160
            );
161
        }
162
        $this->assertNotEquals($url, $responseHeader->getFieldValue());
163
    }
164
165
    /**
166
     * Assert that redirect location matches pattern
167
     *
168
     * @param string $pattern
169
     *
170
     * @throws \PHPUnit_Framework_ExpectationFailedException
171
     */
172
    protected function assertRedirectRegex($pattern)
173
    {
174
        $responseHeader = $this->getResponseHeader('Location');
175
        if (!$responseHeader) {
176
            throw new PHPUnit_Framework_ExpectationFailedException(
177
                'Failed asserting response is a redirect'
178
            );
179
        }
180
        if (!preg_match($pattern, $responseHeader->getFieldValue())) {
181
            throw new PHPUnit_Framework_ExpectationFailedException(
182
                sprintf(
183
                    'Failed asserting response redirects to URL MATCHING "%s", actual redirection is "%s"',
184
                    $pattern,
185
                    $responseHeader->getFieldValue()
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
186
                )
187
            );
188
        }
189
        $this->assertTrue((bool)preg_match($pattern, $responseHeader->getFieldValue()));
190
    }
191
192
    /**
193
     * Assert that redirect location does not match pattern
194
     *
195
     * @param string $pattern
196
     *
197
     * @throws \PHPUnit_Framework_ExpectationFailedException
198
     */
199
    protected function assertNotRedirectRegex($pattern)
200
    {
201
        $responseHeader = $this->getResponseHeader('Location');
202
        if (!$responseHeader) {
203
            throw new PHPUnit_Framework_ExpectationFailedException(
204
                'Failed asserting response is a redirect'
205
            );
206
        }
207
        if (preg_match($pattern, $responseHeader->getFieldValue())) {
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
208
            throw new PHPUnit_Framework_ExpectationFailedException(
209
                sprintf(
210
                    'Failed asserting response DOES NOT redirect to URL MATCHING "%s"',
211
                    $pattern
212
                )
213
            );
214
        }
215
        $this->assertFalse((bool)preg_match($pattern, $responseHeader->getFieldValue()));
216
    }
217
}