feat(expect): add more methods

This commit is contained in:
ceceppa
2020-07-14 08:08:13 +01:00
parent d29c789788
commit 819825bdd2
18 changed files with 419 additions and 30 deletions

View File

@ -230,7 +230,7 @@ final class Expectation
/** /**
* Assert that the absolute difference between $value and $this->value * Assert that the absolute difference between $value and $this->value
* is greather thatn $delta. * is greater than $delta.
* *
* @param mixed $value * @param mixed $value
*/ */
@ -242,31 +242,153 @@ final class Expectation
} }
/** /**
* Assert that the value is a directory. * Assert that the value infinite.
*/ */
public function toBeExistingDirectory(): Expectation public function toBeInfinite(): Expectation
{ {
Assert::assertDirectoryExists($this->value); Assert::assertInfinite($this->value);
return $this; return $this;
} }
/** /**
* Assert that the value is a directory and is readable. * Assert that the value is an instance of $value.
*
* @param mixed $value
*/ */
public function toBeReadableDirectory(): Expectation public function toBeInstanceOf($value): Expectation
{ {
Assert::assertDirectoryIsReadable($this->value); Assert::assertInstanceOf($value, $this->value);
return $this; return $this;
} }
/** /**
* Assert that the value is a directory and is writable. * Assert that the value is an array.
*/ */
public function toBeWritableDirectory(): Expectation public function toBeArray(): Expectation
{ {
Assert::assertDirectoryIsWritable($this->value); Assert::assertIsArray($this->value);
return $this;
}
/**
* Assert that the value is of type bool.
*/
public function toBeBool(): Expectation
{
Assert::assertIsBool($this->value);
return $this;
}
/**
* Assert that the value is of type callable.
*/
public function toBeCallable(): Expectation
{
Assert::assertIsCallable($this->value);
return $this;
}
/**
* Assert that the value is type of float.
*/
public function toBeFloat(): Expectation
{
Assert::assertIsFloat($this->value);
return $this;
}
/**
* Assert that the value is type of int.
*/
public function toBeInt(): Expectation
{
Assert::assertIsInt($this->value);
return $this;
}
/**
* Assert that the value is type of iterable.
*/
public function toBeIterable(): Expectation
{
Assert::assertIsIterable($this->value);
return $this;
}
/**
* Assert that the value is type of numeric.
*/
public function toBeNumeric(): Expectation
{
Assert::assertIsNumeric($this->value);
return $this;
}
/**
* Assert that the value is type of object.
*/
public function toBeObject(): Expectation
{
Assert::assertIsObject($this->value);
return $this;
}
/**
* Assert that the value is type of resource.
*/
public function toBeResource(): Expectation
{
Assert::assertIsResource($this->value);
return $this;
}
/**
* Assert that the value is type of scalar.
*/
public function toBeScalar(): Expectation
{
Assert::assertIsScalar($this->value);
return $this;
}
/**
* Assert that the value is type of string.
*/
public function toBeString(): Expectation
{
Assert::assertIsString($this->value);
return $this;
}
/**
* Assert that the value is NAN.
*/
public function toBeNan(): Expectation
{
Assert::assertNan($this->value);
return $this;
}
/**
* Assert that the value is NAN.
*/
public function toBeNull(): Expectation
{
Assert::assertNull($this->value);
return $this; return $this;
} }

View File

@ -2,9 +2,24 @@
PASS Tests\CustomTestCase\ExecutedTest PASS Tests\CustomTestCase\ExecutedTest
✓ that gets executed ✓ that gets executed
PASS Tests\Expect\ToBe PASS Tests\Expect\toBe
✓ strict comparisons ✓ strict comparisons
✓ failures ✓ failures
✓ not failures
PASS Tests\Expect\toBeArray
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeBool
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeCallable
✓ pass
✓ failures
✓ not failures ✓ not failures
PASS Tests\Expect\toBeEmpty PASS Tests\Expect\toBeEmpty
@ -12,13 +27,13 @@
✓ failures ✓ failures
✓ not failures ✓ not failures
PASS Tests\Expect\toBeExistingDirectory PASS Tests\Expect\toBeFalse
pass strict comparisons
✓ failures ✓ failures
✓ not failures ✓ not failures
PASS Tests\Expect\toBeFalse PASS Tests\Expect\toBeFloat
strict comparisons pass
✓ failures ✓ failures
✓ not failures ✓ not failures
@ -30,6 +45,26 @@
PASS Tests\Expect\toBeGreatherThanOrEqual PASS Tests\Expect\toBeGreatherThanOrEqual
✓ passes ✓ passes
✓ failures ✓ failures
✓ not failures
PASS Tests\Expect\toBeInfinite
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeInstanceOf
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeInt
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeIterable
✓ pass
✓ failures
✓ not failures ✓ not failures
PASS Tests\Expect\toBeLessThan PASS Tests\Expect\toBeLessThan
@ -42,12 +77,37 @@
✓ failures ✓ failures
✓ not failures ✓ not failures
PASS Tests\Expect\toBeReadableDirectory PASS Tests\Expect\toBeNAN
✓ pass ✓ pass
✓ failures ✓ failures
✓ not failures ✓ not failures
PASS Tests\Expect\toBeWritableDirectory PASS Tests\Expect\toBeNull
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeNumeric
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeObject
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeResource
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeScalar
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeString
✓ pass ✓ pass
✓ failures ✓ failures
✓ not failures ✓ not failures
@ -267,5 +327,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, 156 passed Tests: 6 skipped, 192 passed
Time: 4.42s Time: 5.20s

View File

@ -3,13 +3,14 @@
use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () { test('pass', function () {
expect(sys_get_temp_dir())->toBeReadableDirectory(); expect([1, 2, 3])->toBeArray();
expect('1, 2, 3')->not->toBeArray();
}); });
test('failures', function () { test('failures', function () {
expect('/random/path/whatever')->toBeReadableDirectory(); expect(null)->toBeArray();
})->throws(ExpectationFailedException::class); })->throws(ExpectationFailedException::class);
test('not failures', function () { test('not failures', function () {
expect(sys_get_temp_dir())->not->toBeReadableDirectory(); expect(['a', 'b', 'c'])->not->toBeArray();
})->throws(ExpectationFailedException::class); })->throws(ExpectationFailedException::class);

View File

@ -3,13 +3,14 @@
use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () { test('pass', function () {
expect(sys_get_temp_dir())->toBeWritableDirectory(); expect(true)->toBeBool();
expect(0)->not->toBeBool();
}); });
test('failures', function () { test('failures', function () {
expect('/random/path/whatever')->toBeWritableDirectory(); expect(null)->toBeBool();
})->throws(ExpectationFailedException::class); })->throws(ExpectationFailedException::class);
test('not failures', function () { test('not failures', function () {
expect(sys_get_temp_dir())->not->toBeWritableDirectory(); expect(false)->not->toBeBool();
})->throws(ExpectationFailedException::class); })->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,18 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(function () {})->toBeCallable();
expect(null)->not->toBeCallable();
});
test('failures', function () {
$hello = 5;
expect($hello)->toBeCallable();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(function () { return 42; })->not->toBeCallable();
})->throws(ExpectationFailedException::class);

View File

@ -3,15 +3,14 @@
use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () { test('pass', function () {
$temp = sys_get_temp_dir(); expect(1.0)->toBeFloat();
expect(1)->not->toBeFloat();
expect($temp)->toBeExistingDirectory();
}); });
test('failures', function () { test('failures', function () {
expect('/random/path/whatever')->toBeExistingDirectory(); expect(42)->toBeFloat();
})->throws(ExpectationFailedException::class); })->throws(ExpectationFailedException::class);
test('not failures', function () { test('not failures', function () {
expect('.')->not->toBeExistingDirectory(); expect(log(3))->not->toBeFloat();
})->throws(ExpectationFailedException::class); })->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(log(0))->toBeInfinite();
expect(log(1))->not->toBeInfinite();
});
test('failures', function () {
expect(asin(2))->toBeInfinite();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(INF)->not->toBeInfinite();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(new Exception())->toBeInstanceOf(Exception::class);
expect(new Exception())->not->toBeInstanceOf(RuntimeException::class);
});
test('failures', function () {
expect(new Exception())->toBeInstanceOf(RuntimeException::class);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(new Exception())->not->toBeInstanceOf(Exception::class);
})->throws(ExpectationFailedException::class);

16
tests/Expect/toBeInt.php Normal file
View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(42)->toBeInt();
expect(42.0)->not->toBeInt();
});
test('failures', function () {
expect(42.0)->toBeInt();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(6 * 7)->not->toBeInt();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,23 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect([])->toBeIterable();
expect(null)->not->toBeIterable();
});
test('failures', function () {
expect(42)->toBeIterable();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
function gen(): iterable
{
yield 1;
yield 2;
yield 3;
}
expect(gen())->not->toBeIterable();
})->throws(ExpectationFailedException::class);

16
tests/Expect/toBeNAN.php Normal file
View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(asin(2))->toBeNan();
expect(log(0))->not->toBeNan();
});
test('failures', function () {
expect(1)->toBeNan();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(acos(1.5))->not->toBeNan();
})->throws(ExpectationFailedException::class);

16
tests/Expect/toBeNull.php Normal file
View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(null)->toBeNull();
expect('')->not->toBeNull();
});
test('failures', function () {
expect('hello')->toBeNull();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(null)->not->toBeNull();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(42)->toBeNumeric();
expect('A')->not->toBeNumeric();
});
test('failures', function () {
expect(null)->toBeNumeric();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(6 * 7)->not->toBeNumeric();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect((object) ['a' => 1])->toBeObject();
expect(['a' => 1])->not->toBeObject();
});
test('failures', function () {
expect(null)->toBeObject();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect((object) 'ciao')->not->toBeObject();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,22 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
$resource = tmpfile();
afterAll(function () use ($resource) {
fclose($resource);
});
test('pass', function () use ($resource) {
expect($resource)->toBeResource();
expect(null)->not->toBeResource();
});
test('failures', function () {
expect(null)->toBeResource();
})->throws(ExpectationFailedException::class);
test('not failures', function () use ($resource) {
expect($resource)->not->toBeResource();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(1.1)->toBeScalar();
});
test('failures', function () {
expect(null)->toBeScalar();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(42)->not->toBeScalar();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect('1.1')->toBeString();
expect(1.1)->not->toBeString();
});
test('failures', function () {
expect(null)->toBeString();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('42')->not->toBeString();
})->throws(ExpectationFailedException::class);