Skip to content

Commit 0a2ddb6

Browse files
author
nejc
committed
Remove default Pest example tests; only real repository tests remain.
1 parent ec20546 commit 0a2ddb6

File tree

6 files changed

+171
-1
lines changed

6 files changed

+171
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
},
3939
"autoload-dev": {
4040
"psr-4": {
41+
"Tests\\": "tests/",
4142
"Laravelplus\\RepositoryPattern\\Tests\\": "tests"
4243
}
4344
},

phpunit.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
>
7+
<testsuites>
8+
<testsuite name="Unit">
9+
<directory>tests/Unit</directory>
10+
</testsuite>
11+
<testsuite name="Feature">
12+
<directory>tests/Feature</directory>
13+
</testsuite>
14+
</testsuites>
15+
<source>
16+
<include>
17+
<directory>app</directory>
18+
</include>
19+
</source>
20+
<php>
21+
<env name="APP_ENV" value="testing"/>
22+
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
23+
<env name="BCRYPT_ROUNDS" value="4"/>
24+
<env name="CACHE_STORE" value="array"/>
25+
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
26+
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
27+
<env name="MAIL_MAILER" value="array"/>
28+
<env name="PULSE_ENABLED" value="false"/>
29+
<env name="QUEUE_CONNECTION" value="sync"/>
30+
<env name="SESSION_DRIVER" value="array"/>
31+
<env name="TELESCOPE_ENABLED" value="false"/>
32+
</php>
33+
</phpunit>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Capsule\Manager as Capsule;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Schema\Blueprint;
8+
use Prefabs\UserRepository;
9+
10+
describe('UserRepository', function (): void {
11+
beforeEach(function (): void {
12+
// Setup Eloquent with in-memory SQLite
13+
$capsule = new Capsule();
14+
$capsule->addConnection([
15+
'driver' => 'sqlite',
16+
'database' => ':memory:',
17+
'prefix' => '',
18+
]);
19+
$capsule->setAsGlobal();
20+
$capsule->bootEloquent();
21+
22+
// Create users table
23+
Capsule::schema()->create('users', function (Blueprint $table): void {
24+
$table->id();
25+
$table->string('name');
26+
$table->string('email')->unique();
27+
$table->string('password');
28+
$table->timestamps();
29+
});
30+
});
31+
32+
it('can create, find, update, delete, and search users', function (): void {
33+
// Define a simple User model
34+
eval('class TestUser extends \\Illuminate\\Database\\Eloquent\\Model { protected $table = "users"; protected $fillable = ["name", "email", "password"]; public $timestamps = true; }');
35+
$userModel = new TestUser();
36+
$repo = new UserRepository($userModel);
37+
38+
// Create
39+
$user = $repo->create([
40+
'name' => 'Alice',
41+
'email' => '[email protected]',
42+
'password' => 'secret',
43+
]);
44+
expect($user->id)->not()->toBeNull();
45+
expect($user->name)->toBe('Alice');
46+
expect(password_verify('secret', $user->password))->toBeTrue();
47+
48+
// Find
49+
$found = $repo->find($user->id);
50+
expect($found)->not()->toBeNull();
51+
expect($found->email)->toBe('[email protected]');
52+
53+
// Find by email
54+
$foundByEmail = $repo->findBy('email', '[email protected]');
55+
expect($foundByEmail)->not()->toBeNull();
56+
expect($foundByEmail->id)->toBe($user->id);
57+
58+
// Update
59+
$repo->update($user->id, ['name' => 'Alice Updated']);
60+
$updated = $repo->find($user->id);
61+
expect($updated->name)->toBe('Alice Updated');
62+
63+
// Search
64+
$results = $repo->search('Alice');
65+
expect($results)->toHaveCount(1);
66+
expect($results->first()->id)->toBe($user->id);
67+
68+
// All
69+
$all = $repo->all();
70+
expect($all)->toHaveCount(1);
71+
72+
// Delete
73+
$deleted = $repo->delete($user->id);
74+
expect($deleted)->toBeTrue();
75+
expect($repo->find($user->id))->toBeNull();
76+
});
77+
});

tests/Pest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Test Case
6+
|--------------------------------------------------------------------------
7+
|
8+
| The closure you provide to your test functions is always bound to a specific PHPUnit test
9+
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
10+
| need to change it using the "pest()" function to bind a different classes or traits.
11+
|
12+
*/
13+
14+
pest()->extend(Tests\TestCase::class)
15+
// ->use(Illuminate\Foundation\Testing\RefreshDatabase::class)
16+
->in('Feature');
17+
18+
/*
19+
|--------------------------------------------------------------------------
20+
| Expectations
21+
|--------------------------------------------------------------------------
22+
|
23+
| When you're writing tests, you often need to check that values meet certain conditions. The
24+
| "expect()" function gives you access to a set of "expectations" methods that you can use
25+
| to assert different things. Of course, you may extend the Expectation API at any time.
26+
|
27+
*/
28+
29+
expect()->extend('toBeOne', function () {
30+
return $this->toBe(1);
31+
});
32+
33+
/*
34+
|--------------------------------------------------------------------------
35+
| Functions
36+
|--------------------------------------------------------------------------
37+
|
38+
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
39+
| project that you don't want to repeat in every file. Here you can also expose helpers as
40+
| global functions to help you to reduce the number of lines of code in your test files.
41+
|
42+
*/
43+
44+
function something()
45+
{
46+
// ..
47+
}

tests/TestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use PHPUnit\Framework\TestCase as BaseTestCase;
8+
9+
abstract class TestCase extends BaseTestCase
10+
{
11+
//
12+
}

tests/UserRepositoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@
7474
expect($deleted)->toBeTrue();
7575
expect($repo->find($user->id))->toBeNull();
7676
});
77-
});
77+
});

0 commit comments

Comments
 (0)