busigbil /
MVC2025
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Controller; |
||
| 4 | |||
| 5 | use App\Entity\Book; |
||
| 6 | use Doctrine\ORM\EntityManagerInterface; |
||
| 7 | use Doctrine\ORM\Tools\SchemaTool; |
||
| 8 | use Symfony\Component\HttpFoundation\RequestStack; |
||
| 9 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
||
| 10 | use Symfony\Bundle\FrameworkBundle\Console\Application; |
||
| 11 | use Symfony\Component\Console\Input\StringInput; |
||
| 12 | use Symfony\Component\Console\Output\NullOutput; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Test cases for class LibraryController. |
||
| 16 | */ |
||
| 17 | class LibraryControllerTest extends WebTestCase |
||
| 18 | { |
||
| 19 | private static EntityManagerInterface $entityManager; |
||
| 20 | protected static ?int $bookId = null; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Initialize database schema once before test class is run. |
||
| 24 | * Create a book that can be used in all tests. |
||
| 25 | */ |
||
| 26 | public static function setUpBeforeClass(): void |
||
| 27 | { |
||
| 28 | $client = static::createClient(); |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 29 | self::$entityManager = static::getContainer()->get(EntityManagerInterface::class); |
||
| 30 | |||
| 31 | $metadata = self::$entityManager->getMetadataFactory()->getAllMetadata(); |
||
| 32 | if (!empty($metadata)) { |
||
| 33 | $schemaTool = new SchemaTool(self::$entityManager); |
||
| 34 | $schemaTool->dropSchema($metadata); |
||
| 35 | $schemaTool->createSchema($metadata); |
||
| 36 | } |
||
| 37 | |||
| 38 | $book = new Book(); |
||
| 39 | $book->setTitle('Shared Test Book'); |
||
| 40 | $book->setIsbn('9789100194888'); |
||
| 41 | $book->setAuthor('Test Author'); |
||
| 42 | $book->setImage('cover.png'); |
||
| 43 | |||
| 44 | self::$entityManager->persist($book); |
||
| 45 | self::$entityManager->flush(); |
||
| 46 | |||
| 47 | self::$bookId = $book->getId(); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Test instantiation of class |
||
| 52 | */ |
||
| 53 | public function testInstantiateLibraryController(): void |
||
| 54 | { |
||
| 55 | $controller = new LibraryController(); |
||
| 56 | $this->assertInstanceOf("\App\Controller\LibraryController", $controller); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Test route /api/library/books |
||
| 61 | */ |
||
| 62 | public function testControllerJsonBooks(): void |
||
| 63 | { |
||
| 64 | $client = static::createClient(); |
||
| 65 | $client->request('GET', '/api/library/books'); |
||
| 66 | $this->assertResponseIsSuccessful(); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Test route /api/library/book/{isbn<\d+>} |
||
| 71 | */ |
||
| 72 | public function testControllerJsonIsbn(): void |
||
| 73 | { |
||
| 74 | $client = static::createClient(); |
||
| 75 | $client->request('GET', '/api/library/book/9789100194888'); |
||
| 76 | $this->assertResponseIsSuccessful(); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Test route /library |
||
| 81 | */ |
||
| 82 | public function testControllerIndex(): void |
||
| 83 | { |
||
| 84 | $client = static::createClient(); |
||
| 85 | $client->request('GET', '/library'); |
||
| 86 | $this->assertResponseRedirects('/library/show'); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Test route /library/create |
||
| 91 | */ |
||
| 92 | public function testControllerAddBook(): void |
||
| 93 | { |
||
| 94 | $client = static::createClient(); |
||
| 95 | $client->request('GET', '/library/create'); |
||
| 96 | $this->assertResponseIsSuccessful(); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Test route /library/create |
||
| 101 | */ |
||
| 102 | public function testControllerAddBookCallback(): void |
||
| 103 | { |
||
| 104 | $client = static::createClient(); |
||
| 105 | |||
| 106 | // Act — send POST request to your route |
||
| 107 | $client->request('POST', '/library/create', [ |
||
| 108 | 'title' => 'Test Book', |
||
| 109 | 'isbn' => '1234567890', |
||
| 110 | 'author' => 'Test Author', |
||
| 111 | 'image' => 'cover.png', |
||
| 112 | ]); |
||
| 113 | |||
| 114 | // Assert — response should redirect |
||
| 115 | $this->assertResponseRedirects('/library/show'); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Test route /library/show/{id} |
||
| 120 | */ |
||
| 121 | public function testControllerShowBook(): void |
||
| 122 | { |
||
| 123 | $client = static::createClient(); |
||
| 124 | $client->request('GET', '/library/show/' . self::$bookId); |
||
| 125 | $this->assertResponseIsSuccessful(); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Test route /library/show |
||
| 130 | */ |
||
| 131 | public function testControllerShowAll(): void |
||
| 132 | { |
||
| 133 | $client = static::createClient(); |
||
| 134 | $client->request('GET', '/library/show'); |
||
| 135 | $this->assertResponseIsSuccessful(); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Test route /library/update/{id} |
||
| 140 | */ |
||
| 141 | public function testControllerUpdateBook(): void |
||
| 142 | { |
||
| 143 | $client = static::createClient(); |
||
| 144 | $client->request('GET', '/library/update/1'); |
||
| 145 | $this->assertResponseIsSuccessful(); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Test route /library/update/{id} |
||
| 150 | */ |
||
| 151 | public function testControllerUpdateBookCallback(): void |
||
| 152 | { |
||
| 153 | $client = static::createClient(); |
||
| 154 | $client->request('POST', '/library/update/2'); |
||
| 155 | $this->assertResponseRedirects('/library/show'); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Test route /library/delete/{id} |
||
| 160 | */ |
||
| 161 | public function testControllerDeleteBook(): void |
||
| 162 | { |
||
| 163 | $client = static::createClient(); |
||
| 164 | $client->request('POST', '/library/delete/1'); |
||
| 165 | $this->assertResponseRedirects('/library/show'); |
||
| 166 | } |
||
| 167 | } |
||
| 168 |