Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 5 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
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ to write them yourself in every new application.
- [Downloading](#downloading)
- [Usage](#usage)
- [`InteractsWithViews`](#interactswithviews)
- [`TestCollectionMacros`](#testcollectionmacros)
- [Contributing](#contributing)
- [License](#license)

Expand All @@ -42,7 +43,7 @@ and run `composer update` on the command line to download the package:
```

## Usage
You will now have access to several traits to use in your test classes.
You will now have access to several traits and macros to use in your test classes.

### `InteractsWithViews`
This trait adds two view assertions: `assertViewExists` and `assertViewNotExists`.
Expand Down Expand Up @@ -76,6 +77,35 @@ class ServiceTest extends TestCase
}
```

### `TestCollectionMacros`
This set of macros adds some assertions to laravel collections: `assertContains` and `assertNotContains`.
They are used as follows:

```php
<?php

use Sven\LaravelTestingUtils\Collections\TestCollectionMacros;
use Illuminate\Foundation\Testing\TestCase;

class ServiceTest extends TestCase
{
protected function setUp()
{
parent::setUp();
TestCollectionMacros::enable();
}

/** @test */
public function it_fetches_some_data()
{
// ...

$collection->assertContains('some-item');
$collection->assertNotContains('some-other-item');
}
}
```

## Contributing
All contributions (pull requests, issues and feature requests) are
welcome. Make sure to read through the [CONTRIBUTING.md](CONTRIBUTING.md) first,
Expand Down
13 changes: 13 additions & 0 deletions src/Collections/TestCollectionMacros.php
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());
}
}
43 changes: 43 additions & 0 deletions src/Collections/TestCollectionMixin.php
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()),
'Failed asserting that the collection does not contain the specified value.'
);
};
}
}
62 changes: 62 additions & 0 deletions tests/Collections/CollectionContainsTest.php
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');
}
}
62 changes: 62 additions & 0 deletions tests/Collections/CollectionNotContainsTest.php
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');
}
}