LibraryControllerTest::getMockedClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.8333
1
<?php
2
3
namespace App\Tests\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
use App\Entity\Library;
7
use App\Repository\LibraryRepository;
8
9
/**
10
 * Class LibraryControllerTest
11
 */
12
class LibraryControllerTest extends WebTestCase
13
{
14
    private function getMockedClient(): \Symfony\Bundle\FrameworkBundle\KernelBrowser
15
    {
16
        $book = new Library();
17
        $book->setTitle('Mock Book');
18
        $book->setAuthor('Mock Author');
19
        $book->setIsbn('1234567890');
20
        $book->setBookcover('cover.jpg');
21
        $book->setId(1);
22
23
        $mockRepo = $this->createMock(LibraryRepository::class);
24
        $mockRepo->method('findAll')->willReturn([$book]);
25
        $mockRepo->method('find')->willReturn($book);
26
27
        self::ensureKernelShutdown();
28
        $client = static::createClient();
29
        $client->getContainer()->set(LibraryRepository::class, $mockRepo);
30
31
        return $client;
32
    }
33
34
    /**
35
     * Test the library index.
36
     */
37
    public function testIndexPage(): void
38
    {
39
        $client = $this->getMockedClient();
40
        $client->request('GET', '/library');
41
42
        $this->assertResponseIsSuccessful();
43
        $this->assertSelectorTextContains('h1', 'Library');
44
    }
45
46
    /**
47
     * Test the create book form page.
48
     */
49
    public function testCreateBookForm(): void
50
    {
51
        $client = $this->getMockedClient();
52
        $client->request('GET', '/library/create');
53
54
        $this->assertResponseIsSuccessful();
55
        $this->assertSelectorExists('form');
56
    }
57
58
    public function testViewBookById(): void
59
    {
60
        $client = $this->getMockedClient();
61
        $client->request('GET', '/library/view/1');
62
63
        $this->assertResponseIsSuccessful();
64
        $this->assertSelectorTextContains('body', 'Mock Book');
65
    }
66
67
    public function testViewLibrary(): void
68
    {
69
        $client = $this->getMockedClient();
70
        $client->request('GET', '/library/view');
71
72
        $this->assertResponseIsSuccessful();
73
    }
74
75
    /**
76
     * Test loading the update book form by ID.
77
     */
78
    public function testUpdateBook(): void
79
    {
80
        $client = $this->getMockedClient();
81
        $client->request('GET', '/library/update/1');
82
        $this->assertResponseIsSuccessful();
83
        $this->assertSelectorExists('form');
84
    }
85
86
    /**
87
     * Test the delete book.
88
     */
89
    public function testDeleteBook(): void
90
    {
91
        $client = $this->getMockedClient();
92
        $client->request('GET', '/library/delete/1');
93
94
        $this->assertResponseIsSuccessful();
95
    }
96
97
    /**
98
     * Test non-existent book ID gives 404 error.
99
     */
100
    public function testGetBookNotFound(): void
101
    {
102
        $client = static::createClient();
103
104
        $mockRepo = $this->createMock(LibraryRepository::class);
105
        $mockRepo->method('find')->willReturn(null);
106
        $client->getContainer()->set(LibraryRepository::class, $mockRepo);
107
108
        $client->request('GET', '/library/view/999');
109
        $this->assertResponseStatusCodeSame(404);
110
    }
111
}
112