PHP code example of gong023 / assert_chain
1. Go to this page and download the library: Download gong023/assert_chain library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gong023 / assert_chain example snippets
class YourTestClass extends \PHPUnit_Framework_TestCase
{
use AssertChain\AssertChain;
}
public function testWithArray()
{
$arr = [
'intKey' => 1,
'stringKey' => 'foo',
'boolKey' => true,
];
$this->assert()
->notEmpty($arr)
->arrayHasKey('intKey', $arr)
->same(1, $arr['intKey'])
->arrayHasKey('stringKey', $arr)
->same('foo', $arr['stringKey'])
->arrayHasKey('boolKey', $arr)
->true($arr['boolKey']);
/*
* above code is equal to this.
*
* $this->assertNotEmpty($arr);
* $this->assertArrayHasKey('intKey', $arr);
* $this->assertSame(1, $arr['intKey']);
* $this->assertArrayHasKey('stringKey', $arr);
* $this->assertSame('foo', $arr['stringKey']);
* $this->assertArrayHasKey('boolKey', $arr);
* $this->assertTrue($arr['boolKey']);
*/
}
public function testWithArray()
{
$arr = ['key' => 'value'];
$this->centralizedAssert($arr)
->notNull()
->notEmpty()
->notCount(0)
->count(1)
->arrayNotHasKey('no existing key')
->arrayHasKey('key')
->notContains('no existing value')
->contains('value')
->equals(['key' => 'value']);
/*
* above code is equal to this.
*
* $this->assertNotNull($arr);
* $this->assertNotEmpty($arr);
* $this->assertNotCount(0, $arr);
* $this->assertCount(1, $arr);
* $this->assertArrayNotHasKey('no existing key', $arr);
* $this->assertNotContains('no existing value', $arr);
* $this->assertContains('value', $arr);
* $this->assertEquals(['key' => 'value']);
*/
}