Completed
Pull Request — master (#372)
by Mike
31:01 queued 16:07
created

WithCountTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 5
dl 0
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testWithCountUpdatesAfterRecordIsAdded() 0 22 1
A testWithCountOnMorphManyRelationshipUpdatesAfterRecordIsAdded() 0 19 1
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\Comment;
6
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
7
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
8
9
class WithCountTest extends IntegrationTestCase
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: artisan, be, call, seed
Loading history...
10
{
11
    public function testWithCountUpdatesAfterRecordIsAdded()
12
    {
13
        $author1 = (new Author)
14
            ->withCount("books")
15
            ->first();
16
        factory(Book::class, 1)
17
            ->make()
18
            ->each(function ($book) use ($author1) {
19
                $publisher = (new Publisher)->first();
20
                $book->author()->associate($author1);
21
                $book->publisher()->associate($publisher);
22
                $book->save();
23
            });
24
25
        $author2 = (new Author)
26
            ->withCount("books")
27
            ->where("id", $author1->id)
28
            ->first();
29
30
        $this->assertNotEquals($author1->books_count, $author2->books_count);
31
        $this->assertEquals($author1->books_count + 1, $author2->books_count);
32
    }
33
34
    public function testWithCountOnMorphManyRelationshipUpdatesAfterRecordIsAdded()
35
    {
36
        $book1 = (new Book)
37
            ->withCount("comments")
38
            ->first();
39
        $comment = factory(Comment::class, 1)
40
            ->create()
41
            ->first();
42
43
        $book1->comments()->save($comment);
44
45
        $book2 = (new Book)
46
            ->withCount("comments")
47
            ->where("id", $book1->id)
48
            ->first();
49
50
        $this->assertNotEquals($book1->comments_count, $book2->comments_count);
51
        $this->assertEquals($book1->comments_count + 1, $book2->comments_count);
52
    }
53
}
54