Skip to content

Commit 3339ef6

Browse files
committed
Add initial unit tests
1 parent ef62efd commit 3339ef6

File tree

11 files changed

+157
-7
lines changed

11 files changed

+157
-7
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ However, not every pull request will automatically be accepted. I will review ea
1111
## Testing
1212
- [ ] After making your changes, make sure the tests still pass.
1313
- [ ] When adding new functionality, also add new tests.
14-
- [ ] Make sure there are no build errors on our CI server (https://ci.genealabs.com/build-status/view/7)
14+
- [ ] Make sure there are no build errors on our CI server (https://ci.genealabs.com/build-status/view/8)
1515
- [ ] All code must past PHPCS and PHPMD PSR2 validation.
1616

1717
## Submitting changes

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"illuminate/database": "5.5.*"
1515
},
1616
"require-dev": {
17+
"fzaninotto/faker": "~1.4",
1718
"laravel/laravel": "5.5.*",
1819
"mockery/mockery": "0.9.*",
1920
"phpmd/phpmd": "^2.6",
@@ -22,6 +23,9 @@
2223
"sebastian/phpcpd": "*"
2324
},
2425
"autoload": {
26+
"classmap": [
27+
"tests/database/factories"
28+
],
2529
"psr-4": {
2630
"GeneaLabs\\LaravelModelCaching\\": "src/",
2731
"GeneaLabs\\LaravelModelCaching\\Tests\\": "tests/"

src/Builder.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ protected function eagerLoadRelation(array $models, $name, Closure $constraints)
1111
$relation->addEagerConstraints($models);
1212
$constraints($relation);
1313

14+
$parentIds = implode('_', collect($models)->pluck('id')->toArray());
1415
$parentName = str_slug(get_class($relation->getParent()));
15-
$childName = str_slug(get_class($relation->getModel()));
16-
$results = cache()->tags([$parentName, $childName])
17-
->rememberForever("{$parentName}-{$childName}-relation", function () use ($relation) {
16+
$childName = str_slug(get_class($relation->getRelated()));
17+
$cache = cache();
18+
19+
if (is_subclass_of(cache()->getStore(), TaggableStore::class)) {
20+
$cache->tags([$parentName, $childName]);
21+
}
22+
23+
$results = $cache
24+
->rememberForever("{$parentName}_{$parentIds}-{$childName}s", function () use ($relation) {
1825
return $relation->getEager();
1926
});
2027

tests/CreatesApplication.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests;
22

33
use Illuminate\Contracts\Console\Kernel;
4+
use Illuminate\Database\Eloquent\Factory;
45

56
trait CreatesApplication
67
{
78
public function createApplication()
89
{
910
$app = require __DIR__ . '/../vendor/laravel/laravel/bootstrap/app.php';
1011
$app->make(Kernel::class)->bootstrap();
12+
$app->make(Factory::class)->load(__DIR__ . '/database/factories');
13+
$app->afterResolving('migrator', function ($migrator) {
14+
$migrator->path(__DIR__ . '/database/migrations');
15+
});
1116

1217
return $app;
1318
}

tests/Fixtures/Author.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use GeneaLabs\LaravelModelCaching\CachedModel;
4+
use Illuminate\Database\Eloquent\Relations\HasMany;
5+
6+
class Author extends CachedModel
7+
{
8+
protected $fillable = [
9+
'name',
10+
'email',
11+
];
12+
13+
public function books() : HasMany
14+
{
15+
return $this->hasMany(Book::class);
16+
}
17+
}

tests/Fixtures/Book.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use GeneaLabs\LaravelModelCaching\CachedModel;
4+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
5+
6+
class Book extends CachedModel
7+
{
8+
protected $dates = [
9+
'published_at',
10+
];
11+
protected $fillable = [
12+
'description',
13+
'published_at',
14+
'title',
15+
];
16+
17+
public function author() : BelongsTo
18+
{
19+
return $this->belongsTo(Author::class);
20+
}
21+
}

tests/Unit/CacheTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Unit;
22

3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
35
use GeneaLabs\LaravelModelCaching\Tests\TestCase;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
47

58
class CacheTest extends TestCase
69
{
7-
public function testExample()
10+
use RefreshDatabase;
11+
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
factory(Author::class, 10)->create()
17+
->each(function($author) {
18+
factory(Book::class, random_int(2, 10))->make()
19+
->each(function ($book) use ($author) {
20+
$book->author()->associate($author);
21+
$book->save();
22+
});
23+
});
24+
}
25+
26+
public function testCacheIsEmptyBeforeLoadingModels()
827
{
9-
//TODO: add initial test
10-
$this->assertTrue(true);
28+
$this->assertNull(cache()->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'));
29+
}
30+
31+
public function testCacheIsNotEmptyAfterLoadingModels()
32+
{
33+
(new Author)->with('books')->get();
34+
35+
$this->assertNotNull(cache()->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'));
1136
}
1237
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Faker\Generator as Faker;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
5+
6+
$factory->define(Author::class, function (Faker $faker) {
7+
return [
8+
'name' => $faker->name,
9+
'email' => $faker->unique()->safeEmail,
10+
];
11+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use Faker\Generator as Faker;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
6+
$factory->define(Book::class, function (Faker $faker) {
7+
return [
8+
'title' => $faker->title,
9+
'description' => $faker->paragraphs(3, true),
10+
'published_at' => $faker->dateTime,
11+
];
12+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateAuthors extends Migration
7+
{
8+
public function up()
9+
{
10+
Schema::create('authors', function (Blueprint $table) {
11+
$table->increments('id');
12+
$table->timestamps();
13+
14+
$table->string('email');
15+
$table->string('name');
16+
});
17+
}
18+
19+
public function down()
20+
{
21+
//
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateBooks extends Migration
7+
{
8+
public function up()
9+
{
10+
Schema::create('books', function (Blueprint $table) {
11+
$table->increments('id');
12+
$table->unsignedInteger('author_id');
13+
$table->timestamps();
14+
15+
$table->text('description');
16+
$table->dateTime('published_at');
17+
$table->string('title');
18+
});
19+
}
20+
21+
public function down()
22+
{
23+
//
24+
}
25+
}

0 commit comments

Comments
 (0)