Skip to content

Commit

Permalink
added new expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
fabio-ivona committed Oct 2, 2021
1 parent 34d9416 commit 9b6d275
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 10 deletions.
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"require": {
"php": "^7.3 || ^8.0",
"pestphp/pest": "^1.0",
"pestphp/pest-plugin": "^1.0"
"pestphp/pest-plugin": "^1.0",
"pestphp/pest-plugin-laravel": "^1.1"
},
"autoload": {
"psr-4": {
Expand All @@ -40,7 +41,13 @@
"src/Autoload.php"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"require-dev": {
"orchestra/testbench": "^6.21",
"pestphp/pest-dev-tools": "dev-master"
},
"extra": {
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ parameters:
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: false
reportUnmatchedIgnoredErrors: true

ignoreErrors:
- "#Undefined variable: \\$this#"
2 changes: 1 addition & 1 deletion src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

namespace DefStudio\PestLaravelExpectations;


require_once 'Expectations.php';
34 changes: 34 additions & 0 deletions src/Expectations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace DefStudio\PestLaravelExpectations;

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

use Pest\Expectation;
use function Pest\Laravel\assertDatabaseHas;

expect()->extend('toExist', function (): Expectation {
assertDatabaseHas(
$this->value->getTable(),
[$this->value->getKeyName() => $this->value->getKey()],
$this->value->getConnectionName()
);

return $this;
});

/*
* Asserts that the given where condition exists in the database
*
* @param \Illuminate\Database\Eloquent\Model|string $table
* @param string|null $connection
*/
expect()->extend('toBeInDatabase', function ($table, $connection = null): Expectation {
assertDatabaseHas($table, $this->value, $connection);

return $this;
});
3 changes: 0 additions & 3 deletions tests/Example.php

This file was deleted.

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

use PHPUnit\Framework\ExpectationFailedException;
use Tests\Models\User;

test('pass', function () {
$user = User::create([
'name' => 'test user',
'email' => 'email@test.xx',
'password' => Hash::make('password'),
]);

expect(['id' => $user->id])->toBeInDatabase('users');
expect(['name' => 'test user'])->toBeInDatabase('users');
expect(['email' => 'email@test.xx'])->toBeInDatabase('users');
});

test('failures', function () {
expect(['id' => 1])->toBeInDatabase('users');
})->throws(ExpectationFailedException::class);

test('not failures', function () {
$user = User::create([
'name' => 'test user',
'email' => 'email@test.xx',
'password' => Hash::make('password'),
]);

expect(['id' => 1])->not->toBeInDatabase('users');
})->throws(ExpectationFailedException::class);
36 changes: 36 additions & 0 deletions tests/Expect/toExist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;
use Tests\Models\User;

test('pass', function () {
$user = User::create([
'name' => 'test user',
'email' => 'email@test.xx',
'password' => 'password',
]);

expect($user)->toExist();
});

test('failures', function () {
$user = User::create([
'name' => 'test user',
'email' => 'email@test.xx',
'password' => 'password',
]);

$user->delete();

expect($user)->toExist();
})->throws(ExpectationFailedException::class);

test('not failures', function () {
$user = User::create([
'name' => 'test user',
'email' => 'email@test.xx',
'password' => 'password',
]);

expect($user)->not->toExist();
})->throws(ExpectationFailedException::class);
5 changes: 0 additions & 5 deletions tests/Functions.php

This file was deleted.

14 changes: 14 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Tests\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
protected $fillable = [
'name',
'email',
'password',
];
}
5 changes: 5 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Tests\TestCase;

uses(TestCase::class)->in('Expect');
28 changes: 28 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Tests;

class TestCase extends \Orchestra\Testbench\TestCase
{
protected function setUp(): void
{
parent::setUp();

$this->loadMigrationsFrom(__DIR__ . '/resources/database/migrations');
$this->artisan('migrate', ['--database' => 'testbench'])->run();
}

protected function getEnvironmentSetUp($app): void
{
$app['config']->set('view.paths', [
__DIR__ . '/resources/views',
]);
$app['config']->set('app.key', 'base64:Hupx3yAySikrM2/edkZQNQHslgDWYfiBfCuSThJ5SK8=');
$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}

0 comments on commit 9b6d275

Please sign in to comment.