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
|
|
|
|