<?php use Physalis\Spec; use Physalis\Expectation; describe('toMatch', function () { $do; beforeEach(function () use(&$do) { global $coreMatchers; $do = new Expectation($coreMatchers); }); it('matches string with regex', function () use(&$do) { expect($do->expect('aBBa')->toMatch('/ab/i'))->toBe(true); }); it('only matches against strings', function () use(&$do) { expect($do->expect(new Exception())->toMatch('/Exception/'))->toBe(false); expect($do->expect(42)->toMatch('/42/'))->toBe(false); expect($do->expect([4, 2])->toMatch('/\\[4, 2\\]/'))->toBe(false); }); });
<?php use Physalis\Spec; use Physalis\Expectation; describe('toContain', function () { $do; beforeEach(function () use(&$do) { global $coreMatchers; $do = new Expectation($coreMatchers); }); it('matches when array contains element', function () use(&$do) { expect($do->expect([42])->toContain(42))->toBe(true); }); it('matches when string contains substring', function () use(&$do) { expect($do->expect('beginning middle end')->toContain('middle'))->toBe(true); expect($do->expect('beginningmiddleend')->toContain('middle'))->toBe(true); }); it('doesnt match an empty array', function () use(&$do) { expect($do->expect([])->toContain(42))->toBe(false); }); it('doesnt match an empty string', function () use(&$do) { expect($do->expect('')->toContain('middle'))->toBe(false); }); });
<?php use Physalis\Spec; use Physalis\Expectation; describe('toEqual - tests if equal', function () { $do; beforeEach(function () use(&$do) { global $coreMatchers; $do = new Expectation($coreMatchers); }); it('numeric', function () use(&$do) { expect($do->expect(1)->toEqual(1))->toBe(true); expect($do->expect('1')->toEqual(1))->toBe(true); expect($do->expect(1)->toEqual(2))->toBe(false); }); it('boolean', function () use(&$do) { expect($do->expect(true)->toEqual(true))->toBe(true); expect($do->expect(1)->toEqual(true))->toBe(true); expect($do->expect(false)->toEqual(false))->toBe(true); expect($do->expect(0)->toEqual(false))->toBe(true); expect($do->expect(null)->toEqual(false))->toBe(true); expect($do->expect(true)->toEqual(false))->toBe(false); }); it('array', function () use(&$do) { expect($do->expect([])->toEqual([]))->toBe(true); expect($do->expect([1, 2, 3])->toEqual([1, 2, 3]))->toBe(true); expect($do->expect([1, 2, 3])->toEqual([2, 1, 3]))->toBe(false); }); it('object', function () use(&$do) { class BasicObject {
return false; }]; $expectation = new Expectation($example); $expectation->expect()->toHaveCustomMessage(); $messages = $expectation->message(); expect($messages[0])->toBe('regular message'); expect($messages[1])->toBe('inverted message'); }); }); it('can be inverted', function () { $example = ['toPass' => function () { return true; }, 'toFail' => function () { return false; }]; $do = new Expectation($example); expect($do->expect()->not->toPass())->toBe(false); expect($do->expect()->not->toFail())->toBe(true); }); describe('pretty-prints', function () { it('strings', function () { expect(Expectation::pp('hello world'))->toBe("'hello world'"); expect(Expectation::pp('42'))->toBe("'42'"); }); it('numbers', function () { expect(Expectation::pp(42))->toBe('42'); }); it('arrays', function () { expect(Expectation::pp([]))->toBe('[]'); expect(Expectation::pp([1, 2, 3]))->toBe('[1, 2, 3]'); expect(Expectation::pp([123, 'abc']))->toBe("[123, 'abc']");
<?php use Physalis\Spec; use Physalis\Expectation; describe('toBeOfType', function () { $do; beforeEach(function () use(&$do) { global $coreMatchers; $do = new Expectation($coreMatchers); }); it('knows about strings', function () use(&$do) { expect($do->expect('a string')->toBeOfType('string'))->toBe(true); $messages = $do->message(); expect($messages[0])->toBe("Expected 'a string' to be of type <string>"); expect($messages[1])->toBe("Expected 'a string' not to be of type <string>"); }); it('knows about integers', function () use(&$do) { expect($do->expect(123)->toBeOfType('integer'))->toBe(true); $messages = $do->message(); expect($messages[0])->toBe("Expected 123 to be of type <integer>"); expect($messages[1])->toBe("Expected 123 not to be of type <integer>"); }); it('knows about doubles', function () use(&$do) { expect($do->expect(4.56)->toBeOfType('double'))->toBe(true); $messages = $do->message(); expect($messages[0])->toBe("Expected 4.56 to be of type <double>"); expect($messages[1])->toBe("Expected 4.56 not to be of type <double>"); }); it('knows about arrays', function () use(&$do) { expect($do->expect([7, 8, 9])->toBeOfType('array'))->toBe(true); $messages = $do->message();
<?php use Physalis\Spec; use Physalis\Expectation; describe('toThrow', function () { $do; beforeEach(function () use(&$do) { global $coreMatchers; $do = new Expectation($coreMatchers); }); class AnException extends Exception { } it('reports failure for non-callback actual', function () use(&$do) { expect($do->expect(true)->toThrow('AnException'))->toBe(false); }); it('reports success when expected exception is thrown', function () use(&$do) { expect($do->expect(function () { throw new AnException(); })->toThrow('AnException'))->toBe(true); }); it('supports unspecified exceptions', function () use(&$do) { expect($do->expect(function () { throw new Exception(); })->toThrow())->toBe(true); }); it('reports failure when expecting an exception and no exception is thrown', function () use(&$do) { expect($do->expect(function () { })->toThrow('AnException'))->toBe(false); }); it('reports failure when actual exception differs from expected', function () use(&$do) {
<?php use Physalis\Spec; use Physalis\Expectation; describe('toBeEmpty', function () { $do; beforeEach(function () use(&$do) { global $coreMatchers; $do = new Expectation($coreMatchers); }); it('matches an empty array', function () use(&$do) { expect($do->expect([])->toBeEmpty())->toBe(true); }); it('matches an empty string', function () use(&$do) { expect($do->expect('')->toBeEmpty())->toBe(true); }); it('doesnt match an array with an element', function () use(&$do) { expect($do->expect([1])->toBeEmpty())->toBe(false); }); it('doesnt match a non-empty string', function () use(&$do) { expect($do->expect('i have chars')->toBeEmpty())->toBe(false); }); });