Repository navigation
Expand file tree
/
Copy pathControllerTest.php
More file actions
173 lines (144 loc) · 4.83 KB
/
Copy pathControllerTest.php
File metadata and controls
173 lines (144 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php namespace CodeIgniter;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\Mock\MockCodeIgniter;
use CodeIgniter\Validation\Exceptions\ValidationException;
use Config\App;
/**
* Exercise our core Controller class.
* Not a lot of business logic, so concentrate on making sure
* we can exercise everything without blowing up :-/
*
* @backupGlobals enabled
*/
class ControllerTest extends \CodeIgniter\Test\CIUnitTestCase
{
/**
* @var \CodeIgniter\CodeIgniter
*/
protected $codeigniter;
/**
* @var \CodeIgniter\Controller
*/
protected $controller;
/**
* Current request.
*
* @var \CodeIgniter\HTTP\Request
*/
protected $request;
/**
* Current response.
*
* @var \CodeIgniter\HTTP\Response
*/
protected $response;
/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
//--------------------------------------------------------------------
protected function setUp(): void
{
parent::setUp();
$this->config = new App();
$this->request = new \CodeIgniter\HTTP\IncomingRequest($this->config, new \CodeIgniter\HTTP\URI('https://somwhere.com'), null, new UserAgent());
$this->response = new \CodeIgniter\HTTP\Response($this->config);
$this->logger = \Config\Services::logger();
$this->codeigniter = new MockCodeIgniter($this->config);
}
//--------------------------------------------------------------------
public function testConstructor()
{
// make sure we can instantiate one
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
$this->assertInstanceOf(Controller::class, $this->controller);
}
public function testConstructorHTTPS()
{
$original = $_SERVER;
$_SERVER = ['HTTPS' => 'on'];
// make sure we can instantiate one
$this->controller = new Class() extends Controller
{
protected $forceHTTPS = 1;
};
$this->controller->initController($this->request, $this->response, $this->logger);
$this->assertInstanceOf(Controller::class, $this->controller);
$_SERVER = $original; // restore so code coverage doesn't break
}
//--------------------------------------------------------------------
public function testCachePage()
{
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
$method = $this->getPrivateMethodInvoker($this->controller, 'cachePage');
$this->assertNull($method(10));
}
public function testValidate()
{
// make sure we can instantiate one
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
// and that we can attempt validation, with no rules
$method = $this->getPrivateMethodInvoker($this->controller, 'validate');
$this->assertFalse($method([]));
}
public function testValidateWithStringRulesNotFound()
{
$this->expectException(ValidationException::class);
// make sure we can instantiate one
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
$method = $this->getPrivateMethodInvoker($this->controller, 'validate');
$this->assertFalse($method('signup'));
}
public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig()
{
$validation = config('Validation');
$validation->signup = [
'username' => 'required',
];
$validation->signup_errors = [
'username' => [
'required' => 'You must choose a username.',
],
];
// make sure we can instantiate one
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
$method = $this->getPrivateMethodInvoker($this->controller, 'validate');
$this->assertFalse($method('signup'));
$this->assertEquals('You must choose a username.', Services::validation()->getError());
}
public function testValidateWithStringRulesFoundUseMessagesParameter()
{
$validation = config('Validation');
$validation->signup = [
'username' => 'required',
];
// make sure we can instantiate one
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
$method = $this->getPrivateMethodInvoker($this->controller, 'validate');
$this->assertFalse($method('signup', [
'username' => [
'required' => 'You must choose a username.',
],
]));
$this->assertEquals('You must choose a username.', Services::validation()->getError());
}
//--------------------------------------------------------------------
public function testHelpers()
{
$this->controller = new Class() extends Controller
{
protected $helpers = [
'cookie',
'text',
];
};
$this->controller->initController($this->request, $this->response, $this->logger);
$this->assertInstanceOf(Controller::class, $this->controller);
}
}