153 lines
5.4 KiB
PHP
153 lines
5.4 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Tests\Controller\Admin\System\StaffingRule;
|
||
|
||
use App\Entity\User;
|
||
use App\Repository\StaffingRuleRepository;
|
||
use App\Tests\Service\Assignment\StaffingFixturesTrait;
|
||
use Doctrine\DBAL\Connection;
|
||
use Doctrine\ORM\EntityManagerInterface;
|
||
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
|
||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||
use Symfony\Component\HttpFoundation\Response;
|
||
|
||
class StaffingRuleControllerTest extends WebTestCase
|
||
{
|
||
use StaffingFixturesTrait;
|
||
|
||
private KernelBrowser $client;
|
||
|
||
private EntityManagerInterface $entityManager;
|
||
|
||
protected function setUp(): void
|
||
{
|
||
$this->client = static::createClient(server: ['HTTPS' => true]);
|
||
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
|
||
|
||
$this->resetStaffingTables(self::getContainer()->get(Connection::class));
|
||
$this->entityManager->createQuery(sprintf('DELETE FROM %s', User::class))->execute();
|
||
|
||
$this->client->loginUser($this->admin());
|
||
}
|
||
|
||
public function testARuleIsCreatedWithItsCodeNormalized(): void
|
||
{
|
||
$this->submitCreate(' dkimp ');
|
||
|
||
$rules = $this->repository()->getList();
|
||
|
||
$this->assertCount(1, $rules);
|
||
$this->assertSame('DKIMP', $rules[0]->getProductCode());
|
||
$this->assertSame(2, $rules[0]->getGuides());
|
||
$this->assertTrue($rules[0]->isSkiBeginner());
|
||
$this->assertFalse($rules[0]->isEnabled());
|
||
|
||
$this->client->request('GET', '/admin/system/staffing-rule');
|
||
|
||
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
|
||
$this->assertStringContainsString('DKIMP', (string) $this->client->getResponse()->getContent());
|
||
}
|
||
|
||
public function testASecondRowForTheSameProductIsRefused(): void
|
||
{
|
||
$this->submitCreate('DKIMP');
|
||
$this->submitCreate('dkimp');
|
||
|
||
$this->assertStringContainsString('Für dieses Produkt gibt es bereits eine Zeile', (string) $this->client->getResponse()->getContent());
|
||
$this->assertCount(1, $this->repository()->getList());
|
||
}
|
||
|
||
public function testADeletedRowFreesItsCode(): void
|
||
{
|
||
$this->submitCreate('DKIMP');
|
||
$this->repository()->getList()[0]->setDeleted();
|
||
$this->entityManager->flush();
|
||
|
||
$this->submitCreate('DKIMP');
|
||
|
||
$this->assertCount(1, $this->repository()->getList());
|
||
}
|
||
|
||
public function testThePreviewRendersWithoutCandidates(): void
|
||
{
|
||
$this->submitCreate('DKIMP');
|
||
|
||
$this->client->request('GET', '/admin/system/staffing-rule/preview');
|
||
|
||
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
|
||
$this->assertStringContainsString('Keine Termine', (string) $this->client->getResponse()->getContent());
|
||
}
|
||
|
||
public function testThePreviewShowsWhatWouldBeCreated(): void
|
||
{
|
||
$this->staffingRule($this->entityManager, 'DKIMP', enabled: false);
|
||
$this->staffingDestination($this->entityManager, 'DKIMP110127', 'DKIMP');
|
||
$this->entityManager->flush();
|
||
|
||
$this->client->request('GET', '/admin/system/staffing-rule/preview');
|
||
|
||
$content = (string) $this->client->getResponse()->getContent();
|
||
|
||
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
|
||
$this->assertStringContainsString('DKIMP110127', $content);
|
||
$this->assertStringContainsString('Zeile inaktiv', $content);
|
||
$this->assertStringContainsString('2× Reiseleitung', $content);
|
||
$this->assertStringContainsString('Busbegleitung ab Göttingen, 22:00 Uhr', $content);
|
||
$this->assertStringContainsString('1× Kursleitung Ski Anfänger:innen', $content);
|
||
$this->assertStringContainsString('Ski-/Snowboardkurs (pro Stunde)', $content);
|
||
}
|
||
|
||
public function testThePreviewDownloadsAsXlsx(): void
|
||
{
|
||
$this->staffingRule($this->entityManager, 'DKIMP');
|
||
$this->staffingDestination($this->entityManager, 'DKIMP110127', 'DKIMP');
|
||
$this->entityManager->flush();
|
||
|
||
$this->client->request('GET', '/admin/system/staffing-rule/export');
|
||
|
||
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
|
||
$this->assertSame(
|
||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||
$this->client->getResponse()->headers->get('Content-Type'),
|
||
);
|
||
}
|
||
|
||
private function submitCreate(string $productCode): void
|
||
{
|
||
$crawler = $this->client->request('GET', '/admin/system/staffing-rule/create');
|
||
$form = $crawler->filter('form')->form([
|
||
'staffing_rule[productCode]' => $productCode,
|
||
'staffing_rule[guides]' => '2',
|
||
]);
|
||
$form['staffing_rule[skiBeginner]']->tick();
|
||
|
||
$this->client->submit($form);
|
||
}
|
||
|
||
private function repository(): StaffingRuleRepository
|
||
{
|
||
$this->entityManager->clear();
|
||
|
||
return self::getContainer()->get(StaffingRuleRepository::class);
|
||
}
|
||
|
||
private function admin(): User
|
||
{
|
||
$user = (new User())
|
||
->setFirstName('Anna')
|
||
->setLastName('Admin')
|
||
->setEmail('[email protected]')
|
||
->setRoles(['ROLE_TEAM_ADMIN'])
|
||
->setBusProAddressId(1)
|
||
->setBusProPersonId(1)
|
||
;
|
||
|
||
$this->entityManager->persist($user);
|
||
$this->entityManager->flush();
|
||
|
||
return $user;
|
||
}
|
||
}
|