ArticleControllerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Tests\Controller;
  3. use App\Entity\Article;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Doctrine\ORM\EntityRepository;
  6. use Symfony\Bundle\FrameworkBundle\KernelBrowser;
  7. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  8. final class ArticleControllerTest extends WebTestCase
  9. {
  10. private KernelBrowser $client;
  11. private EntityManagerInterface $manager;
  12. private EntityRepository $repository;
  13. private string $path = '/article/';
  14. protected function setUp(): void
  15. {
  16. $this->client = static::createClient();
  17. $this->manager = static::getContainer()->get('doctrine')->getManager();
  18. $this->repository = $this->manager->getRepository(Article::class);
  19. foreach ($this->repository->findAll() as $object) {
  20. $this->manager->remove($object);
  21. }
  22. $this->manager->flush();
  23. }
  24. public function testIndex(): void
  25. {
  26. $this->client->followRedirects();
  27. $crawler = $this->client->request('GET', $this->path);
  28. self::assertResponseStatusCodeSame(200);
  29. self::assertPageTitleContains('Article index');
  30. // Use the $crawler to perform additional assertions e.g.
  31. // self::assertSame('Some text on the page', $crawler->filter('.p')->first());
  32. }
  33. public function testNew(): void
  34. {
  35. $this->markTestIncomplete();
  36. $this->client->request('GET', sprintf('%snew', $this->path));
  37. self::assertResponseStatusCodeSame(200);
  38. $this->client->submitForm('Save', [
  39. 'article[title]' => 'Testing',
  40. 'article[publicationDate]' => 'Testing',
  41. 'article[content]' => 'Testing',
  42. 'article[author]' => 'Testing',
  43. ]);
  44. self::assertResponseRedirects($this->path);
  45. self::assertSame(1, $this->repository->count([]));
  46. }
  47. public function testShow(): void
  48. {
  49. $this->markTestIncomplete();
  50. $fixture = new Article();
  51. $fixture->setTitle('My Title');
  52. $fixture->setPublicationDate('My Title');
  53. $fixture->setContent('My Title');
  54. $fixture->setAuthor('My Title');
  55. $this->manager->persist($fixture);
  56. $this->manager->flush();
  57. $this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId()));
  58. self::assertResponseStatusCodeSame(200);
  59. self::assertPageTitleContains('Article');
  60. // Use assertions to check that the properties are properly displayed.
  61. }
  62. public function testEdit(): void
  63. {
  64. $this->markTestIncomplete();
  65. $fixture = new Article();
  66. $fixture->setTitle('Value');
  67. $fixture->setPublicationDate('Value');
  68. $fixture->setContent('Value');
  69. $fixture->setAuthor('Value');
  70. $this->manager->persist($fixture);
  71. $this->manager->flush();
  72. $this->client->request('GET', sprintf('%s%s/edit', $this->path, $fixture->getId()));
  73. $this->client->submitForm('Update', [
  74. 'article[title]' => 'Something New',
  75. 'article[publicationDate]' => 'Something New',
  76. 'article[content]' => 'Something New',
  77. 'article[author]' => 'Something New',
  78. ]);
  79. self::assertResponseRedirects('/article/');
  80. $fixture = $this->repository->findAll();
  81. self::assertSame('Something New', $fixture[0]->getTitle());
  82. self::assertSame('Something New', $fixture[0]->getPublicationDate());
  83. self::assertSame('Something New', $fixture[0]->getContent());
  84. self::assertSame('Something New', $fixture[0]->getAuthor());
  85. }
  86. public function testRemove(): void
  87. {
  88. $this->markTestIncomplete();
  89. $fixture = new Article();
  90. $fixture->setTitle('Value');
  91. $fixture->setPublicationDate('Value');
  92. $fixture->setContent('Value');
  93. $fixture->setAuthor('Value');
  94. $this->manager->persist($fixture);
  95. $this->manager->flush();
  96. $this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId()));
  97. $this->client->submitForm('Delete');
  98. self::assertResponseRedirects('/article/');
  99. self::assertSame(0, $this->repository->count([]));
  100. }
  101. }