Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 'toStartWith' and 'toEndWith' expectations #187

Merged
merged 3 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ public function toContain($needle): Expectation
return $this;
}

/**
* Asserts that the value starts with $expected.
*/
public function toStartWith(string $expected): Expectation
{
Assert::assertStringStartsWith($expected, $this->value);

return $this;
}

/**
* Asserts that the value ends with $expected.
*/
public function toEndWith(string $expected): Expectation
{
Assert::assertStringEndsWith($expected, $this->value);

return $this;
}

/**
* Asserts that $count matches the number of elements of the value.
*/
Expand Down
12 changes: 11 additions & 1 deletion tests/.snapshots/success.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@
✓ passes strings
✓ passes arrays
✓ failures
✓ not failures

PASS Tests\Expect\toEndWith
✓ pass
✓ failures
✓ not failures

PASS Tests\Expect\toEqual
Expand Down Expand Up @@ -195,6 +200,11 @@
PASS Tests\Expect\toMatchObject
✓ pass
✓ failures
✓ not failures

PASS Tests\Expect\toStartWith
✓ pass
✓ failures
✓ not failures

PASS Tests\Features\AfterAll
Expand Down Expand Up @@ -363,5 +373,5 @@
✓ depends with defined arguments
✓ depends run test only once

Tests: 6 skipped, 214 passed
Tests: 6 skipped, 220 passed

15 changes: 15 additions & 0 deletions tests/Expect/toEndWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;

test('pass', function () {
expect('username')->toEndWith('name');
});

test('failures', function () {
expect('username')->toEndWith('password');
})->throws(ExpectationFailedException::class);

test('not failures', function () {
expect('username')->not->toEndWith('name');
})->throws(ExpectationFailedException::class);
15 changes: 15 additions & 0 deletions tests/Expect/toStartWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;

test('pass', function () {
expect('username')->toStartWith('user');
});

test('failures', function () {
expect('username')->toStartWith('password');
})->throws(ExpectationFailedException::class);

test('not failures', function () {
expect('username')->not->toStartWith('user');
})->throws(ExpectationFailedException::class);