1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Tests\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class LibraryControllerJsonTest |
9
|
|
|
*/ |
10
|
|
|
class LibraryControllerJsonTest extends WebTestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Test the /api/library/books. |
14
|
|
|
*/ |
15
|
|
|
public function testApiShowLibrary(): void |
16
|
|
|
{ |
17
|
|
|
$client = static::createClient(); |
18
|
|
|
$client->request('GET', '/api/library/books'); |
19
|
|
|
|
20
|
|
|
$this->assertResponseIsSuccessful(); |
21
|
|
|
$this->assertResponseFormatSame('json'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Test the /api/library/book/{isbn} with a valid ISBN. |
26
|
|
|
*/ |
27
|
|
|
public function testApiShowBookIsbn(): void |
28
|
|
|
{ |
29
|
|
|
$client = static::createClient(); |
30
|
|
|
|
31
|
|
|
$isbn = '9780099448785'; |
32
|
|
|
$client->request('GET', "/api/library/book/{$isbn}"); |
33
|
|
|
|
34
|
|
|
$this->assertResponseIsSuccessful(); |
35
|
|
|
$this->assertResponseFormatSame('json'); |
36
|
|
|
|
37
|
|
|
$content = $client->getResponse()->getContent(); |
38
|
|
|
$this->assertIsString($content, 'Response content is not a string.'); |
39
|
|
|
|
40
|
|
|
$data = json_decode($content, true); |
41
|
|
|
$this->assertIsArray($data, 'Decoded JSON is not an array.'); |
42
|
|
|
|
43
|
|
|
$this->assertEquals($isbn, $data['isbn'] ?? null); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Test the /api/library/book/{isbn} with an invalid ISBN. |
48
|
|
|
*/ |
49
|
|
|
public function testApiShowBookIsbnInvalid(): void |
50
|
|
|
{ |
51
|
|
|
$client = static::createClient(); |
52
|
|
|
|
53
|
|
|
$isbn = '1111111-isbn'; |
54
|
|
|
$client->request('GET', "/api/library/book/{$isbn}"); |
55
|
|
|
|
56
|
|
|
$this->assertResponseStatusCodeSame(404); |
57
|
|
|
} |
58
|
|
|
} |