|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace App\Tests\Controller; |
| 15 | + |
| 16 | +use ApiTestCase\ApiTestCase; |
| 17 | +use App\Conference\Entity\Speaker; |
| 18 | +use App\Conference\Factory\SpeakerFactory; |
| 19 | +use Coduo\PHPMatcher\Backtrace\VoidBacktrace; |
| 20 | +use Coduo\PHPMatcher\Matcher; |
| 21 | +use Doctrine\ORM\EntityManagerInterface; |
| 22 | +use Symfony\Component\HttpFoundation\Response; |
| 23 | +use Zenstruck\Foundry\Test\Factories; |
| 24 | +use Zenstruck\Foundry\Test\ResetDatabase; |
| 25 | + |
| 26 | +final class SpeakerUiTest extends ApiTestCase |
| 27 | +{ |
| 28 | + use Factories; |
| 29 | + use ResetDatabase; |
| 30 | + |
| 31 | + /** @test */ |
| 32 | + public function it_allows_browsing_speakers(): void |
| 33 | + { |
| 34 | + SpeakerFactory::new() |
| 35 | + ->withFirstName('Francis') |
| 36 | + ->withLastName('Hilaire') |
| 37 | + ->create() |
| 38 | + ; |
| 39 | + |
| 40 | + SpeakerFactory::new() |
| 41 | + ->withFirstName('Gregor') |
| 42 | + ->withLastName('Šink') |
| 43 | + ->create() |
| 44 | + ; |
| 45 | + |
| 46 | + $this->client->request('GET', '/admin/speakers'); |
| 47 | + $response = $this->client->getResponse(); |
| 48 | + |
| 49 | + $this->assertResponseCode($response, Response::HTTP_OK); |
| 50 | + $content = $response->getContent(); |
| 51 | + |
| 52 | + $this->assertStringContainsString('<td>Francis Hilaire</td>', $content); |
| 53 | + $this->assertStringContainsString('<td>Gregor Šink</td>', $content); |
| 54 | + } |
| 55 | + |
| 56 | + /** @test */ |
| 57 | + public function it_allows_accessing_speaker_creation_page(): void |
| 58 | + { |
| 59 | + $this->client->request('GET', '/admin/speakers/new'); |
| 60 | + |
| 61 | + $this->assertResponseCode($this->client->getResponse(), Response::HTTP_OK); |
| 62 | + } |
| 63 | + |
| 64 | + /** @test */ |
| 65 | + public function it_allows_creating_a_speaker(): void |
| 66 | + { |
| 67 | + $this->client->request('GET', '/admin/speakers/new'); |
| 68 | + $this->client->submitForm('Create', [ |
| 69 | + 'speaker[firstName]' => 'Francis', |
| 70 | + 'speaker[lastName]' => 'Hilaire', |
| 71 | + ]); |
| 72 | + |
| 73 | + $this->assertResponseRedirects(null, expectedCode: Response::HTTP_FOUND); |
| 74 | + |
| 75 | + /** @var Speaker|null $speaker */ |
| 76 | + $speaker = static::getContainer()->get(EntityManagerInterface::class)->getRepository(Speaker::class)->findOneBy(['firstName' => 'Francis']); |
| 77 | + |
| 78 | + $this->assertNotNull($speaker); |
| 79 | + $this->assertSame('Francis Hilaire', $speaker->getFullName()); |
| 80 | + } |
| 81 | + |
| 82 | + /** @test */ |
| 83 | + public function it_allows_updating_a_speaker(): void |
| 84 | + { |
| 85 | + $speaker = SpeakerFactory::createOne(); |
| 86 | + |
| 87 | + $this->client->request('GET', '/admin/speakers/' . $speaker->getId() . '/edit'); |
| 88 | + $this->client->submitForm('Save changes', [ |
| 89 | + 'speaker[firstName]' => 'Francis', |
| 90 | + 'speaker[lastName]' => 'Hilaire', |
| 91 | + ]); |
| 92 | + |
| 93 | + $this->assertResponseRedirects(null, expectedCode: Response::HTTP_FOUND); |
| 94 | + |
| 95 | + $speaker->_refresh(); |
| 96 | + $this->assertSame('Francis Hilaire', $speaker->getFullName()); |
| 97 | + } |
| 98 | + |
| 99 | + /** @test */ |
| 100 | + public function it_allows_deleting_a_speaker(): void |
| 101 | + { |
| 102 | + SpeakerFactory::createOne(); |
| 103 | + |
| 104 | + $this->client->request('GET', '/admin/speakers'); |
| 105 | + $this->client->submitForm('Delete'); |
| 106 | + |
| 107 | + $this->assertResponseRedirects(null, expectedCode: Response::HTTP_FOUND); |
| 108 | + |
| 109 | + /** @var Speaker[] $speakers */ |
| 110 | + $speakers = static::getContainer()->get(EntityManagerInterface::class)->getRepository(Speaker::class)->findAll(); |
| 111 | + |
| 112 | + $this->assertEmpty($speakers); |
| 113 | + } |
| 114 | + |
| 115 | + protected function buildMatcher(): Matcher |
| 116 | + { |
| 117 | + return $this->matcherFactory->createMatcher(new VoidBacktrace()); |
| 118 | + } |
| 119 | +} |
0 commit comments