Simple Assertions
Assertions are organized into packages (and sub-packages) and assertion calls. In the following example there are two packages php, php/version and one call atLeast():
<?php class LogTest extends PhpRack_Test { public function testLogFileIsWritable() { $logFile = '/home/product/errors.log'; if (file_exists($logFile)) { $this->_log("Log file '{$logFile}' exists: " . filesize($logFile) . " bytes"); $this->assert->success("Log file is OK"); } else { $this->assert->fail("Log file '{$logFile}' is absent"); } } }
There is a full reference of assertion packages available (if you want to add your own package, email us and we give you an access to SVN repository).
PHP assertions
<?php class PhpConfigurationIsValidTest extends PhpRack_Test { public function testPhpVersionIsCorrect() { // version of PHP is equal or higher than 5.2 $this->assert->php->version ->atLeast('5.2'); } public function testPhpExtensionsAreValid() { // validate the all required PHP extensions are loaded $this->assert->php->extensions ->isLoaded('simplexml') ->isLoaded('fileinfo') ->isLoaded('xsl'); // validate that fileinfo is loaded and configured properly $this->assert->php->extensions->fileinfo->isAlive() } public function testViewPhpinfo() { // show plain-text version of phpinfo() $this->assert->php ->phpinfo(); } public function testPhpLint() { $options = array( 'extensions' => 'php,phtml', 'exclude' => '/\.svn/', ); // lint validation of all files in the directory $this->assert->php->lint('/../../application', $options); } public function testPear() { $this->assert->php->pear ->showList() // show full list of available PEAR packages ->package('phing/phing')->atLeast('2.4.1') // it exists and the version is at least 2.4.1 ->package('pear.phpunit.de/PHPUnit')->exactly('3.4.12') // in this exact version ->package('VersionControl_SVN') // make sure it exists ->package('HTTP_Request2'); } public function testPhpIni() { $this->assert->php ->ini('short_open_tag') // make sure it is set to TRUE in php.ini ->ini('memory_limit')->atLeast('128M'); // at least 128M is set for memory_limit } public function testPhpFunctions() { $this->assert->php ->fnExists('lcfirst') // validate that lcfirst() exists ->fnExists('imagejpeg'); // validate another function, etc. } }
Shell-related assertions
<?php class MyTest extends PhpRack_Test { public function testWhoAmI() { // validate the current user is "apache" $this->assert->shell->exec('whoami', '/apache/'); } }
Network assertions
<?php class NetworkIsAccessibleTest extends PhpRack_Test { public function testPortsAreOpen() { // the port is open and accessible $this->assert->network->ports ->isOpen(80, 'aws.amazon.com'); // incoming port is open $this->assert->network->ports ->isOpen(80); } public function testUrlIsAccessible() { // validate that the URL is accessible $this->assert->network->url ->url('http://www.google.com') // set URL (and validate it here) ->regex('/google\.com/'); // make HTTP call and find pattern in result } }
Disc assertions
<?php class FilesTest extends PhpRack_Test { public function testShowFiles() { // show directory structure $this->assert->disc ->showDirectory( '/home/product', // list all files in this directory and beneath array( 'exclude' => array( // exclude files that match this pattern(s) '/\.svn/', ), 'maxDepth' => 1, // maximum directory depth to show ) ); } public function testFreeSpace() { // test that we have enough free disc space $this->assert->disc->freeSpace ->atLeast(10); // 10 Mb at least } public function testSingleFile() { $file = '../../test.log'; $this->assert->disc->file ->cat($file) // show full content of the file ->head($file, 10) // show 10 first lines of the file ->tail($file, 5) // show 5 last lines of the file ->exists($file) // OK if file exists ->isReadable($file) // OK if this file is readable ->isWritable($file) // OK if this file is writable (has enough permissions) ->isDir($file); // OK if it's a directory } public function testIncrementalLogView() { // in AJAX web front this assertion will show a file, keeping its latest 25 lines visible // and guaranteeing that any lines stays visible for at least 10 seconds $this->assert->disc->file ->tailf('../my-log.txt', 25, 10); } }
Database assertions
<?php class DatabaseTest extends PhpRack_Test { public function testConnectionIsAlive() { // we validate that the DB is accessible $this->assert->db->mysql ->connect($host, $port, $username, $password) // we can connect ->dbExists($dbname) // DB exists ->tableExists('user'); // table "user" exists } public function testViewDatabaseSchema() { // we can use application-specific INI file, with "production" section inside $ini = new phpRack_Adapters_Config_Ini(APPLICATION_PATH . '/config/app.ini', 'production'); $this->assert->db->mysql ->connect( $ini->resources->db->params->host, $ini->resources->db->params->port, $ini->resources->db->params->username, $ini->resources->db->params->password ) ->dbExists($ini->resources->db->params->dbname) ->showSchema() // show entire DB schema ->showConnections(); // show full list of currently open connections } public function testViewSomeData() { $this->assert->db->mysql ->connect($host, $port, $username, $password) ->query('SELECT * FROM user LIMIT 5'); } }
Quality-Of-Service assertions
<?php class QosTest extends PhpRack_Test { public function testLatency() { $this->assert->qos->latency( array( 'scenario' => array( // list of URL-s to test 'http://www.example.com', ), 'averageMs' => 500, // 500ms average per request 'peakMs' => 2000, // 2s maximum per request ), ); } }
