feat(expect): updates test suite to use expectation api

This commit is contained in:
Nuno Maduro
2020-07-14 23:15:14 +02:00
parent e03d015120
commit 832882160f
37 changed files with 151 additions and 257 deletions

View File

@ -3,7 +3,6 @@
$finder = PhpCsFixer\Finder::create() $finder = PhpCsFixer\Finder::create()
->in(__DIR__ . DIRECTORY_SEPARATOR . 'tests') ->in(__DIR__ . DIRECTORY_SEPARATOR . 'tests')
->in(__DIR__ . DIRECTORY_SEPARATOR . 'bin') ->in(__DIR__ . DIRECTORY_SEPARATOR . 'bin')
->in(__DIR__ . DIRECTORY_SEPARATOR . 'compiled')
->in(__DIR__ . DIRECTORY_SEPARATOR . 'scripts') ->in(__DIR__ . DIRECTORY_SEPARATOR . 'scripts')
->in(__DIR__ . DIRECTORY_SEPARATOR . 'stubs') ->in(__DIR__ . DIRECTORY_SEPARATOR . 'stubs')
->in(__DIR__ . DIRECTORY_SEPARATOR . 'src') ->in(__DIR__ . DIRECTORY_SEPARATOR . 'src')

View File

@ -62,6 +62,16 @@ final class Expectation
return $this; return $this;
} }
/**
* Assert the value is true.
*/
public function toBeTrue(): Expectation
{
Assert::assertTrue($this->value);
return $this;
}
/** /**
* Assert the value is false. * Assert the value is false.
*/ */
@ -123,54 +133,15 @@ final class Expectation
/** /**
* Assert that needles is an element of value. * Assert that needles is an element of value.
* *
* @param mixed $needle * @param mixed $value
*/ */
public function toContain($needle): Expectation public function toContain($value): Expectation
{ {
Assert::assertContains($needle, $this->value); if (is_string($value)) {
Assert::assertStringContainsString($value, $this->value);
return $this; } else {
} Assert::assertContains($value, $this->value);
}
/**
* Assert the value contains only variables of type.
*
* @param mixed $type
*/
public function toContainOnly($type): Expectation
{
Assert::assertContainsOnly($type, $this->value);
return $this;
}
/**
* Assert the value contains only instances of $instance.
*/
public function toContainOnlyInstancesOf(string $instance): Expectation
{
Assert::assertContainsOnlyInstancesOf($instance, $this->value);
return $this;
}
/**
* Assert that needles is a substring of value.
*/
public function toContainString(string $needle): Expectation
{
Assert::assertStringContainsString($needle, $this->value);
return $this;
}
/**
* Assert that needles is a substring of value, ignoring the
* difference in casing.
*/
public function toContainStringIgnoringCase(string $needle): Expectation
{
Assert::assertStringContainsStringIgnoringCase($needle, $this->value);
return $this; return $this;
} }
@ -178,7 +149,7 @@ final class Expectation
/** /**
* Assert that $count matches the number of elements of $value. * Assert that $count matches the number of elements of $value.
*/ */
public function toCount(int $count): Expectation public function toHaveCount(int $count): Expectation
{ {
Assert::assertCount($count, $this->value); Assert::assertCount($count, $this->value);
@ -234,7 +205,7 @@ final class Expectation
* *
* @param mixed $value * @param mixed $value
*/ */
public function toEqualWithDelta($value, float $delta): Expectation public function toBeEqualWithDelta($value, float $delta): Expectation
{ {
Assert::assertEqualsWithDelta($value, $this->value, $delta); Assert::assertEqualsWithDelta($value, $this->value, $delta);
@ -254,11 +225,12 @@ final class Expectation
/** /**
* Assert that the value is an instance of $value. * Assert that the value is an instance of $value.
* *
* @param mixed $value * @param string $class
*/ */
public function toBeInstanceOf($value): Expectation public function toBeInstanceOf($class): Expectation
{ {
Assert::assertInstanceOf($value, $this->value); /* @phpstan-ignore-next-line */
Assert::assertInstanceOf($class, $this->value);
return $this; return $this;
} }
@ -384,7 +356,7 @@ final class Expectation
} }
/** /**
* Assert that the value is NAN. * Assert that the value is null.
*/ */
public function toBeNull(): Expectation public function toBeNull(): Expectation
{ {

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pest\PendingObjects; namespace Pest\PendingObjects;
use Closure; use Closure;
use Pest\Expectation;
use Pest\Factories\TestCaseFactory; use Pest\Factories\TestCaseFactory;
use Pest\Support\Backtrace; use Pest\Support\Backtrace;
use Pest\Support\NullClosure; use Pest\Support\NullClosure;
@ -84,6 +85,16 @@ final class TestCall
return $this; return $this;
} }
/**
* Creates a new expectation.
*
* @param mixed $value the Value
*/
public function expect($value): Expectation
{
return expect($value);
}
/** /**
* Sets the test depends. * Sets the test depends.
*/ */

View File

@ -108,6 +108,8 @@ function afterAll(Closure $closure = null): void
/** /**
* Creates a new expectation. * Creates a new expectation.
*
* @param mixed $value the Value
*/ */
function expect($value): Expectation function expect($value): Expectation
{ {

View File

@ -110,31 +110,16 @@
PASS Tests\Expect\toBeString PASS Tests\Expect\toBeString
✓ pass ✓ pass
✓ failures ✓ failures
✓ not failures
PASS Tests\Expect\toBeTrue
✓ strict comparisons
✓ failures
✓ not failures ✓ not failures
PASS Tests\Expect\toContain PASS Tests\Expect\toContain
✓ passes ✓ passes
✓ failures ✓ failures
✓ not failures
PASS Tests\Expect\toContainOnly
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toContainOnlyInstancesOf
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toContainString
✓ is case sensitive
✓ failures
✓ not failures
PASS Tests\Expect\toContainStringIgnoringCase
✓ ignore difference in casing
✓ failures
✓ not failures ✓ not failures
PASS Tests\Expect\toCount PASS Tests\Expect\toCount
@ -231,9 +216,9 @@
PASS Tests\Features\HigherOrderTests PASS Tests\Features\HigherOrderTests
✓ it proxies calls to object ✓ it proxies calls to object
PASS Tests\Features\It WARN Tests\Features\It
✓ it is a test ✓ it is a test
it is a higher order message test ! it is a higher order message test → This test did not perform any assertions /Users/nunomaduro/pestphp/pest/src/Factories/TestCaseFactory.php(204) : eval()'d code:4
PASS Tests\Features\Macro PASS Tests\Features\Macro
✓ it can call chained macro method ✓ it can call chained macro method
@ -243,8 +228,8 @@
✓ it has bar ✓ it has bar
PASS Tests\Features\PendingHigherOrderTests PASS Tests\Features\PendingHigherOrderTests
✓ get 'foo' → get 'bar' → assertTrue true ✓ get 'foo' → get 'bar'
✓ get 'foo' → assertTrue true ✓ get 'foo'
WARN Tests\Features\Skip WARN Tests\Features\Skip
✓ it do not skips ✓ it do not skips
@ -255,9 +240,9 @@
✓ it do not skips with falsy closure condition ✓ it do not skips with falsy closure condition
- it skips with condition and message → skipped because foo - it skips with condition and message → skipped because foo
PASS Tests\Features\Test WARN Tests\Features\Test
✓ a test ✓ a test
higher order message test ! higher order message test → This test did not perform any assertions /Users/nunomaduro/pestphp/pest/src/Factories/TestCaseFactory.php(204) : eval()'d code:4
PASS Tests\Fixtures\DirectoryWithTests\ExampleTest PASS Tests\Fixtures\DirectoryWithTests\ExampleTest
✓ it example 1 ✓ it example 1
@ -327,5 +312,5 @@
WARN Tests\Visual\Success WARN Tests\Visual\Success
- visual snapshot of test suite on success - visual snapshot of test suite on success
Tests: 6 skipped, 192 passed Tests: 2 risked, 6 skipped, 181 passed
Time: 5.20s Time: 5.72s

View File

@ -8,7 +8,7 @@ trait PluginTrait
{ {
public function assertPluginTraitGotRegistered(): void public function assertPluginTraitGotRegistered(): void
{ {
assertTrue(true); $this->assertTrue(true);
} }
} }
@ -16,7 +16,7 @@ trait SecondPluginTrait
{ {
public function assertSecondPluginTraitGotRegistered(): void public function assertSecondPluginTraitGotRegistered(): void
{ {
assertTrue(true); $this->assertTrue(true);
} }
} }

15
tests/Expect/toBeTrue.php Normal file
View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('strict comparisons', function () {
expect(true)->toBeTrue();
});
test('failures', function () {
expect('')->toBeTrue();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(false)->not->toBe(false);
})->throws(ExpectationFailedException::class);

View File

@ -1,17 +0,0 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect([1, 2, 3])->toContainOnly('int');
expect(['hello', 'world'])->toContainOnly('string');
});
test('failures', function () {
expect([1, 2, '3'])->toContainOnly('string');
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect([1, 2, 3])->not->toContainOnly('int');
expect(['hello', 'world'])->not->toContainOnly('string');
})->throws(ExpectationFailedException::class);

View File

@ -1,23 +0,0 @@
<?php
use Pest\Actions\AddsTests;
use Pest\Expectation;
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
$expected = [new Expectation('whatever')];
expect($expected)->toContainOnlyInstancesOf(Expectation::class);
});
test('failures', function () {
$expected = [new Expectation('whatever')];
expect($expected)->toContainOnlyInstancesOf(AddsTests::class);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
$expected = [new Expectation('whatever')];
expect($expected)->not->toContainOnlyInstancesOf(Expectation::class);
})->throws(ExpectationFailedException::class);

View File

@ -1,16 +0,0 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('is case sensitive', function () {
expect('hello world')->toContainString('world');
expect('hello world')->not->toContainString('World');
});
test('failures', function () {
expect('hello world')->toContainString('Hello');
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('hello world')->not->toContainString('hello');
})->throws(ExpectationFailedException::class);

View File

@ -1,17 +0,0 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('ignore difference in casing', function () {
expect('hello world')->toContainStringIgnoringCase('world');
expect('hello world')->toContainStringIgnoringCase('World');
});
test('failures', function () {
expect('hello world')->toContainStringIgnoringCase('hi');
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('hello world')->not->toContainStringIgnoringCase('Hello');
expect('hello world')->not->toContainStringIgnoringCase('hello');
})->throws(ExpectationFailedException::class);

View File

@ -3,13 +3,13 @@
use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () { test('pass', function () {
expect([1, 2, 3])->toCount(3); expect([1, 2, 3])->toHaveCount(3);
}); });
test('failures', function () { test('failures', function () {
expect([1, 2, 3])->toCount(4); expect([1, 2, 3])->toHaveCount(4);
})->throws(ExpectationFailedException::class); })->throws(ExpectationFailedException::class);
test('not failures', function () { test('not failures', function () {
expect([1, 2, 3])->not->toCount(3); expect([1, 2, 3])->not->toHaveCount(3);
})->throws(ExpectationFailedException::class); })->throws(ExpectationFailedException::class);

View File

@ -3,13 +3,13 @@
use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () { test('pass', function () {
expect(1.0)->toEqualWithDelta(1.3, .4); expect(1.0)->toBeEqualWithDelta(1.3, .4);
}); });
test('failures', function () { test('failures', function () {
expect(1.0)->toEqualWithDelta(1.5, .1); expect(1.0)->toBeEqualWithDelta(1.5, .1);
})->throws(ExpectationFailedException::class); })->throws(ExpectationFailedException::class);
test('not failures', function () { test('not failures', function () {
expect(1.0)->not->toEqualWithDelta(1.6, .7); expect(1.0)->not->toBeEqualWithDelta(1.6, .7);
})->throws(ExpectationFailedException::class); })->throws(ExpectationFailedException::class);

View File

@ -8,8 +8,8 @@ afterAll(function () use ($file) {
test('deletes file after all', function () use ($file) { test('deletes file after all', function () use ($file) {
file_put_contents($file, 'foo'); file_put_contents($file, 'foo');
assertFileExists($file); $this->assertFileExists($file);
register_shutdown_function(function () use ($file) { register_shutdown_function(function () use ($file) {
assertFileNotExists($file); $this->assertFileNotExists($file);
}); });
}); });

View File

@ -11,10 +11,10 @@ afterEach(function () use ($state) {
}); });
it('does not get executed before the test', function () { it('does not get executed before the test', function () {
assertFalse(property_exists($this->state, 'bar')); expect(property_exists($this->state, 'bar'))->toBeFalse();
}); });
it('gets executed after the test', function () { it('gets executed after the test', function () {
assertTrue(property_exists($this->state, 'bar')); expect(property_exists($this->state, 'bar'))->toBeTrue();
assertEquals(2, $this->state->bar); expect($this->state->bar)->toBe(2);
}); });

View File

@ -8,11 +8,11 @@ beforeAll(function () use ($foo) {
}); });
it('gets executed before tests', function () use ($foo) { it('gets executed before tests', function () use ($foo) {
assertEquals($foo->bar, 1); expect($foo->bar)->toBe(1);
$foo->bar = 'changed'; $foo->bar = 'changed';
}); });
it('do not get executed before each test', function () use ($foo) { it('do not get executed before each test', function () use ($foo) {
assertEquals($foo->bar, 'changed'); expect($foo->bar)->toBe('changed');
}); });

View File

@ -5,11 +5,11 @@ beforeEach(function () {
}); });
it('gets executed before each test', function () { it('gets executed before each test', function () {
assertEquals($this->bar, 2); expect($this->bar)->toBe(2);
$this->bar = 'changed'; $this->bar = 'changed';
}); });
it('gets executed before each test once again', function () { it('gets executed before each test once again', function () {
assertEquals($this->bar, 2); expect($this->bar)->toBe(2);
}); });

View File

@ -23,13 +23,13 @@ it('sets closures', function () {
yield [1]; yield [1];
}); });
assertEquals([[1]], iterator_to_array(Datasets::get('foo')())); expect(iterator_to_array(Datasets::get('foo')()))->toBe([[1]]);
}); });
it('sets arrays', function () { it('sets arrays', function () {
Datasets::set('bar', [[2]]); Datasets::set('bar', [[2]]);
assertEquals([[2]], Datasets::get('bar')); expect(Datasets::get('bar'))->toBe([[2]]);
}); });
it('gets bound to test case object', function () { it('gets bound to test case object', function () {
@ -37,7 +37,7 @@ it('gets bound to test case object', function () {
})->with([['a'], ['b']]); })->with([['a'], ['b']]);
test('it truncates the description', function () { test('it truncates the description', function () {
assertTrue(true); expect(true)->toBe(true);
// it gets tested by the integration test // it gets tested by the integration test
})->with([str_repeat('Fooo', 10000000)]); })->with([str_repeat('Fooo', 10000000)]);
@ -48,51 +48,51 @@ $datasets = [[1], [2]];
test('lazy datasets', function ($text) use ($state, $datasets) { test('lazy datasets', function ($text) use ($state, $datasets) {
$state->text .= $text; $state->text .= $text;
assertTrue(in_array([$text], $datasets)); expect(in_array([$text], $datasets))->toBe(true);
})->with($datasets); })->with($datasets);
test('lazy datasets did the job right', function () use ($state) { test('lazy datasets did the job right', function () use ($state) {
assertEquals('12', $state->text); expect($state->text)->toBe('12');
}); });
$state->text = ''; $state->text = '';
test('eager datasets', function ($text) use ($state, $datasets) { test('eager datasets', function ($text) use ($state, $datasets) {
$state->text .= $text; $state->text .= $text;
assertTrue(in_array([$text], $datasets)); expect($datasets)->toContain([$text]);
})->with(function () use ($datasets) { })->with(function () use ($datasets) {
return $datasets; return $datasets;
}); });
test('eager datasets did the job right', function () use ($state) { test('eager datasets did the job right', function () use ($state) {
assertEquals('1212', $state->text); expect($state->text)->toBe('1212');
}); });
test('lazy registered datasets', function ($text) use ($state, $datasets) { test('lazy registered datasets', function ($text) use ($state, $datasets) {
$state->text .= $text; $state->text .= $text;
assertTrue(in_array([$text], $datasets)); expect($datasets)->toContain([$text]);
})->with('numbers.array'); })->with('numbers.array');
test('lazy registered datasets did the job right', function () use ($state) { test('lazy registered datasets did the job right', function () use ($state) {
assertEquals('121212', $state->text); expect($state->text)->toBe('121212');
}); });
test('eager registered datasets', function ($text) use ($state, $datasets) { test('eager registered datasets', function ($text) use ($state, $datasets) {
$state->text .= $text; $state->text .= $text;
assertTrue(in_array([$text], $datasets)); expect($datasets)->toContain([$text]);
})->with('numbers.closure'); })->with('numbers.closure');
test('eager registered datasets did the job right', function () use ($state) { test('eager registered datasets did the job right', function () use ($state) {
assertEquals('12121212', $state->text); expect($state->text)->toBe('12121212');
}); });
test('eager wrapped registered datasets', function ($text) use ($state, $datasets) { test('eager wrapped registered datasets', function ($text) use ($state, $datasets) {
$state->text .= $text; $state->text .= $text;
assertTrue(in_array([$text], $datasets)); expect($datasets)->toContain([$text]);
})->with('numbers.closure.wrapped'); })->with('numbers.closure.wrapped');
test('eager registered wrapped datasets did the job right', function () use ($state) { test('eager registered wrapped datasets did the job right', function () use ($state) {
assertEquals('1212121212', $state->text); expect($state->text)->toBe('1212121212');
}); });
class Bar class Bar
@ -105,13 +105,13 @@ $namedDatasets = [
]; ];
test('lazy named datasets', function ($text) use ($state, $datasets) { test('lazy named datasets', function ($text) use ($state, $datasets) {
assertTrue(true); expect(true)->toBeTrue();
})->with($namedDatasets); })->with($namedDatasets);
$counter = 0; $counter = 0;
it('creates unique test case names', function (string $name, Plugin $plugin, bool $bool) use (&$counter) { it('creates unique test case names', function (string $name, Plugin $plugin, bool $bool) use (&$counter) {
assertTrue(true); expect(true)->toBeTrue();
$counter++; $counter++;
})->with([ })->with([
['Name 1', new Plugin(), true], ['Name 1', new Plugin(), true],
@ -123,5 +123,5 @@ it('creates unique test case names', function (string $name, Plugin $plugin, boo
]); ]);
it('creates unique test case names - count', function () use (&$counter) { it('creates unique test case names - count', function () use (&$counter) {
assertEquals(6, $counter); expect($counter)->toBe(6);
}); });

View File

@ -3,38 +3,32 @@
$runCounter = 0; $runCounter = 0;
test('first', function () use (&$runCounter) { test('first', function () use (&$runCounter) {
assertTrue(true); expect(true)->toBeTrue();
$runCounter++; $runCounter++;
return 'first'; return 'first';
}); });
test('second', function () use (&$runCounter) { test('second', function () use (&$runCounter) {
assertTrue(true); expect(true)->toBeTrue();
$runCounter++; $runCounter++;
return 'second'; return 'second';
}); });
test('depends', function () { test('depends', function () {
assertEquals( expect(func_get_args())->toBe(['first', 'second']);
['first', 'second'],
func_get_args()
);
})->depends('first', 'second'); })->depends('first', 'second');
test('depends with ...params', function (string ...$params) { test('depends with ...params', function (string ...$params) {
assertEquals( expect(func_get_args())->toBe($params);
['first', 'second'],
$params
);
})->depends('first', 'second'); })->depends('first', 'second');
test('depends with defined arguments', function (string $first, string $second) { test('depends with defined arguments', function (string $first, string $second) {
assertEquals('first', $first); expect($first)->toBe('first');
assertEquals('second', $second); expect($second)->toBe('second');
})->depends('first', 'second'); })->depends('first', 'second');
test('depends run test only once', function () use (&$runCounter) { test('depends run test only once', function () use (&$runCounter) {
assertEquals(2, $runCounter); expect($runCounter)->toBe(2);
})->depends('first', 'second'); })->depends('first', 'second');

View File

@ -7,7 +7,7 @@ function addUser()
it('can set/get properties on $this', function () { it('can set/get properties on $this', function () {
addUser(); addUser();
assertEquals('nuno', $this->user); expect($this->user)->toBe('nuno');
}); });
it('throws error if property do not exist', function () { it('throws error if property do not exist', function () {
@ -27,15 +27,14 @@ function mockUser()
$mock = test()->createMock(User::class); $mock = test()->createMock(User::class);
$mock->method('getName') $mock->method('getName')
->willReturn('maduro'); ->willReturn('maduro');
return $mock; return $mock;
} }
it('allows to call underlying protected/private methods', function () { it('allows to call underlying protected/private methods', function () {
$user = mockUser(); $user = mockUser();
expect($user->getName())->toBe('maduro');
assertEquals('maduro', $user->getName());
}); });
it('throws error if method do not exist', function () { it('throws error if method do not exist', function () {

View File

@ -1,7 +1,7 @@
<?php <?php
it('is a test', function () { it('is a test', function () {
assertArrayHasKey('key', ['key' => 'foo']); $this->assertArrayHasKey('key', ['key' => 'foo']);
}); });
it('is a higher order message test')->assertTrue(true); it('is a higher order message test')->expect(true)->toBeTrue();

View File

@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase;
uses(Macroable::class); uses(Macroable::class);
beforeEach()->macro('bar', function () { beforeEach()->macro('bar', function () {
assertInstanceOf(TestCase::class, $this); expect($this)->toBeInstanceOf(TestCase::class);
}); });
it('can call chained macro method')->bar(); it('can call chained macro method')->bar();

View File

@ -1,11 +1,12 @@
<?php <?php
use Pest\PendingObjects\TestCall;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
uses(Gettable::class); uses(Gettable::class);
/** /**
* @return TestCase|Gettable * @return TestCase|TestCall|Gettable
*/ */
function get(string $route) function get(string $route)
{ {
@ -15,15 +16,15 @@ function get(string $route)
trait Gettable trait Gettable
{ {
/** /**
* @return TestCase|Gettable * @return TestCase|TestCall|Gettable
*/ */
public function get(string $route) public function get(string $route)
{ {
assertNotEmpty($route); expect($route)->not->toBeEmpty();
return $this; return $this;
} }
} }
get('foo')->get('bar')->assertTrue(true); get('foo')->get('bar')->expect(true)->toBeTrue();
get('foo')->assertTrue(true); get('foo')->expect(true)->toBeTrue();

View File

@ -1,7 +1,7 @@
<?php <?php
test('a test', function () { test('a test', function () {
assertArrayHasKey('key', ['key' => 'foo']); $this->assertArrayHasKey('key', ['key' => 'foo']);
}); });
test('higher order message test')->assertTrue(true); test('higher order message test')->expect(true)->toBeTrue();

View File

@ -20,8 +20,8 @@ afterAll(function () {
}); });
register_shutdown_function(function () use ($foo) { register_shutdown_function(function () use ($foo) {
assertFalse($foo->beforeAll); expect($foo->beforeAll)->toBeFalse();
assertFalse($foo->beforeEach); expect($foo->beforeEach)->toBeFalse();
assertFalse($foo->afterEach); expect($foo->afterEach)->toBeFalse();
assertFalse($foo->afterAll); expect($foo->afterAll)->toBeFalse();
}); });

View File

@ -10,6 +10,6 @@ class CustomTestCaseInSubFolder extends TestCase
{ {
public function assertCustomInSubFolderTrue() public function assertCustomInSubFolderTrue()
{ {
assertTrue(true); $this->assertTrue(true);
} }
} }

View File

@ -12,7 +12,7 @@ class MyCustomClass extends PHPUnit\Framework\TestCase
{ {
public function assertTrueIsTrue() public function assertTrueIsTrue()
{ {
assertTrue(true); $this->assertTrue(true);
} }
} }

View File

@ -1,5 +1,5 @@
<?php <?php
test('basic', function () { test('basic', function () {
assertTrue(true); expect(true)->toBeTrue();
}); });

View File

@ -7,14 +7,14 @@ use PHPUnit\TextUI\DefaultResultPrinter;
it('sets defaults', function () { it('sets defaults', function () {
$arguments = AddsDefaults::to(['bar' => 'foo']); $arguments = AddsDefaults::to(['bar' => 'foo']);
assertInstanceOf(Printer::class, $arguments['printer']); expect($arguments['printer'])->toBeInstanceOf(Printer::class);
assertEquals($arguments['bar'], 'foo'); expect($arguments['bar'])->toBe('foo');
}); });
it('does not override options', function () { it('does not override options', function () {
$defaultResultPrinter = new DefaultResultPrinter(); $defaultResultPrinter = new DefaultResultPrinter();
assertEquals(AddsDefaults::to(['printer' => $defaultResultPrinter]), [ expect(AddsDefaults::to(['printer' => $defaultResultPrinter]))->tobe([
'printer' => $defaultResultPrinter, 'printer' => $defaultResultPrinter,
]); ]);
}); });

View File

@ -16,10 +16,10 @@ test('default php unit tests', function () {
$phpUnitTestCase = new class() extends PhpUnitTestCase { $phpUnitTestCase = new class() extends PhpUnitTestCase {
}; };
$testSuite->addTest($phpUnitTestCase); $testSuite->addTest($phpUnitTestCase);
assertCount(1, $testSuite->tests()); expect($testSuite->tests())->toHaveCount(1);
AddsTests::to($testSuite, new \Pest\TestSuite(getcwd())); AddsTests::to($testSuite, new \Pest\TestSuite(getcwd()));
assertCount(1, $testSuite->tests()); expect($testSuite->tests())->toHaveCount(1);
}); });
it('removes warnings', function () use ($pestTestCase) { it('removes warnings', function () use ($pestTestCase) {
@ -28,5 +28,5 @@ it('removes warnings', function () use ($pestTestCase) {
$testSuite->addTest($warningTestCase); $testSuite->addTest($warningTestCase);
AddsTests::to($testSuite, new \Pest\TestSuite(getcwd())); AddsTests::to($testSuite, new \Pest\TestSuite(getcwd()));
assertCount(0, $testSuite->tests()); expect($testSuite->tests())->toHaveCount(0);
}); });

View File

@ -38,5 +38,5 @@ it('do not throws exception when `process isolation` is false', function () {
'configuration' => $filename, 'configuration' => $filename,
]); ]);
assertTrue(true); expect(true)->toBeTrue();
}); });

View File

@ -8,7 +8,7 @@ it('outputs the version when --version is used', function () {
$plugin = new Version($output); $plugin = new Version($output);
$plugin->handleArguments(['foo', '--version']); $plugin->handleArguments(['foo', '--version']);
assertStringContainsString('Pest 0.2.2', $output->fetch()); expect($output->fetch())->toContain('Pest 0.2.2');
}); });
it('do not outputs version when --version is not used', function () { it('do not outputs version when --version is not used', function () {
@ -16,5 +16,5 @@ it('do not outputs version when --version is not used', function () {
$plugin = new Version($output); $plugin = new Version($output);
$plugin->handleArguments(['foo', 'bar']); $plugin->handleArguments(['foo', 'bar']);
assertEquals('', $output->fetch()); expect($output->fetch())->toBe('');
}); });

View File

@ -7,5 +7,5 @@ it('gets file name from called file', function () {
return Backtrace::file(); return Backtrace::file();
}; };
assertEquals(__FILE__, $a()); expect($a())->toBe(__FILE__);
}); });

View File

@ -15,32 +15,32 @@ it('exists')
it('gets an instance', function () { it('gets an instance', function () {
$this->container->add(Container::class, $this->container); $this->container->add(Container::class, $this->container);
assertSame($this->container, $this->container->get(Container::class)); expect($this->container->get(Container::class))->toBe($this->container);
}); });
test('autowire', function () { test('autowire', function () {
assertInstanceOf(Container::class, $this->container->get(Container::class)); expect($this->container->get(Container::class))->toBeInstanceOf(Container::class);
}); });
it('creates an instance and resolves parameters', function () { it('creates an instance and resolves parameters', function () {
$this->container->add(Container::class, $this->container); $this->container->add(Container::class, $this->container);
$instance = $this->container->get(ClassWithDependency::class); $instance = $this->container->get(ClassWithDependency::class);
assertInstanceOf(ClassWithDependency::class, $instance); expect($instance)->toBeInstanceOf(ClassWithDependency::class);
}); });
it('creates an instance and resolves also sub parameters', function () { it('creates an instance and resolves also sub parameters', function () {
$this->container->add(Container::class, $this->container); $this->container->add(Container::class, $this->container);
$instance = $this->container->get(ClassWithSubDependency::class); $instance = $this->container->get(ClassWithSubDependency::class);
assertInstanceOf(ClassWithSubDependency::class, $instance); expect($instance)->toBeInstanceOf(ClassWithSubDependency::class);
}); });
it('can resolve builtin value types', function () { it('can resolve builtin value types', function () {
$this->container->add('rootPath', getcwd()); $this->container->add('rootPath', getcwd());
$instance = $this->container->get(TestSuite::class); $instance = $this->container->get(TestSuite::class);
assertInstanceOf(TestSuite::class, $instance); expect($instance)->toBeInstanceOf(TestSuite::class);
}); });
it('cannot resolve a parameter without type', function () { it('cannot resolve a parameter without type', function () {

View File

@ -3,9 +3,10 @@
use Pest\Support\Reflection; use Pest\Support\Reflection;
it('gets file name from closure', function () { it('gets file name from closure', function () {
$fileName = Reflection::getFileNameFromClosure(function () {}); $fileName = Reflection::getFileNameFromClosure(function () {
});
assertEquals(__FILE__, $fileName); expect($fileName)->toBe(__FILE__);
}); });
it('gets property values', function () { it('gets property values', function () {
@ -15,5 +16,5 @@ it('gets property values', function () {
$value = Reflection::getPropertyValue($class, 'foo'); $value = Reflection::getPropertyValue($class, 'foo');
assertEquals('bar', $value); expect($value)->toBe('bar');
}); });

View File

@ -10,7 +10,7 @@ $run = function (string $target, $decorated = false) {
return $decorated ? $process->getOutput() : preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput()); return $decorated ? $process->getOutput() : preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput());
}; };
$snapshot = function ($name) { $snapshot = function ($name) {
$testsPath = dirname(__DIR__); $testsPath = dirname(__DIR__);
return file_get_contents(implode(DIRECTORY_SEPARATOR, [ return file_get_contents(implode(DIRECTORY_SEPARATOR, [
@ -21,23 +21,15 @@ $snapshot = function ($name) {
}; };
test('allows to run a single test', function () use ($run, $snapshot) { test('allows to run a single test', function () use ($run, $snapshot) {
assertStringContainsString( expect($run('tests/Fixtures/DirectoryWithTests/ExampleTest.php'))->toContain($snapshot('allows-to-run-a-single-test'));
$snapshot('allows-to-run-a-single-test'),
$run('tests/Fixtures/DirectoryWithTests/ExampleTest.php'));
})->skip(PHP_OS_FAMILY === 'Windows'); })->skip(PHP_OS_FAMILY === 'Windows');
test('allows to run a directory', function () use ($run, $snapshot) { test('allows to run a directory', function () use ($run, $snapshot) {
assertStringContainsString( expect($run('tests/Fixtures'))->toContain($snapshot('allows-to-run-a-directory'));
$snapshot('allows-to-run-a-directory'),
$run('tests/Fixtures')
);
})->skip(PHP_OS_FAMILY === 'Windows'); })->skip(PHP_OS_FAMILY === 'Windows');
it('has ascii chars', function () use ($run, $snapshot) { it('has ascii chars', function () use ($run, $snapshot) {
assertStringContainsString( expect($run('tests/Fixtures/DirectoryWithTests/ExampleTest.php', true))->toContain($snapshot('has-ascii-chars'));
$snapshot('has-ascii-chars'),
$run('tests/Fixtures/DirectoryWithTests/ExampleTest.php', true)
);
})->skip(PHP_OS_FAMILY === 'Windows'); })->skip(PHP_OS_FAMILY === 'Windows');
it('disable decorating printer when colors is set to never', function () use ($snapshot) { it('disable decorating printer when colors is set to never', function () use ($snapshot) {
@ -49,9 +41,5 @@ it('disable decorating printer when colors is set to never', function () use ($s
], dirname(__DIR__, 2)); ], dirname(__DIR__, 2));
$process->run(); $process->run();
$output = $process->getOutput(); $output = $process->getOutput();
expect($output)->toContain($snapshot('disable-decorating-printer'));
assertStringContainsString(
$snapshot('disable-decorating-printer'),
$output
);
})->skip(PHP_OS_FAMILY === 'Windows'); })->skip(PHP_OS_FAMILY === 'Windows');

View File

@ -22,7 +22,7 @@ test('visual snapshot of test suite on success', function () {
$output = explode("\n", $output()); $output = explode("\n", $output());
array_pop($output); array_pop($output);
array_pop($output); array_pop($output);
assertStringContainsString(implode("\n", $output), file_get_contents($snapshot)); expect(file_get_contents($snapshot))->toContain(implode("\n", $output));
} }
})->skip(!getenv('REBUILD_SNAPSHOTS') && getenv('EXCLUDE')) })->skip(!getenv('REBUILD_SNAPSHOTS') && getenv('EXCLUDE'))
->skip(PHP_OS_FAMILY === 'Windows'); ->skip(PHP_OS_FAMILY === 'Windows');