Skip to content

Commit

Permalink
Merge pull request #2 from def-studio/adds_collection_expectations
Browse files Browse the repository at this point in the history
adds toBeCollection() and toBeEloquentCollection() expectations
  • Loading branch information
fabio-ivona authored Oct 3, 2021
2 parents 2bdc9b4 + 8ed0065 commit 6d768ed
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [v0.0.3](https://github.com/pestphp/defstudio-plugin-laravel-expectations/compare/v0.0.2...v0.0.3) - 2021-10-03
### Added
- `toBeCollection()` expectation
- `toBeEloquentCollection()` expectation

## [v0.0.2](https://github.com/pestphp/defstudio-plugin-laravel-expectations/compare/v0.0.1...v0.0.2) - 2021-10-03
### Updated
Expand Down
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,38 @@ The expectations added by this plugin are authomatically loaded into Pest's expe

## Expectations

### `toExist()`
### `toBeCollection()`

Assert the given model exists in the database.
Asserts that the value is an instance of \Illuminate\Support\Collection

```php
expect($model)->toExist();
expect(collect[1,2,3])->toBeCollection();
```

### `toBeEloquentCollection()`

Asserts that the value is an instance of \Illuminate\Database\Eloquent\Collection

```php
expect(User::all())->toBeCollection();
```

### `toBeInDatabase()`

Assert that the given _where condition_ exists in the database
Asserts that the given _where condition_ exists in the database

```php
expect(['name' => 'Fabio'])->toBeInDatabase(table: 'users');
```

### `toExist()`

Asserts the given model exists in the database.

```php
expect($model)->toExist();
```

## Tests

Run all tests:
Expand Down
26 changes: 21 additions & 5 deletions src/Expectations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace DefStudio\PestLaravelExpectations;

/*
* Assert the given model exists in the database.
*/

use Illuminate\Support\Collection;
use Pest\Expectation;
use function Pest\Laravel\assertDatabaseHas;

/*
* Asserts the given model exists in the database.
*/
expect()->extend('toExist', function (): Expectation {
assertDatabaseHas(
$this->value->getTable(),
Expand All @@ -22,7 +22,7 @@
});

/*
* Assert that the given "where condition" exists in the database
* Asserts that the given "where condition" exists in the database
*
* @param \Illuminate\Database\Eloquent\Model|string $table
* @param string|null $connection
Expand All @@ -32,3 +32,19 @@

return $this;
});

/*
* Asserts that the value is an instance of \Illuminate\Support\Collection
*/
expect()->extend('toBeCollection', function (): Expectation {
// @phpstan-ignore-next-line
return $this->toBeInstanceOf(Collection::class);
});

/*
* Asserts that the value is an instance of \Illuminate\Database\Eloquent\Collection
*/
expect()->extend('toBeEloquentCollection', function (): Expectation {
// @phpstan-ignore-next-line
return $this->toBeInstanceOf(\Illuminate\Database\Eloquent\Collection::class);
});
16 changes: 16 additions & 0 deletions tests/Expect/toBeCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;

test('pass', function () {
expect(collect([1, 2, 3]))->toBeCollection();
expect('1, 2, 3')->not->toBeCollection();
});

test('failures', function () {
expect((object) [])->toBeCollection();
})->throws(ExpectationFailedException::class);

test('not failures', function () {
expect(collect(['a', 'b', 'c']))->not->toBeCollection();
})->throws(ExpectationFailedException::class);
17 changes: 17 additions & 0 deletions tests/Expect/toBeEloquentCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Illuminate\Database\Eloquent\Collection;
use PHPUnit\Framework\ExpectationFailedException;

test('pass', function () {
expect(Collection::empty())->toBeEloquentCollection();
expect('1, 2, 3')->not->toBeCollection();
});

test('failures', function () {
expect(collect(['a', 'b', 'c']))->toBeEloquentCollection();
})->throws(ExpectationFailedException::class);

test('not failures', function () {
expect(Collection::empty())->not->toBeEloquentCollection();
})->throws(ExpectationFailedException::class);

0 comments on commit 6d768ed

Please sign in to comment.