Merge pull request #1067 from jim-parry/testing/common

Testing/common
This commit is contained in:
Lonnie Ezell 2018-06-07 23:32:54 -05:00 committed by GitHub
commit 2720620536
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 221 additions and 18 deletions

View File

@ -440,8 +440,10 @@ if ( ! function_exists('log_message'))
return $logger->log($level, $message, $context);
}
// @codeCoverageIgnoreStart
return Services::logger(true)
->log($level, $message, $context);
// @codeCoverageIgnoreEnd
}
}
@ -668,6 +670,9 @@ if ( ! function_exists('force_https'))
* Defaults to 1 year.
* @param RequestInterface $request
* @param ResponseInterface $response
*
* Not testable, as it will exit!
* @codeCoverageIgnore
*/
function force_https(int $duration = 31536000, RequestInterface $request = null, ResponseInterface $response = null)
{
@ -837,6 +842,8 @@ if ( ! function_exists('is_really_writable'))
* @param string $file
*
* @return bool
*
* @codeCoverageIgnore Not practical to test, as travis runs on linux
*/
function is_really_writable($file)
{
@ -931,6 +938,8 @@ if ( ! function_exists('function_usable'))
* @param string $function_name Function to check for
* @return bool TRUE if the function exists and is safe to call,
* FALSE otherwise.
*
* @codeCoverageIgnore This is too exotic
*/
function function_usable($function_name)
{
@ -959,6 +968,8 @@ if (! function_exists('dd'))
* Prints a Kint debug report and exits.
*
* @param array ...$vars
*
* @codeCoverageIgnore Can't be tested ... exits
*/
function dd(...$vars)
{

View File

@ -0,0 +1 @@
<h1><?= $testString ?></h1>

View File

@ -1,5 +1,16 @@
<?php
use Config\App;
use Config\Autoload;
use CodeIgniter\Config\Services;
use CodeIgniter\Router\RouteCollection;
use CodeIgniter\HTTP\RequestResponse;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\UserAgent;
use Tests\Support\Autoloader\MockFileLocator;
use Tests\Support\HTTP\MockIncomingRequest;
/**
* @backupGlobals enabled
*/
@ -12,7 +23,7 @@ class CommomFunctionsTest extends \CIUnitTestCase
{
parent::setUp();
unset($_ENV['foo'], $_SERVER['foo']);
unset($_ENV['foo'], $_SERVER['foo']);
}
//--------------------------------------------------------------------
@ -48,40 +59,220 @@ class CommomFunctionsTest extends \CIUnitTestCase
// ------------------------------------------------------------------------
public function testEnvReturnsDefault()
{
$this->assertEquals('baz', env('foo', 'baz'));
}
public function testEnvReturnsDefault()
{
$this->assertEquals('baz', env('foo', 'baz'));
}
public function testEnvGetsFromSERVER()
{
$_SERVER['foo'] = 'bar';
public function testEnvGetsFromSERVER()
{
$_SERVER['foo'] = 'bar';
$this->assertEquals('bar', env('foo', 'baz'));
}
$this->assertEquals('bar', env('foo', 'baz'));
}
public function testEnvGetsFromENV()
{
$_ENV['foo'] = 'bar';
public function testEnvGetsFromENV()
{
$_ENV['foo'] = 'bar';
$this->assertEquals('bar', env('foo', 'baz'));
}
$this->assertEquals('bar', env('foo', 'baz'));
}
public function testEnvBooleans()
{
$_ENV['p1'] = 'true';
$_ENV['p2'] = 'false';
$_ENV['p3'] = 'empty';
$_ENV['p4'] = 'null';
$this->assertTrue(env('p1'));
$this->assertFalse(env('p2'));
$this->assertEmpty(env('p3'));
$this->assertNull(env('p4'));
}
// ------------------------------------------------------------------------
public function testRedirectReturnsRedirectResponse()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$response = $this->createMock(\CodeIgniter\HTTP\Response::class);
$routes = new \CodeIgniter\Router\RouteCollection(new \Tests\Support\Autoloader\MockFileLocator(new \Config\Autoload()));
$routes = new \CodeIgniter\Router\RouteCollection(new \Tests\Support\Autoloader\MockFileLocator(new \Config\Autoload()));
\CodeIgniter\Services::injectMock('response', $response);
\CodeIgniter\Services::injectMock('routes', $routes);
$routes->add('home/base', 'Controller::index', ['as' => 'base']);
$response->method('redirect')
->will($this->returnArgument(0));
->will($this->returnArgument(0));
$this->assertInstanceOf(\CodeIgniter\HTTP\RedirectResponse::class, redirect('base'));
}
}
public function testRedirectDefault()
{
$this->assertInstanceOf(\CodeIgniter\HTTP\RedirectResponse::class, redirect());
}
// ------------------------------------------------------------------------
public function testView()
{
$data = [
'testString' => 'bar',
'bar' => 'baz',
];
$expected = '<h1>bar</h1>';
$this->assertContains($expected, view('\Tests\Support\View\Views\simple', $data, []));
}
public function testViewSavedData()
{
$data = [
'testString' => 'bar',
'bar' => 'baz',
];
$expected = '<h1>bar</h1>';
$this->assertContains($expected, view('\Tests\Support\View\Views\simple', $data, ['saveData' => true]));
$this->assertContains($expected, view('\Tests\Support\View\Views\simple'));
}
// ------------------------------------------------------------------------
public function testViewCell()
{
$expected = 'Hello';
$this->assertEquals($expected, view_cell('\CodeIgniter\View\SampleClass::hello'));
}
// ------------------------------------------------------------------------
public function testEscapeBadContext()
{
$this->expectException(InvalidArgumentException::class);
esc(['width' => '800', 'height' => '600'], 'bogus');
}
// ------------------------------------------------------------------------
public function testSessionInstance()
{
$this->assertInstanceOf(CodeIgniter\Session\Session::class, session());
}
public function testSessionVariable()
{
$_SESSION['notbogus'] = 'Hi there';
$this->assertEquals('Hi there', session('notbogus'));
}
public function testSessionVariableNotThere()
{
$_SESSION['bogus'] = 'Hi there';
$this->assertEquals(null, session('notbogus'));
}
// ------------------------------------------------------------------------
public function testSingleService()
{
$timer1 = single_service('timer');
$timer2 = single_service('timer');
$this->assertFalse($timer1 === $timer2);
}
// ------------------------------------------------------------------------
public function testRouteTo()
{
// prime the pump
$routes = service('routes');
$routes->add('path/(:any)/to/(:num)', 'myController::goto/$1/$2');
$this->assertEquals('/path/string/to/13', route_to('myController::goto', 'string', 13));
}
// ------------------------------------------------------------------------
public function testInvisible()
{
$this->assertEquals('Javascript', remove_invisible_characters("Java\0script"));
}
public function testInvisibleEncoded()
{
$this->assertEquals('Javascript', remove_invisible_characters("Java%0cscript", true));
}
// ------------------------------------------------------------------------
public function testAppTimezone()
{
$this->assertEquals('America/Chicago', app_timezone());
}
// ------------------------------------------------------------------------
public function testCSRFToken()
{
$this->assertEquals('csrf_test_name', csrf_token());
}
public function testHash()
{
$this->assertEquals(32, strlen(csrf_hash()));
}
public function testCSRFField()
{
$this->assertContains('<input type="hidden" ', csrf_field());
}
// ------------------------------------------------------------------------
public function testOldInput()
{
// setup from RedirectResponseTest...
$_SERVER['REQUEST_METHOD'] = 'GET';
$this->config = new App();
$this->config->baseURL = 'http://example.com';
$this->routes = new RouteCollection(new MockFileLocator(new Autoload()));
Services::injectMock('routes', $this->routes);
$this->request = new MockIncomingRequest($this->config, new URI('http://example.com'), null, new UserAgent());
Services::injectMock('request', $this->request);
// setup & ask for a redirect...
$_SESSION = [];
$_GET = ['foo' => 'bar'];
$_POST = ['bar' => 'baz', 'zibble' => serialize('fritz')];
$response = new RedirectResponse(new App());
$returned = $response->withInput();
$this->assertEquals('bar', old('foo')); // regular parameter
$this->assertEquals('doo', old('yabba dabba', 'doo')); // non-existing parameter
$this->assertEquals('fritz', old('zibble')); // serialized parameter
}
// ------------------------------------------------------------------------
public function testReallyWritable()
{
// cannot test fully on *nix
$this->assertTrue(is_really_writable(WRITEPATH));
}
// ------------------------------------------------------------------------
public function testSlashItem()
{
$this->assertEquals('/', slash_item('cookiePath')); // slash already there
$this->assertEquals('', slash_item('cookieDomain')); // empty, so untouched
$this->assertEquals('en/', slash_item('defaultLocale')); // slash appended
}
}