Skip to content

Commit 60d66cc

Browse files
committed
Add more unit tests
1 parent 7187a07 commit 60d66cc

10 files changed

+217
-1
lines changed

tests/Fixtures/Author.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use GeneaLabs\LaravelModelCaching\CachedModel;
44
use Illuminate\Database\Eloquent\Relations\HasMany;
5+
use Illuminate\Database\Eloquent\Relations\HasOne;
56

67
class Author extends CachedModel
78
{
@@ -14,4 +15,9 @@ public function books() : HasMany
1415
{
1516
return $this->hasMany(Book::class);
1617
}
18+
19+
public function profile() : HasOne
20+
{
21+
return $this->hasOne(Profile::class);
22+
}
1723
}

tests/Fixtures/Book.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use GeneaLabs\LaravelModelCaching\CachedModel;
44
use Illuminate\Database\Eloquent\Relations\BelongsTo;
5+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
56

67
class Book extends CachedModel
78
{
@@ -18,4 +19,9 @@ public function author() : BelongsTo
1819
{
1920
return $this->belongsTo(Author::class);
2021
}
22+
23+
public function stores() : BelongsToMany
24+
{
25+
return $this->belongsToMany(Store::class);
26+
}
2127
}

tests/Fixtures/Profile.php

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

tests/Fixtures/Store.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\BelongsToMany;
5+
6+
class Store extends CachedModel
7+
{
8+
protected $fillable = [
9+
'address',
10+
'name',
11+
];
12+
13+
public function books() : BelongsToMany
14+
{
15+
return $this->belongsToMany(Book::class);
16+
}
17+
}

tests/Unit/CacheTest.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
44
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
57
use GeneaLabs\LaravelModelCaching\Tests\TestCase;
68
use Illuminate\Foundation\Testing\RefreshDatabase;
79

@@ -14,15 +16,24 @@ public function setUp()
1416
parent::setUp();
1517

1618
cache()->flush();
17-
1819
factory(Author::class, 10)->create()
1920
->each(function($author) {
2021
factory(Book::class, random_int(2, 10))->make()
2122
->each(function ($book) use ($author) {
2223
$book->author()->associate($author);
2324
$book->save();
2425
});
26+
factory(Profile::class)->make([
27+
'author_id' => $author->id,
28+
]);
29+
});
30+
31+
$bookIds = (new Book)->all()->pluck('id');
32+
factory(Store::class, 10)->create()
33+
->each(function ($store) use ($bookIds) {
34+
$store->books()->sync(rand($bookIds->min(), $bookIds->max()));
2535
});
36+
cache()->flush();
2637
}
2738

2839
public function testCacheIsEmptyBeforeLoadingModels()
@@ -77,4 +88,60 @@ public function testDeletingModelClearsCache()
7788

7889
$this->assertNull($results);
7990
}
91+
92+
public function testHasManyRelationshipIsCached()
93+
{
94+
$authors = (new Author)->with('books')->get();
95+
$authorIds = implode('_', $authors->pluck('id')->toArray());
96+
97+
$results = cache()->tags([
98+
'genealabslaravelmodelcachingtestsfixturesauthor',
99+
'genealabslaravelmodelcachingtestsfixturesbook'
100+
])
101+
->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesbooks");
102+
103+
$this->assertNotNull($results);
104+
}
105+
106+
public function testBelongsToRelationshipIsCached()
107+
{
108+
$books = (new Book)->with('author')->get()->first();
109+
$bookIds = implode('_', $books->pluck('id')->toArray());
110+
111+
$results = cache()->tags([
112+
'genealabslaravelmodelcachingtestsfixturesbook',
113+
'genealabslaravelmodelcachingtestsfixturesauthor'
114+
])
115+
->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesauthors");
116+
117+
$this->assertNotNull($results);
118+
}
119+
120+
public function testBelongsToManyRelationshipIsCached()
121+
{
122+
$books = (new Book)->with('stores')->get();
123+
$bookIds = implode('_', $books->pluck('id')->toArray());
124+
125+
$results = cache()->tags([
126+
'genealabslaravelmodelcachingtestsfixturesbook',
127+
'genealabslaravelmodelcachingtestsfixturesstore'
128+
])
129+
->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesstores");
130+
131+
$this->assertNotNull($results);
132+
}
133+
134+
public function testHasOneRelationshipIsCached()
135+
{
136+
$authors = (new Author)->with('profile')->get();
137+
$authorIds = implode('_', $authors->pluck('id')->toArray());
138+
139+
$results = cache()->tags([
140+
'genealabslaravelmodelcachingtestsfixturesauthor',
141+
'genealabslaravelmodelcachingtestsfixturesprofile'
142+
])
143+
->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesprofiles");
144+
145+
$this->assertNotNull($results);
146+
}
80147
}
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\Profile;
5+
6+
$factory->define(Profile::class, function (Faker $faker) {
7+
return [
8+
'first_name' => $faker->firstName,
9+
'first_name' => $faker->lastName,
10+
];
11+
});
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\Store;
5+
6+
$factory->define(Store::class, function (Faker $faker) {
7+
return [
8+
'address' => $faker->address,
9+
'name' => $faker->company,
10+
];
11+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateProfiles extends Migration
7+
{
8+
public function up()
9+
{
10+
Schema::create('profiles', function (Blueprint $table) {
11+
$table->increments('id');
12+
$table->unsignedInteger('author_id');
13+
$table->timestamps();
14+
15+
$table->text('first_name');
16+
$table->text('last_name');
17+
});
18+
}
19+
20+
public function down()
21+
{
22+
//
23+
}
24+
}
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 CreateStores extends Migration
7+
{
8+
public function up()
9+
{
10+
Schema::create('stores', function (Blueprint $table) {
11+
$table->increments('id');
12+
$table->timestamps();
13+
14+
$table->text('address');
15+
$table->text('name');
16+
});
17+
}
18+
19+
public function down()
20+
{
21+
//
22+
}
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateBookStore extends Migration
7+
{
8+
public function up()
9+
{
10+
Schema::create('book_store', function (Blueprint $table) {
11+
$table->increments('id');
12+
$table->unsignedInteger('book_id');
13+
$table->unsignedInteger('store_id');
14+
$table->timestamps();
15+
16+
$table->foreign('book_id')
17+
->references('id')
18+
->on('books')
19+
->onUpdate('CASCADE')
20+
->onDelete('CASCADE');
21+
$table->foreign('store_id')
22+
->references('id')
23+
->on('stores')
24+
->onUpdate('CASCADE')
25+
->onDelete('CASCADE');
26+
});
27+
}
28+
29+
public function down()
30+
{
31+
//
32+
}
33+
}

0 commit comments

Comments
 (0)