|
1 | 1 | <?php
|
| 2 | +/** |
| 3 | + * Redirects module for Codeception. |
| 4 | + * |
| 5 | + * @package gamajo\codeception-redirects |
| 6 | + * @author Gary Jones |
| 7 | + * @copyright 2016 Gamajo Tech |
| 8 | + * @license MIT |
| 9 | + */ |
2 | 10 |
|
3 | 11 | namespace Codeception\Module;
|
4 | 12 |
|
5 | 13 | use Codeception\Module;
|
6 | 14 |
|
7 |
| -class Redirects extends Module{ |
8 |
| - |
| 15 | +/** |
| 16 | + * Class Redirects |
| 17 | + * |
| 18 | + * @package Codeception\Module |
| 19 | + * @author Gary Jones |
| 20 | + */ |
| 21 | +class Redirects extends Module |
| 22 | +{ |
9 | 23 | /**
|
10 |
| - * Protocol constants that can be used with permanentRedirectForProtocol() method. |
| 24 | + * Protocol (http) constant that can be used with permanentRedirectForProtocol() method. |
11 | 25 | */
|
12 | 26 | const PROTOCOL_HTTP = 'http';
|
| 27 | + |
| 28 | + /** |
| 29 | + * Protocol (https) constant that can be used with permanentRedirectForProtocol() method. |
| 30 | + */ |
13 | 31 | const PROTOCOL_HTTPS = 'https';
|
14 | 32 |
|
15 |
| - /** |
16 |
| - * Check that a 301 HTTP Status is returned with the correct Location URL. |
17 |
| - * |
18 |
| - * @since 0.1.0 |
19 |
| - * |
20 |
| - * @param string $url Relative or absolute URL of redirect destination. |
21 |
| - */ |
22 |
| - public function seePermanentRedirectTo($url) |
23 |
| - { |
24 |
| - $response = $this->getModule('PhpBrowser')->client->getInternalResponse(); |
25 |
| - $responseCode = $response->getStatus(); |
26 |
| - $locationHeader = $response->getHeader('Location', true); |
| 33 | + /** |
| 34 | + * Toggle redirections on and off. |
| 35 | + * |
| 36 | + * By default, BrowserKit will follow redirections, so to check for 30* |
| 37 | + * HTTP status codes and Location headers, they have to be turned off. |
| 38 | + * |
| 39 | + * @since 0.1.0 |
| 40 | + * |
| 41 | + * @param bool $followRedirects Optional. Whether to follow redirects or not. |
| 42 | + * Default is true. |
| 43 | + */ |
| 44 | + public function followRedirects($followRedirects = true) |
| 45 | + { |
| 46 | + $this->getModule('PhpBrowser')->client->followRedirects($followRedirects); |
| 47 | + } |
27 | 48 |
|
28 |
| - // Check for 301 response code. |
29 |
| - $this->assertEquals(301, $responseCode); |
| 49 | + /** |
| 50 | + * Check that a 200 HTTP Status is returned with the correct Location URL. |
| 51 | + * |
| 52 | + * Should be HTTP. |
| 53 | + * |
| 54 | + * @since 0.1.0 |
| 55 | + * |
| 56 | + * @param string $url Relative or absolute URL of redirect destination. |
| 57 | + */ |
| 58 | + public function seePermanentRedirectToHttpFor($url) |
| 59 | + { |
| 60 | + $this->permanentRedirectForProtocol($url, self::PROTOCOL_HTTP); |
| 61 | + } |
30 | 62 |
|
31 |
| - // Check location header URL contains submitted URL. |
32 |
| - $this->assertContains($url, $locationHeader); |
33 |
| - } |
| 63 | + /** |
| 64 | + * Check that a 301 HTTP Status is returned with the correct Location URL. |
| 65 | + * |
| 66 | + * @since 0.1.0 |
| 67 | + * |
| 68 | + * @param string $url Relative or absolute URL of redirect destination. |
| 69 | + */ |
| 70 | + public function seePermanentRedirectTo($url) |
| 71 | + { |
| 72 | + $response = $this->getModule('PhpBrowser')->client->getInternalResponse(); |
| 73 | + $responseCode = $response->getStatus(); |
| 74 | + $locationHeader = $response->getHeader('Location', true); |
34 | 75 |
|
35 |
| - /** |
36 |
| - * Check that a 200 HTTP Status is returned with the correct Location URL. |
37 |
| - * |
38 |
| - * Should be HTTPS. |
39 |
| - * |
40 |
| - * @since 0.1.0 |
41 |
| - * |
42 |
| - * @param string $url Relative or absolute URL of redirect destination. |
43 |
| - */ |
44 |
| - public function seePermanentRedirectToHttpsFor($url) |
45 |
| - { |
46 |
| - $this->permanentRedirectForProtocol($url, self::PROTOCOL_HTTPS); |
47 |
| - } |
| 76 | + // Check for 301 response code. |
| 77 | + $this->assertEquals(301, $responseCode); |
48 | 78 |
|
49 |
| - /** |
50 |
| - * Check that a 200 HTTP Status is returned with the correct Location URL. |
51 |
| - * |
52 |
| - * Should be HTTP. |
53 |
| - * |
54 |
| - * @since 0.1.0 |
55 |
| - * |
56 |
| - * @param string $url Relative or absolute URL of redirect destination. |
57 |
| - */ |
58 |
| - public function seePermanentRedirectToHttpFor($url) |
59 |
| - { |
60 |
| - $this->permanentRedirectForProtocol($url, self::PROTOCOL_HTTP); |
61 |
| - } |
| 79 | + // Check location header URL contains submitted URL. |
| 80 | + $this->assertContains($url, $locationHeader); |
| 81 | + } |
62 | 82 |
|
63 |
| - /** |
64 |
| - * Toggle redirections on and off. |
65 |
| - * |
66 |
| - * By default, BrowserKit will follow redirections, so to check for 30* |
67 |
| - * HTTP status codes and Location headers, they have to be turned off. |
68 |
| - * |
69 |
| - * @since 0.1.0 |
70 |
| - * |
71 |
| - * @param bool $followRedirects Optional. Whether to follow redirects or not. |
72 |
| - * Default is true. |
73 |
| - */ |
74 |
| - public function followRedirects($followRedirects = true) |
75 |
| - { |
76 |
| - $this->getModule('PhpBrowser')->client->followRedirects($followRedirects); |
77 |
| - } |
| 83 | + /** |
| 84 | + * Check that a 200 HTTP Status is returned with the correct Location URL. |
| 85 | + * |
| 86 | + * Should be HTTPS. |
| 87 | + * |
| 88 | + * @since 0.1.0 |
| 89 | + * |
| 90 | + * @param string $url Relative or absolute URL of redirect destination. |
| 91 | + */ |
| 92 | + public function seePermanentRedirectToHttpsFor($url) |
| 93 | + { |
| 94 | + $this->permanentRedirectForProtocol($url, self::PROTOCOL_HTTPS); |
| 95 | + } |
78 | 96 |
|
79 |
| - /** |
80 |
| - * Check that a 200 HTTP Status is returned with the correct Location URL. |
81 |
| - * |
82 |
| - * @param string $url Relative or absolute URL of redirect destination. |
83 |
| - * @param string $protocol Protocol: 'http' or 'https'. |
84 |
| - */ |
85 |
| - protected function permanentRedirectForProtocol($url, $protocol) |
86 |
| - { |
87 |
| - $url = ltrim($url, '/'); |
88 |
| - $this->getModule('REST')->sendHead($url); |
| 97 | + /** |
| 98 | + * Check that a 200 HTTP Status is returned with the correct Location URL. |
| 99 | + * |
| 100 | + * @param string $url Relative or absolute URL of redirect destination. |
| 101 | + * @param string $protocol Protocol: 'http' or 'https'. |
| 102 | + */ |
| 103 | + protected function permanentRedirectForProtocol($url, $protocol) |
| 104 | + { |
| 105 | + $url = ltrim($url, '/'); |
| 106 | + $this->getModule('REST')->sendHead($url); |
89 | 107 |
|
90 |
| - $client = $this->getModule('PhpBrowser')->client; |
91 |
| - $responseCode = $client->getInternalResponse()->getStatus(); |
92 |
| - $responseUri = $client->getHistory()->current()->getUri(); |
93 |
| - $scheme = parse_url($responseUri, PHP_URL_SCHEME); |
| 108 | + $client = $this->getModule('PhpBrowser')->client; |
| 109 | + $responseCode = $client->getInternalResponse()->getStatus(); |
| 110 | + $responseUri = $client->getHistory()->current()->getUri(); |
| 111 | + $scheme = parse_url($responseUri, PHP_URL_SCHEME); |
94 | 112 |
|
95 |
| - // Check for 200 response code. |
96 |
| - $this->assertEquals(200, $responseCode); |
| 113 | + // Check for 200 response code. |
| 114 | + $this->assertEquals(200, $responseCode); |
97 | 115 |
|
98 |
| - // Check for submitted http/https value matches destination URL. |
99 |
| - $this->assertEquals($protocol, $scheme); |
| 116 | + // Check for submitted http/https value matches destination URL. |
| 117 | + $this->assertEquals($protocol, $scheme); |
100 | 118 |
|
101 |
| - // Check that destination URL contains submitted URL part. |
102 |
| - $this->assertContains($url, $responseUri); |
103 |
| - } |
| 119 | + // Check that destination URL contains submitted URL part. |
| 120 | + $this->assertContains($url, $responseUri); |
| 121 | + } |
104 | 122 | }
|
0 commit comments