Unit Testing¶
Example Commands¶
vendor/bin/phpunit --version
vendor/bin/phpunit --configuration="phspark\phpunit.xml" --testsuite backend-tests
vendor/bin/phpunit --configuration="phspark\phpunit.xml" --testsuite frontend-tests
vendor/bin/phpunit --configuration="phspark\phpunit.xml" --testsuite backend-tests --filter testLoggedInCall
vendor/bin/phpunit --configuration="phspark\phpunit.xml" ./tests/backend/CommonBackendTest.php --filter testHtmlEscape
vendor/bin/phpunit --configuration="phspark\phpunit.xml" ./tests/backend/ConfigBackendTest.php --filter testSetItem
vendor/bin/phpunit --configuration="phspark\phpunit.xml" ./_backend/extension/module/marketplace/ --filter testLoggedInCall
vendor/bin/phpunit --configuration="phspark\phpunit.xml" ./_backend/extension/module/marketplace/tests/MarketplaceBackendTest.php --filter testBackendController
Backend Test¶
Create a file: tests/backend/MyBackendTest.php
<?php
namespace Tests;
use Src\Tests\PSUnitTestCase;
final class CommonBackendTest extends PSUnitTestCase
{
public function testHtmlEscape()
{
$this->assertEquals(
html_escape('Here is a string containing "quoted" text.'),
'Here is a string containing "quoted" text.'
);
$this->assertEquals(
html_escape(array('associative' => 'and', array('multi' => 'dimentional'))),
array('associative' => 'and', array('multi' => 'dimentional'))
);
}
}
Frontend Test¶
Create a file: tests/frontend/MyTest.php
<?php
namespace Tests;
use Src\Tests\PSUnitTestCase;
class MyFrontendTest extends PSUnitTestCase
{
public function testSeoUrl()
{
$model = $this->loadModel("seourl");
$seo_url = $model->get(1);
$this->assertEquals('api-v1-extension-purchase', $seo_url['keyword']);
}
}
Extension Test¶
Create a file: _backend/extension/[module]/[marketpalce]/tests/[MarketplaceBackendTest.php]
<?php
namespace Test;
use Src\Tests\PSUnitTestCase;
class MarketplaceBackendTest extends PSUnitTestCase
{
public function testLoggedInCall()
{
$this->login('test@gmail.com', 'foobar'); // username, password
$response = $this->dispatchAction('extension/module/marketplace/extension/index');
$this->assertMatchesRegularExpression('/Extensions/', $response->getOutput());
$this->logout();
}
public function testIsBackend()
{
$this->assertTrue($this->isBackend());
}
public function testBackendController()
{
$response = $this->dispatchAction('dashboard/index');
$this->assertMatchesRegularExpression('/Login/', $response->getOutput());
}
}
Helper Methods¶
<?php
$this->assertMatchesRegularExpression('/Extension Marketplace/', $response->getOutput());
$this->expectException('Phspark\Encryption\Exceptions\EncryptionException');
$this->expectExceptionMessage('Unable to connect to the database.');
$this->assertNotEmpty($this->encryption->createKey());
$this->assertEquals(32, strlen($this->encryption->createKey()));
$this->assertInstanceOf(EncrypterInterface::class, $encrypter);
$this->assertEquals('anything', $encrypter->key);
$this->assertEquals(['/peanuts'], $this->manager->getFiles());
$this->assertEquals([$callback2, $callback1], Events::listeners('foo'));
$this->assertEquals(['c', 'd', 'a', 'b'], $result);
$this->assertSame(123, $db->getConnection());
$this->assertTrue(isset($this->encryption->digest));
$this->assertFalse(isset($this->encryption->bogus));
$this->assertNull($this->encryption->bogus);
$this->assertNotNull($pager->getNextPage());
$this->assertGreaterThan(0, count($logged));
$this->assertGreaterThan(0.0, $db->getConnectDuration());
$this->assertCount(1, $timers, 'No timers were stored.');
$this->assertArrayHasKey('test1', $timers, 'No "test1" array found.');
$this->assertArrayNotHasKey('__ci_vars', $_SESSION);
$this->assertIsArray($cookies[0][2]);
$this->assertGreaterThanOrEqual(1.0, $timers['test1']['duration']);
$this->assertLessThanOrEqual($_SESSION['__ci_vars']['foo'], $time + 300);
$this->assertCloseEnough(11 * 60, $timer->getElapsedTime('longjohn'));
$this->assertContains('foo', $keys);
$this->assertNotContains('bar', $keys);
$this->assertStringNotContainsString('samesite', $cookies[0][3]);
assertIsArray()
assertIsBool()
assertIsFloat()
assertIsInt()
assertIsNumeric()
assertIsObject()
assertIsResource()
assertIsString()
assertIsScalar()
assertIsCallable()
assertIsIterable()
assertIsNotArray()
assertIsNotBool()
assertIsNotFloat()
assertIsNotInt()
assertIsNotNumeric()
assertIsNotObject()
assertIsNotResource()
assertIsNotString()
assertIsNotScalar()
assertIsNotCallable()
assertIsNotIterable()