PHP code example of sirbrillig / corretto
1. Go to this page and download the library: Download sirbrillig/corretto 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/ */
sirbrillig / corretto example snippets
use function Corretto\describe, Corretto\it, Corretto\expect;
describe( 'isFive()', function() {
it( 'returns true if its argument is five', function() {
expect( isFive( 5 ) )->toBeTrue();
} );
it( 'returns false if its argument is not five', function() {
expect( isFive( 6 ) )->toBeFalse();
} );
} );
class FooExpectation extends Corretto\Expectation {
public function toBeFoo() {
if ( ! $this->actual === 'foo' ) {
throw new \Exception( 'not foo' );
}
}
}
Corretto\extendExpectation( 'FooExpectation' );
test( 'string is "foo"', function() {
$string = 'foo';
expect( $string )->toBeFoo();
} );
it( 'does something', function() {
...
} );
it( 'does something not yet defined' );
it( 'SKIP', 'does something we should not run', function() {
...
} );
describe( 'some tests to run', function() {
test( '...' );
test( '...' );
test( '...' );
} );
describe( 'MyObject', function() {
describe( 'getName()', function() {
describe( 'when the name is missing', function() {
it( 'returns a default name', function() {
$obj = new MyObject();
expect( $obj->getName() )->toEqual( 'default' );
} );
} );
describe( 'when the name is set', function() {
it( 'returns the name', function() {
$obj = new MyObject( 'name' );
expect( $obj->getName() )->toEqual( 'name' );
} );
} );
} );
} );
describe( 'SKIP', 'some tests not to run', function() {
...
} );
describe( 'MyObject', function() {
$ctx = new \StdClass();
beforeEach( function() use ( &$ctx ) {
$ctx->color = 'blue';
} );
describe( 'getName()', function() use ( &$ctx ) {
beforeEach( function() use ( &$ctx ) {
$ctx->obj = new MyObject();
} );
it( 'returns a default name when the name is missing', function() use ( &$ctx ) {
expect( $ctx->obj->getName() )->toEqual( 'default' );
} );
it( 'returns the name', function() use ( &$ctx ) {
$ctx->obj->name = 'name';
expect( $ctx->obj->getName() )->toEqual( 'name' );
} );
it( 'returns a name matching the color', function() use ( &$ctx ) {
$ctx->obj->name = $ctx->color;
expect( $ctx->obj->getName() )->toEqual( $ctx->color );
} );
} );
} );
describe( 'MyObject', function() {
beforeEach( function( $ctx ) {
$ctx->color = 'blue';
} );
describe( 'getName()', function() {
beforeEach( function( $ctx ) {
$ctx->obj = new MyObject();
} );
it( 'returns a default name when the name is missing', function( $ctx ) {
expect( $ctx->obj->getName() )->toEqual( 'default' );
} );
it( 'returns the name', function( $ctx ) {
$ctx->obj->name = 'name';
expect( $ctx->obj->getName() )->toEqual( 'name' );
} );
it( 'returns a name matching the color', function( $ctx ) {
$ctx->obj->name = $ctx->color;
expect( $ctx->obj->getName() )->toEqual( $ctx->color );
} );
} );
} );
use function Corretto\describe, Corretto\it;
use function Corretto\assertTrue, Corretto\assertFalse, Corretto\assertEquals, Corretto\assertNotEquals;
use function Corretto\test, Corretto\suite;
use function Corretto\specify, Corretto\context;
use function Corretto\beforeEach, Corretto\afterEach, Corretto\before, Corretto\after;
use function Corretto\expect;
it( 'allows tests outside a suite', function() {
assertTrue( true );
} );
test( 'tests can use "test" as well as "it"', function() {
assertTrue( true );
} );
specify( 'tests can use "specify" as well as "it"', function() {
assertTrue( true );
} );
describe( 'describe()', function() {
describe( 'when nested', function() {
describe( 'more than once', function() {
it( 'passes if its argument is true', function() {
assertTrue( true );
} );
} );
it( 'skips tests with no function' );
it( 'SKIP', 'skips tests with the SKIP string as the first argument', function() {
assertTrue( false );
} );
it( 'passes if its argument is true', function() {
assertTrue( true );
} );
} );
it( 'supports non-nested tests along with nested ones', function() {
assertTrue( true );
} );
describe( 'when multiple tests are nested at the same level', function() {
it( 'passes if its argument is true', function() {
assertTrue( true );
} );
} );
describe( 'SKIP', 'allows skipping whole suites', function() {
it( 'passes if its argument is true', function() {
assertTrue( false );
} );
} );
} );
context( 'a bunch of tests', function() {
specify( 'suites can use "context" as well as "describe"', function() {
assertTrue( true );
} );
} );
suite( 'my tests', function() {
test( 'suites can use "suite" as well as "describe"', function() {
assertTrue( true );
} );
suite( 'there are many assertions', function() {
test( 'assertEquals()', function() {
$actual = 'expected';
assertEquals( 'expected', $actual );
} );
test( 'assertNotEquals()', function() {
$actual = 'actual';
assertNotEquals( 'expected', $actual );
} );
test( 'assertTrue()', function() {
assertTrue( true );
} );
test( 'assertFalse()', function() {
assertFalse( false );
} );
} );
suite( 'expectation syntax also works for assertions', function() {
suite( 'expect()', function() {
test( '->toBeTrue()', function() {
expect( true )->toBeTrue();
} );
test( '->toBeFalse()', function() {
expect( false )->toBeFalse();
} );
test( '->toEqual()', function() {
expect( 'hi' )->toEqual( 'hi' );
} );
test( '->toNotEqual()', function() {
expect( 'hi' )->toNotEqual( 'bye' );
} );
} );
} );
} );
describe( 'set up and tear down', function() {
$ctx = new \StdClass();
describe( 'beforeEach()', function() use ( &$ctx ) {
beforeEach( function() use ( &$ctx ) {
$ctx->name = 'hello';
} );
it( 'sets up the test context', function() use ( &$ctx ) {
expect( $ctx->name )->toEqual( 'hello' );
$ctx->name = 'bye';
} );
it( 'runs again before each test', function() use ( &$ctx ) {
expect( $ctx->name )->toNotEqual( 'bye' );
} );
} );
describe( 'before()', function() use ( &$ctx ) {
before( function() use ( &$ctx ) {
$ctx->name = 'hello';
} );
it( 'sets up the test context', function() use ( &$ctx ) {
expect( $ctx->name )->toEqual( 'hello' );
$ctx->name = 'bye';
} );
it( 'runs only once before the suite runs', function() use ( &$ctx ) {
expect( $ctx->name )->toEqual( 'bye' );
} );
} );
describe( 'afterEach()', function() {
$name = 'hello';
afterEach( function() use ( &$name ) {
$name = 'bye';
} );
it( 'is run after each test', function() use( &$name ) {
expect( $name )->toEqual( 'hello' );
} );
it( 'runs again after each test', function() use ( &$name ) {
expect( $name )->toEqual( 'bye' );
} );
} );
$name = 'hello';
describe( 'after()', function() use( &$name ) {
after( function() use ( &$name ) {
$name = 'bye';
} );
it( 'is run after all tests in a suite', function() use( &$name ) {
expect( $name )->toEqual( 'hello' );
} );
} );
describe( 'after() (continued)', function() use( &$name ) {
it( 'is run at the end of a suite', function() use ( &$name ) {
expect( $name )->toEqual( 'bye' );
} );
} );
} );
composer global