Skip to content

Commit 43ef1ec

Browse files
authored
Merge pull request #1635 from jim-parry/unit-testing
Unit testing enhancements
2 parents 66c1d8a + c2a5cde commit 43ef1ec

17 files changed

Lines changed: 449 additions & 71 deletions

File tree

system/CLI/CommandRunner.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ protected function createCommandList()
136136
// If no matching command files were found, bail
137137
if (empty($files))
138138
{
139+
// This should never happen in unit testing.
140+
// if it does, we have far bigger problems!
141+
// @codeCoverageIgnoreStart
139142
return;
143+
// @codeCoverageIgnoreEnd
140144
}
141145

142146
// Loop over each file checking to see if a command with that

system/Config/BaseConfig.php

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php namespace CodeIgniter\Config;
1+
<?php
2+
namespace CodeIgniter\Config;
23

34
/**
45
* CodeIgniter
@@ -54,10 +55,8 @@ class BaseConfig
5455
*
5556
* @var array
5657
*/
57-
public static $registrars = [];
58-
58+
public static $registrars = [];
5959
protected static $didDiscovery = false;
60-
6160
protected static $moduleConfig;
6261

6362
/**
@@ -73,7 +72,7 @@ public function __construct()
7372
$properties = array_keys(get_object_vars($this));
7473
$prefix = get_class($this);
7574
$slashAt = strrpos($prefix, '\\');
76-
$shortPrefix = strtolower(substr($prefix, $slashAt === false ? 0 : $slashAt + 1 ));
75+
$shortPrefix = strtolower(substr($prefix, $slashAt === false ? 0 : $slashAt + 1));
7776

7877
foreach ($properties as $property)
7978
{
@@ -83,52 +82,49 @@ public function __construct()
8382
{
8483
if ($value = $this->getEnvValue("{$property}.{$key}", $prefix, $shortPrefix))
8584
{
86-
if (is_null($value))
85+
if (! is_null($value))
8786
{
88-
continue;
89-
}
90-
91-
if ($value === 'false')
92-
{
93-
$value = false;
87+
if ($value === 'false')
88+
{
89+
$value = false;
90+
}
91+
elseif ($value === 'true')
92+
{
93+
$value = true;
94+
}
95+
96+
$this->$property[$key] = $value;
9497
}
95-
elseif ($value === 'true')
96-
{
97-
$value = true;
98-
}
99-
100-
$this->$property[$key] = $value;
10198
}
10299
}
103100
}
104101
else
105102
{
106103
if (($value = $this->getEnvValue($property, $prefix, $shortPrefix)) !== false)
107104
{
108-
if (is_null($value))
105+
if (! is_null($value))
109106
{
110-
continue;
111-
}
107+
if ($value === 'false')
108+
{
109+
$value = false;
110+
}
111+
elseif ($value === 'true')
112+
{
113+
$value = true;
114+
}
112115

113-
if ($value === 'false')
114-
{
115-
$value = false;
116+
$this->$property = is_bool($value) ? $value : trim($value, '\'"');
116117
}
117-
elseif ($value === 'true')
118-
{
119-
$value = true;
120-
}
121-
122-
$this->$property = is_bool($value)
123-
? $value
124-
: trim($value, '\'"');
125118
}
126119
}
127120
}
128121

129122
if (defined('ENVIRONMENT') && ENVIRONMENT !== 'testing')
130123
{
124+
// well, this won't happen during unit testing
125+
// @codeCoverageIgnoreStart
131126
$this->registerProperties();
127+
// @codeCoverageIgnoreEnd
132128
}
133129
}
134130

system/Config/BaseService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public static function __callStatic(string $name, array $arguments)
184184
{
185185
$name = strtolower($name);
186186

187-
if (method_exists(__CLASS__, $name))
187+
if (method_exists(Services::class, $name))
188188
{
189189
return Services::$name(...$arguments);
190190
}

system/Events/Events.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php namespace CodeIgniter\Events;
1+
<?php
2+
namespace CodeIgniter\Events;
23

34
use Config\Services;
45

@@ -112,12 +113,10 @@ public static function initialize()
112113

113114
foreach (static::$files as $file)
114115
{
115-
if (! is_file($file))
116+
if (is_file($file))
116117
{
117-
continue;
118+
include $file;
118119
}
119-
120-
include $file;
121120
}
122121

123122
static::$initialized = true;
@@ -183,9 +182,7 @@ public static function trigger($eventName, ...$arguments): bool
183182
{
184183
$start = microtime(true);
185184

186-
$result = static::$simulate === false
187-
? $listener(...$arguments)
188-
: true;
185+
$result = static::$simulate === false ? $listener(...$arguments) : true;
189186

190187
if (CI_DEBUG)
191188
{
@@ -345,5 +342,4 @@ public static function getPerformanceLogs()
345342
}
346343

347344
//--------------------------------------------------------------------
348-
349345
}

system/Filters/Filters.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public function initialize(string $uri = null)
204204
{
205205
if ($this->initialized === true)
206206
{
207-
return;
207+
return $this;
208208
}
209209

210210
$this->processGlobals($uri);
@@ -242,9 +242,7 @@ public function getFilters()
242242
*/
243243
public function addFilter(string $class, string $alias = null, string $when = 'before', string $section = 'globals')
244244
{
245-
$alias = is_null($alias)
246-
? md5($class)
247-
: $alias;
245+
$alias = $alias ?? md5($class);
248246

249247
if (! isset($this->config->{$section}))
250248
{

system/Format/Exceptions/FormatException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public static function forInvalidJSON(string $error = null)
99
return new static(lang('Format.invalidJSON', [$error]));
1010
}
1111

12+
/**
13+
* This will never be thrown in travis-ci
14+
*
15+
* @codeCoverageIgnore
16+
*/
1217
public static function forMissingExtension()
1318
{
1419
return new static(lang('Format.missingExtension'));

system/Format/XMLFormatter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ public function format(array $data)
5353
// but best to check, and then provide a fallback.
5454
if (! extension_loaded('simplexml'))
5555
{
56+
// never thrown in travis-ci
57+
// @codeCoverageIgnoreStart
5658
throw FormatException::forMissingExtension();
59+
// @codeCoverageIgnoreEnd
5760
}
5861

5962
$output = new \SimpleXMLElement('<?xml version="1.0"?><response></response>');

tests/_support/Commands/AppInfo.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,20 @@ public function run(array $params)
1717
CLI::write('CI Version: ' . CLI::color(CodeIgniter::CI_VERSION, 'red'));
1818
}
1919

20+
public function bomb()
21+
{
22+
try
23+
{
24+
CLI::color('test', 'white', 'Background');
25+
}
26+
catch (\RuntimeException $oops)
27+
{
28+
$this->showError($oops);
29+
}
30+
}
31+
32+
public function helpme()
33+
{
34+
$this->call('help');
35+
}
2036
}

tests/system/API/ResponseTraitTest.php

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php namespace CodeIgniter\API;
1+
<?php
2+
namespace CodeIgniter\API;
23

34
use CodeIgniter\HTTP\URI;
45
use CodeIgniter\HTTP\UserAgent;
@@ -9,6 +10,7 @@
910

1011
class ResponseTraitTest extends \CIUnitTestCase
1112
{
13+
1214
protected $request;
1315
protected $response;
1416

@@ -45,8 +47,8 @@ protected function makeController(array $userConfig = [], string $uri = 'http://
4547

4648
if (is_null($this->request))
4749
{
48-
$this->request = new MockIncomingRequest((object)$config, new URI($uri), null, new UserAgent());
49-
$this->response = new MockResponse((object)$config);
50+
$this->request = new MockIncomingRequest((object) $config, new URI($uri), null, new UserAgent());
51+
$this->response = new MockResponse((object) $config);
5052
}
5153

5254
// Insert headers into request.
@@ -102,15 +104,66 @@ public function testNoFormatterJSON()
102104
$this->assertEquals($expected, $this->response->getBody());
103105
}
104106

105-
public function testNoFormatterHTML()
107+
public function testNoFormatter()
106108
{
107109
$this->formatter = null;
108-
$controller = $this->makeController();
110+
$controller = $this->makeController([], 'http://codeigniter.com', ['Accept' => 'application/json']);
109111
$controller->respondCreated('A Custom Reason');
110112

111113
$this->assertEquals('A Custom Reason', $this->response->getBody());
112114
}
113115

116+
public function testAssociativeArrayPayload()
117+
{
118+
$this->formatter = null;
119+
$controller = $this->makeController();
120+
$payload = ['answer' => 42];
121+
$expected = <<<EOH
122+
{
123+
"answer": 42
124+
}
125+
EOH;
126+
$controller->respond($payload);
127+
$this->assertEquals($expected, $this->response->getBody());
128+
}
129+
130+
public function testArrayPayload()
131+
{
132+
$this->formatter = null;
133+
$controller = $this->makeController();
134+
$payload = [
135+
1,
136+
2,
137+
3,
138+
];
139+
$expected = <<<EOH
140+
[
141+
1,
142+
2,
143+
3
144+
]
145+
EOH;
146+
$controller->respond($payload);
147+
$this->assertEquals($expected, $this->response->getBody());
148+
}
149+
150+
public function testPHPtoArrayPayload()
151+
{
152+
$this->formatter = null;
153+
$controller = $this->makeController();
154+
$payload = new \stdClass();
155+
$payload->name = 'Tom';
156+
$payload->id = 1;
157+
$expected = <<<EOH
158+
{
159+
"name": "Tom",
160+
"id": 1
161+
}
162+
EOH;
163+
$controller->respond((array)$payload);
164+
$this->assertEquals($expected, $this->response->getBody());
165+
}
166+
114167
public function testRespondSets404WithNoData()
115168
{
116169
$controller = $this->makeController();

tests/system/CLI/CommandRunnerTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php namespace CodeIgniter\CLI;
1+
<?php
2+
namespace CodeIgniter\CLI;
23

34
use CodeIgniter\HTTP\UserAgent;
45
use Config\Services;
@@ -9,7 +10,6 @@ class CommandRunnerTest extends \CIUnitTestCase
910
{
1011

1112
private $stream_filter;
12-
1313
protected $env;
1414
protected $config;
1515
protected $request;
@@ -73,6 +73,38 @@ public function testDefaultCommand()
7373
$this->assertContains('help command_name', $result);
7474
}
7575

76+
public function testHelpCommand()
77+
{
78+
$this->runner->index(['help']);
79+
$result = CITestStreamFilter::$buffer;
80+
81+
// make sure the result looks like basic help
82+
$this->assertContains('Displays basic usage information.', $result);
83+
$this->assertContains('help command_name', $result);
84+
}
85+
86+
public function testHelpCommandDetails()
87+
{
88+
$this->runner->index(['help', 'session:migration']);
89+
$result = CITestStreamFilter::$buffer;
90+
91+
// make sure the result looks like more detailed help
92+
$this->assertContains('Description:', $result);
93+
$this->assertContains('Usage:', $result);
94+
$this->assertContains('Options:', $result);
95+
}
96+
97+
public function testCommandProperties()
98+
{
99+
$this->runner->index(['help']);
100+
$result = CITestStreamFilter::$buffer;
101+
$commands = $this->runner->getCommands();
102+
$command = new $commands['help']['class']($this->logger, $this->runner);
103+
104+
$this->assertEquals('Displays basic usage information.', $command->description);
105+
$this->assertNull($command->notdescription);
106+
}
107+
76108
public function testEmptyCommand()
77109
{
78110
$this->runner->index([null, 'list']);

0 commit comments

Comments
 (0)