Skip to content

Commit 66e99b6

Browse files
committed
Added custom Http\Response and seeRedirectTo method
1 parent 508ceb9 commit 66e99b6

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Arachne\Codeception\Http\DI;
4+
5+
use Nette\DI\CompilerExtension;
6+
7+
/**
8+
* @author Jáchym Toušek
9+
*/
10+
class HttpExtension extends CompilerExtension
11+
{
12+
13+
public function loadConfiguration()
14+
{
15+
$builder = $this->getContainerBuilder();
16+
17+
$builder->getDefinition('httpResponse')
18+
->setClass('Arachne\Codeception\Http\Response');
19+
}
20+
21+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
3+
namespace Arachne\Codeception\Http;
4+
5+
use Nette\Http\IResponse;
6+
use Nette\Http\Response as HttpResponse;
7+
use Nette\Object;
8+
use Nette\Utils\DateTime;
9+
10+
/**
11+
* HttpResponse class for tests.
12+
*
13+
* @author Jáchym Toušek
14+
*/
15+
class Response extends Object implements IResponse
16+
{
17+
18+
/** @var int */
19+
private $code = self::S200_OK;
20+
21+
/** @var array */
22+
private $headers = [];
23+
24+
/**
25+
* @param int
26+
* @return self
27+
*/
28+
public function setCode($code)
29+
{
30+
$this->code = $code;
31+
return $this;
32+
}
33+
34+
/**
35+
* @return int
36+
*/
37+
public function getCode()
38+
{
39+
return $this->code;
40+
}
41+
42+
/**
43+
* @param string $name
44+
* @param string $value
45+
* @return self
46+
*/
47+
public function setHeader($name, $value)
48+
{
49+
$this->headers[$name] = $value;
50+
return $this;
51+
}
52+
53+
/**
54+
* @param string $name
55+
* @param string $value
56+
* @return self
57+
*/
58+
public function addHeader($name, $value)
59+
{
60+
$this->headers[$name] = $value;
61+
return $this;
62+
}
63+
64+
/**
65+
* @param string $type
66+
* @param string $charset
67+
* @return self
68+
*/
69+
public function setContentType($type, $charset = NULL)
70+
{
71+
$this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : ''));
72+
return $this;
73+
}
74+
75+
/**
76+
* @param string $url
77+
* @param int $code
78+
*/
79+
public function redirect($url, $code = self::S302_FOUND)
80+
{
81+
$this->setCode($code);
82+
$this->setHeader('Location', $url);
83+
}
84+
85+
/**
86+
* @param string|int|DateTime $time
87+
* @return self
88+
*/
89+
public function setExpiration($time)
90+
{
91+
if (!$time) {
92+
$this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate');
93+
$this->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
94+
return $this;
95+
}
96+
97+
$time = DateTime::from($time);
98+
$this->setHeader('Cache-Control', 'max-age=' . ($time->format('U') - time()));
99+
$this->setHeader('Expires', HttpResponse::date($time));
100+
return $this;
101+
}
102+
103+
/**
104+
* @return bool
105+
*/
106+
public function isSent()
107+
{
108+
return FALSE;
109+
}
110+
111+
/**
112+
* @param string $name
113+
* @param mixed $default
114+
* @return mixed
115+
*/
116+
public function getHeader($name, $default = NULL)
117+
{
118+
return isset($this->headers[$name]) ? $this->headers[$name] : $default;
119+
}
120+
121+
/**
122+
* @return array
123+
*/
124+
public function getHeaders()
125+
{
126+
return $this->headers;
127+
}
128+
129+
/**
130+
* @param string $name
131+
* @param string $value
132+
* @param string|int|DateTime $time
133+
* @param string $path
134+
* @param string $domain
135+
* @param bool $secure
136+
* @param bool $httpOnly
137+
* @return self
138+
*/
139+
public function setCookie($name, $value, $time, $path = NULL, $domain = NULL, $secure = NULL, $httpOnly = NULL)
140+
{
141+
return $this;
142+
}
143+
144+
/**
145+
* @param string $name
146+
* @param string $path
147+
* @param string $domain
148+
* @param bool $secure
149+
*/
150+
public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL)
151+
{
152+
return $this;
153+
}
154+
155+
}

src/Codeception/Module/Nette.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ public function grabService($service)
107107
$this->fail($e->getMessage());
108108
}
109109
}
110+
111+
public function seeRedirectTo($url)
112+
{
113+
$response = $this->container->getByType('Nette\Http\IResponse');
114+
if ($response->getHeader('Location') !== $url) {
115+
$this->fail('Couldn\'t confirm redirect target to be "' . $url . '", Location header contains "' . $response->getHeader('Location') . '".');
116+
}
117+
}
110118

111119
public function debugContent()
112120
{

src/Codeception/Module/config.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
nette:
22
security:
33
frames: true
4+
5+
extensions:
6+
Arachne.Codeception.Http: Arachne\Codeception\Http\DI\HttpExtension

0 commit comments

Comments
 (0)