Skip to content

Commit 28d685d

Browse files
committed
Add tests for HasMany, HasOneThrough relationships.
1 parent b9960b6 commit 28d685d

16 files changed

+279
-6
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"phpunit/phpunit": "^8.0",
2929
"predis/predis": "*",
3030
"sebastian/phpcpd": "*",
31+
"squizlabs/php_codesniffer": "^3.4",
3132
"symfony/thanks": "*"
3233
},
3334
"autoload": {

phpmd.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0"?>
2+
<ruleset
3+
name="laravel"
4+
xmlns="http://pmd.sf.net/ruleset/1.0.0"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
7+
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"
8+
>
9+
<description>
10+
Custom ruleset for Laravel.
11+
</description>
12+
13+
<rule ref="rulesets/cleancode.xml">
14+
<exclude name="StaticAccess" />
15+
</rule>
16+
17+
<rule ref="rulesets/codesize.xml" />
18+
19+
<rule ref="rulesets/controversial.xml" />
20+
21+
<rule ref="rulesets/design.xml" />
22+
23+
<rule ref="rulesets/naming.xml" />
24+
<rule ref="rulesets/naming.xml/LongVariable">
25+
<properties>
26+
<property name="maximum">
27+
<value>36</value>
28+
</property>
29+
</properties>
30+
</rule>
31+
<rule ref="rulesets/naming.xml/ShortMethodName">
32+
<properties>
33+
<property name="minimum">
34+
<value>2</value>
35+
</property>
36+
</properties>
37+
</rule>
38+
<rule ref="rulesets/naming.xml/ShortVariable">
39+
<properties>
40+
<property name="minimum">
41+
<value>2</value>
42+
</property>
43+
</properties>
44+
</rule>
45+
46+
<rule ref="rulesets/unusedcode.xml" />
47+
</ruleset>

tests/Fixtures/Author.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Database\Eloquent\Relations\HasMany;
77
use Illuminate\Database\Eloquent\Relations\HasOne;
8+
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
89
use Illuminate\Database\Eloquent\SoftDeletes;
910

1011
class Author extends Model
@@ -26,6 +27,11 @@ public function books() : HasMany
2627
return $this->hasMany(Book::class);
2728
}
2829

30+
public function printer() : HasOneThrough
31+
{
32+
return $this->hasOneThrough(Printer::class, Book::class);
33+
}
34+
2935
public function profile() : HasOne
3036
{
3137
return $this->hasOne(Profile::class);

tests/Fixtures/Book.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ class Book extends Model
1919
'published_at',
2020
];
2121
protected $fillable = [
22+
"author_id",
2223
'description',
2324
'published_at',
2425
'title',
26+
"publisher_id",
2527
'price',
2628
];
2729

tests/Fixtures/Printer.php

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

tests/Fixtures/UncachedAuthor.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Illuminate\Database\Eloquent\Model;
55
use Illuminate\Database\Eloquent\Relations\HasMany;
66
use Illuminate\Database\Eloquent\Relations\HasOne;
7+
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
78
use Illuminate\Database\Eloquent\SoftDeletes;
89

910
class UncachedAuthor extends Model
@@ -33,6 +34,15 @@ public function getLatestBookAttribute()
3334
->first();
3435
}
3536

37+
public function printer() : HasOneThrough
38+
{
39+
return $this->hasOneThrough(
40+
Printer::class,
41+
Book::class,
42+
"author_id"
43+
);
44+
}
45+
3646
public function profile() : HasOne
3747
{
3848
return $this->hasOne(UncachedProfile::class, 'author_id', 'id');

tests/Fixtures/UncachedAuthorWithInlineGlobalScope.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Illuminate\Database\Eloquent\Model;
55
use Illuminate\Database\Eloquent\Relations\HasMany;
66
use Illuminate\Database\Eloquent\Relations\HasOne;
7+
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
78
use Illuminate\Database\Eloquent\SoftDeletes;
89

910
class UncachedAuthorWithInlineGlobalScope extends Model
@@ -34,6 +35,15 @@ public function books() : HasMany
3435
return $this->hasMany(UncachedBook::class);
3536
}
3637

38+
public function printer() : HasOneThrough
39+
{
40+
return $this->hasOneThrough(
41+
Printer::class,
42+
Book::class,
43+
"author_id"
44+
);
45+
}
46+
3747
public function profile() : HasOne
3848
{
3949
return $this->hasOne(UncachedProfile::class);

tests/Fixtures/UncachedPrinter.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 Illuminate\Database\Eloquent\Model;
4+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
5+
6+
class UncachedPrinter extends Model
7+
{
8+
protected $fillable = [
9+
"book_id",
10+
'name',
11+
];
12+
protected $table = "printer";
13+
14+
public function publisher() : BelongsTo
15+
{
16+
return $this->belongsTo(Publisher::class);
17+
}
18+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
5+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
6+
7+
class HasManyTest extends IntegrationTestCase
8+
{
9+
public function testEagerloadedHasMany()
10+
{
11+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-books.author_id_inraw_1");
12+
$tags = [
13+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
14+
];
15+
16+
$books = (new Author)
17+
->with("books")
18+
->find(1)
19+
->books;
20+
$cachedResults = $this->cache()
21+
->tags($tags)
22+
->get($key)['value'];
23+
$liveResults = (new UncachedAuthor)
24+
->with("books")
25+
->find(1)
26+
->books;
27+
28+
$this->assertEquals($liveResults->pluck("id"), $books->pluck("id"));
29+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
30+
$this->assertNotEmpty($books);
31+
$this->assertNotEmpty($cachedResults);
32+
$this->assertNotEmpty($liveResults);
33+
}
34+
35+
public function testLazyloadedHasMany()
36+
{
37+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-books.author_id_=_1-books.author_id_notnull");
38+
$tags = [
39+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
40+
];
41+
42+
$books = (new Author)
43+
->find(1)
44+
->books;
45+
$cachedResults = $this->cache()
46+
->tags($tags)
47+
->get($key)['value'];
48+
$liveResults = (new UncachedAuthor)
49+
->find(1)
50+
->books;
51+
52+
$this->assertEquals($liveResults->pluck("id"), $books->pluck("id"));
53+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
54+
$this->assertNotEmpty($books);
55+
$this->assertNotEmpty($cachedResults);
56+
$this->assertNotEmpty($liveResults);
57+
}
58+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
7+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
8+
9+
class HasOneThroughTest extends IntegrationTestCase
10+
{
11+
public function testEagerloadedHasOneThrough()
12+
{
13+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-authors.id_=_1-testing:{$this->testingSqlitePath}testing.sqlite:printer-first");
14+
$tags = [
15+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
16+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesprinter",
17+
];
18+
19+
$printer = (new Author)
20+
->with("printer")
21+
->find(1)
22+
->printer;
23+
$cachedResults = $this->cache()
24+
->tags($tags)
25+
->get($key)['value']
26+
->printer;
27+
$liveResults = (new UncachedAuthor)
28+
->with("printer")
29+
->find(1)
30+
->printer;
31+
32+
$this->assertEquals($liveResults->id, $printer->id);
33+
$this->assertEquals($liveResults->id, $cachedResults->id);
34+
$this->assertNotEmpty($printer);
35+
$this->assertNotEmpty($cachedResults);
36+
$this->assertNotEmpty($liveResults);
37+
}
38+
39+
public function testLazyloadedHasMany()
40+
{
41+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-authors.id_=_1-limit_1");
42+
$tags = [
43+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
44+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesprinter",
45+
];
46+
47+
// $printer = (new Author)
48+
// ->find(1)
49+
// ->printer;
50+
// $cachedResults = $this->cache()
51+
// ->tags($tags)
52+
// ->get($key)['value']
53+
// ->printer;
54+
// $liveResults = (new UncachedAuthor)
55+
// ->find(1)
56+
// ->printer;
57+
58+
// $this->assertEquals($liveResults->id, $printer->id);
59+
// $this->assertEquals($liveResults->id, $cachedResults->id);
60+
// $this->assertNotEmpty($printer);
61+
// $this->assertNotEmpty($cachedResults);
62+
// $this->assertNotEmpty($liveResults);
63+
$this->markTestSkipped();
64+
}
65+
}

0 commit comments

Comments
 (0)