This repository was archived by the owner on Jan 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Adds testing macros for laravel collections #4
Merged
svenluijten
merged 6 commits into
svenluijten:master
from
eduarguz:feature/collection-macros
Oct 10, 2018
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
72871e6
Adds testing macros for laravel collections
eduarguz 835d098
Adds support for native contains sintax
eduarguz 69ae0a7
fixes code style
eduarguz 67679c0
Fix Code Style
eduarguz 24b9588
Adds Documentation for TestCollectionMacros
eduarguz 65d0794
capitalization of brand name
svenluijten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| namespace Sven\LaravelTestingUtils\Collections; | ||
|
|
||
| use Illuminate\Support\Collection; | ||
|
|
||
| class TestCollectionMacros | ||
| { | ||
| public static function enable(): void | ||
| { | ||
| Collection::mixin(new TestCollectionMixin()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
|
|
||
| namespace Sven\LaravelTestingUtils\Collections; | ||
|
|
||
| use PHPUnit\Framework\Assert as PHPUnit; | ||
|
|
||
| /** | ||
| * @method bool contains($key, $operator = null, $value = null) | ||
| */ | ||
| class TestCollectionMixin | ||
| { | ||
| /** | ||
| * Assert that an item exists in the collection. | ||
| * | ||
| * @return \Closure | ||
| */ | ||
| public function assertContains() | ||
| { | ||
| return function ($key, $operator = null, $value = null) { | ||
| /* @var \Illuminate\Support\Collection $this */ | ||
| PHPUnit::assertTrue( | ||
| $this->contains(...func_get_args()), | ||
| 'Failed asserting that the collection contains the specified value.' | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Assert that an item does not exist in the collection. | ||
| * | ||
| * @return \Closure | ||
| */ | ||
| public function assertNotContains() | ||
| { | ||
| return function ($key, $operator = null, $value = null) { | ||
| /* @var \Illuminate\Support\Collection $this */ | ||
| PHPUnit::assertFalse( | ||
| $this->contains(...func_get_args()), | ||
svenluijten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 'Failed asserting that the collection does not contain the specified value.' | ||
| ); | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| namespace Sven\LaravelTestingUtils\Tests\Collections; | ||
|
|
||
| use PHPUnit\Framework\AssertionFailedError; | ||
| use Sven\LaravelTestingUtils\Collections\TestCollectionMacros; | ||
| use Sven\LaravelTestingUtils\Tests\TestCase; | ||
|
|
||
| class CollectionContainsTest extends TestCase | ||
| { | ||
| protected function setUp() | ||
| { | ||
| parent::setUp(); | ||
| TestCollectionMacros::enable(); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function collection_contains_the_item() | ||
| { | ||
| collect(['cap', 'thor'])->assertContains('cap'); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function collection_contains_the_item_with_callable() | ||
| { | ||
| collect(['cap', 'thor'])->assertContains(function ($value) { | ||
| return $value == 'thor'; | ||
| }); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function collection_contains_the_item_with_key_value_pair() | ||
| { | ||
| collect([['name' => 'cap'], ['name' => 'thor']])->assertContains('name', 'cap'); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function collection_does_not_contain_the_item() | ||
| { | ||
| $this->expectException(AssertionFailedError::class); | ||
|
|
||
| collect(['cap', 'thor'])->assertContains('hulk'); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function collection_does_not_contain_the_item_with_callable() | ||
| { | ||
| $this->expectException(AssertionFailedError::class); | ||
|
|
||
| collect(['cap', 'thor'])->assertContains(function ($value) { | ||
| return $value == 'hulk'; | ||
| }); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function collection_does_not_contain_the_item_with_key_value_pair() | ||
| { | ||
| $this->expectException(AssertionFailedError::class); | ||
|
|
||
| collect([['name' => 'cap'], ['name' => 'thor']])->assertContains('name', 'hulk'); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| namespace Sven\LaravelTestingUtils\Tests\Collections; | ||
|
|
||
| use PHPUnit\Framework\AssertionFailedError; | ||
| use Sven\LaravelTestingUtils\Collections\TestCollectionMacros; | ||
| use Sven\LaravelTestingUtils\Tests\TestCase; | ||
|
|
||
| class CollectionNotContainsTest extends TestCase | ||
| { | ||
| protected function setUp() | ||
| { | ||
| parent::setUp(); | ||
| TestCollectionMacros::enable(); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function collection_does_not_contain_the_item() | ||
| { | ||
| collect(['cap', 'thor'])->assertNotContains('hulk'); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function collection_does_not_contain_the_item_with_callable() | ||
| { | ||
| collect(['cap', 'thor'])->assertNotContains(function ($value) { | ||
| return $value == 'hulk'; | ||
| }); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function collection_does_not_contain_the_item_with_key_value_pair() | ||
| { | ||
| collect([['name' => 'cap'], ['name' => 'thor']])->assertNotContains('name', 'hulk'); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function collection_contains_the_item() | ||
| { | ||
| $this->expectException(AssertionFailedError::class); | ||
|
|
||
| collect(['cap', 'thor'])->assertNotContains('cap'); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function collection_contains_the_item_with_callable() | ||
| { | ||
| $this->expectException(AssertionFailedError::class); | ||
|
|
||
| collect(['cap', 'thor'])->assertNotContains(function ($value) { | ||
| return $value == 'thor'; | ||
| }); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function collection_contains_the_item_with_key_value_pair() | ||
| { | ||
| $this->expectException(AssertionFailedError::class); | ||
|
|
||
| collect([['name' => 'cap'], ['name' => 'thor']])->assertNotContains('name', 'cap'); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.