Completed
Push — master ( 3f89d7...f790e9 )
by Mike
30:28 queued 29:00
created

testHasRelationshipResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration;
2
3
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
4
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
5
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
6
7
class CachedBuilderRelationshipsTest extends IntegrationTestCase
8
{
9
    public function testHasRelationshipResults()
10
    {
11
        $booksWithStores = (new Book)
12
            ->with("stores")
13
            ->has("stores")
14
            ->get();
15
        $key = "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-exists-and_books.id_=_book_store.book_id-testing:{$this->testingSqlitePath}testing.sqlite:stores";
16
        $tags = [
17
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
18
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesstore",
19
        ];
20
        $cachedResults = $this
21
            ->cache()
22
            ->tags($tags)
23
            ->get(sha1($key))["value"];
24
25
        $this->assertNotEmpty($booksWithStores);
26
        $this->assertEquals($booksWithStores, $cachedResults);
27
    }
28
29
    public function testWhereHasRelationship()
30
    {
31
        $books = (new Book)
32
            ->with("stores")
33
            ->whereHas("stores", function ($query) {
34
                $query->whereRaw('address like ?', ['%s%']);
35
            })
36
            ->get();
37
38
        $uncachedBooks = (new UncachedBook)
39
            ->with("stores")
40
            ->whereHas("stores", function ($query) {
41
                $query->whereRaw('address like ?', ['%s%']);
42
            })
43
            ->get();
44
45
        $this->assertEquals($books->pluck("id"), $uncachedBooks->pluck("id"));
46
    }
47
}
48