Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing enhancements #1635

Merged
merged 10 commits into from
Dec 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions system/CLI/CommandRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ protected function createCommandList()
// If no matching command files were found, bail
if (empty($files))
{
// This should never happen in unit testing.
// if it does, we have far bigger problems!
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
}

// Loop over each file checking to see if a command with that
Expand Down
60 changes: 28 additions & 32 deletions system/Config/BaseConfig.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php namespace CodeIgniter\Config;
<?php
namespace CodeIgniter\Config;

/**
* CodeIgniter
Expand Down Expand Up @@ -54,10 +55,8 @@ class BaseConfig
*
* @var array
*/
public static $registrars = [];

public static $registrars = [];
protected static $didDiscovery = false;

protected static $moduleConfig;

/**
Expand All @@ -73,7 +72,7 @@ public function __construct()
$properties = array_keys(get_object_vars($this));
$prefix = get_class($this);
$slashAt = strrpos($prefix, '\\');
$shortPrefix = strtolower(substr($prefix, $slashAt === false ? 0 : $slashAt + 1 ));
$shortPrefix = strtolower(substr($prefix, $slashAt === false ? 0 : $slashAt + 1));

foreach ($properties as $property)
{
Expand All @@ -83,52 +82,49 @@ public function __construct()
{
if ($value = $this->getEnvValue("{$property}.{$key}", $prefix, $shortPrefix))
{
if (is_null($value))
if (! is_null($value))
{
continue;
}

if ($value === 'false')
{
$value = false;
if ($value === 'false')
{
$value = false;
}
elseif ($value === 'true')
{
$value = true;
}

$this->$property[$key] = $value;
}
elseif ($value === 'true')
{
$value = true;
}

$this->$property[$key] = $value;
}
}
}
else
{
if (($value = $this->getEnvValue($property, $prefix, $shortPrefix)) !== false)
{
if (is_null($value))
if (! is_null($value))
{
continue;
}
if ($value === 'false')
{
$value = false;
}
elseif ($value === 'true')
{
$value = true;
}

if ($value === 'false')
{
$value = false;
$this->$property = is_bool($value) ? $value : trim($value, '\'"');
}
elseif ($value === 'true')
{
$value = true;
}

$this->$property = is_bool($value)
? $value
: trim($value, '\'"');
}
}
}

if (defined('ENVIRONMENT') && ENVIRONMENT !== 'testing')
{
// well, this won't happen during unit testing
// @codeCoverageIgnoreStart
$this->registerProperties();
// @codeCoverageIgnoreEnd
}
}

Expand Down
2 changes: 1 addition & 1 deletion system/Config/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public static function __callStatic(string $name, array $arguments)
{
$name = strtolower($name);

if (method_exists(__CLASS__, $name))
if (method_exists(Services::class, $name))
{
return Services::$name(...$arguments);
}
Expand Down
14 changes: 5 additions & 9 deletions system/Events/Events.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php namespace CodeIgniter\Events;
<?php
namespace CodeIgniter\Events;

use Config\Services;

Expand Down Expand Up @@ -112,12 +113,10 @@ public static function initialize()

foreach (static::$files as $file)
{
if (! is_file($file))
if (is_file($file))
{
continue;
include $file;
}

include $file;
}

static::$initialized = true;
Expand Down Expand Up @@ -183,9 +182,7 @@ public static function trigger($eventName, ...$arguments): bool
{
$start = microtime(true);

$result = static::$simulate === false
? $listener(...$arguments)
: true;
$result = static::$simulate === false ? $listener(...$arguments) : true;

if (CI_DEBUG)
{
Expand Down Expand Up @@ -345,5 +342,4 @@ public static function getPerformanceLogs()
}

//--------------------------------------------------------------------

}
6 changes: 2 additions & 4 deletions system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function initialize(string $uri = null)
{
if ($this->initialized === true)
{
return;
return $this;
}

$this->processGlobals($uri);
Expand Down Expand Up @@ -242,9 +242,7 @@ public function getFilters()
*/
public function addFilter(string $class, string $alias = null, string $when = 'before', string $section = 'globals')
{
$alias = is_null($alias)
? md5($class)
: $alias;
$alias = $alias ?? md5($class);

if (! isset($this->config->{$section}))
{
Expand Down
5 changes: 5 additions & 0 deletions system/Format/Exceptions/FormatException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public static function forInvalidJSON(string $error = null)
return new static(lang('Format.invalidJSON', [$error]));
}

/**
* This will never be thrown in travis-ci
*
* @codeCoverageIgnore
*/
public static function forMissingExtension()
{
return new static(lang('Format.missingExtension'));
Expand Down
3 changes: 3 additions & 0 deletions system/Format/XMLFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public function format(array $data)
// but best to check, and then provide a fallback.
if (! extension_loaded('simplexml'))
{
// never thrown in travis-ci
// @codeCoverageIgnoreStart
throw FormatException::forMissingExtension();
// @codeCoverageIgnoreEnd
}

$output = new \SimpleXMLElement('<?xml version="1.0"?><response></response>');
Expand Down
16 changes: 16 additions & 0 deletions tests/_support/Commands/AppInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,20 @@ public function run(array $params)
CLI::write('CI Version: ' . CLI::color(CodeIgniter::CI_VERSION, 'red'));
}

public function bomb()
{
try
{
CLI::color('test', 'white', 'Background');
}
catch (\RuntimeException $oops)
{
$this->showError($oops);
}
}

public function helpme()
{
$this->call('help');
}
}
63 changes: 58 additions & 5 deletions tests/system/API/ResponseTraitTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php namespace CodeIgniter\API;
<?php
namespace CodeIgniter\API;

use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\UserAgent;
Expand All @@ -9,6 +10,7 @@

class ResponseTraitTest extends \CIUnitTestCase
{

protected $request;
protected $response;

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

if (is_null($this->request))
{
$this->request = new MockIncomingRequest((object)$config, new URI($uri), null, new UserAgent());
$this->response = new MockResponse((object)$config);
$this->request = new MockIncomingRequest((object) $config, new URI($uri), null, new UserAgent());
$this->response = new MockResponse((object) $config);
}

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

public function testNoFormatterHTML()
public function testNoFormatter()
{
$this->formatter = null;
$controller = $this->makeController();
$controller = $this->makeController([], 'http://codeigniter.com', ['Accept' => 'application/json']);
$controller->respondCreated('A Custom Reason');

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

public function testAssociativeArrayPayload()
{
$this->formatter = null;
$controller = $this->makeController();
$payload = ['answer' => 42];
$expected = <<<EOH
{
"answer": 42
}
EOH;
$controller->respond($payload);
$this->assertEquals($expected, $this->response->getBody());
}

public function testArrayPayload()
{
$this->formatter = null;
$controller = $this->makeController();
$payload = [
1,
2,
3,
];
$expected = <<<EOH
[
1,
2,
3
]
EOH;
$controller->respond($payload);
$this->assertEquals($expected, $this->response->getBody());
}

public function testPHPtoArrayPayload()
{
$this->formatter = null;
$controller = $this->makeController();
$payload = new \stdClass();
$payload->name = 'Tom';
$payload->id = 1;
$expected = <<<EOH
{
"name": "Tom",
"id": 1
}
EOH;
$controller->respond((array)$payload);
$this->assertEquals($expected, $this->response->getBody());
}

public function testRespondSets404WithNoData()
{
$controller = $this->makeController();
Expand Down
36 changes: 34 additions & 2 deletions tests/system/CLI/CommandRunnerTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php namespace CodeIgniter\CLI;
<?php
namespace CodeIgniter\CLI;

use CodeIgniter\HTTP\UserAgent;
use Config\Services;
Expand All @@ -9,7 +10,6 @@ class CommandRunnerTest extends \CIUnitTestCase
{

private $stream_filter;

protected $env;
protected $config;
protected $request;
Expand Down Expand Up @@ -73,6 +73,38 @@ public function testDefaultCommand()
$this->assertContains('help command_name', $result);
}

public function testHelpCommand()
{
$this->runner->index(['help']);
$result = CITestStreamFilter::$buffer;

// make sure the result looks like basic help
$this->assertContains('Displays basic usage information.', $result);
$this->assertContains('help command_name', $result);
}

public function testHelpCommandDetails()
{
$this->runner->index(['help', 'session:migration']);
$result = CITestStreamFilter::$buffer;

// make sure the result looks like more detailed help
$this->assertContains('Description:', $result);
$this->assertContains('Usage:', $result);
$this->assertContains('Options:', $result);
}

public function testCommandProperties()
{
$this->runner->index(['help']);
$result = CITestStreamFilter::$buffer;
$commands = $this->runner->getCommands();
$command = new $commands['help']['class']($this->logger, $this->runner);

$this->assertEquals('Displays basic usage information.', $command->description);
$this->assertNull($command->notdescription);
}

public function testEmptyCommand()
{
$this->runner->index([null, 'list']);
Expand Down
Loading