. */ declare(strict_types=1); namespace SP\Modules\Web\Controllers\Client; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\Exception; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use SP\Domain\Client\Models\Client; use SP\Infrastructure\Database\QueryResult; use SP\Tests\BodyChecker; use SP\Tests\Generators\ClientGenerator; use SP\Tests\IntegrationTestCase; use Symfony\Component\DomCrawler\Crawler; /** * Class ClientTest */ #[Group('integration')] class ClientTest extends IntegrationTestCase { /** * @throws ContainerExceptionInterface * @throws Exception * @throws NotFoundExceptionInterface */ #[Test] #[BodyChecker('outputCheckerCreate')] public function create() { $container = $this->buildContainer( IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'client/create']) ); IntegrationTestCase::runApp($container); } /** * @throws ContainerExceptionInterface * @throws Exception * @throws NotFoundExceptionInterface */ #[Test] public function deleteMultiple() { $container = $this->buildContainer( IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'client/delete', 'items' => [100, 200, 300]]) ); IntegrationTestCase::runApp($container); $this->expectOutputString('{"status":"OK","description":"Clients deleted","data":null}'); } /** * @throws ContainerExceptionInterface * @throws Exception * @throws NotFoundExceptionInterface */ #[Test] public function deleteSingle() { $container = $this->buildContainer( IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'client/delete/100']) ); IntegrationTestCase::runApp($container); $this->expectOutputString('{"status":"OK","description":"Client deleted","data":null}'); } /** * @throws ContainerExceptionInterface * @throws Exception * @throws NotFoundExceptionInterface */ #[Test] #[BodyChecker('outputCheckerEdit')] public function edit() { $this->addDatabaseMapperResolver( Client::class, new QueryResult([ClientGenerator::factory()->buildClient()]) ); $container = $this->buildContainer( IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'client/edit/100']) ); IntegrationTestCase::runApp($container); } /** * @throws ContainerExceptionInterface * @throws Exception * @throws NotFoundExceptionInterface */ #[Test] public function saveCreate() { $data = [ 'name' => self::$faker->name(), 'description' => self::$faker->text(), 'isglobal' => self::$faker->boolean(), ]; $container = $this->buildContainer( IntegrationTestCase::buildRequest('post', 'index.php', ['r' => 'client/saveCreate'], $data) ); IntegrationTestCase::runApp($container); $this->expectOutputString('{"status":"OK","description":"Client added","data":null}'); } /** * @throws ContainerExceptionInterface * @throws Exception * @throws NotFoundExceptionInterface */ #[Test] public function saveEdit() { $data = [ 'name' => self::$faker->name(), 'description' => self::$faker->text(), 'isglobal' => self::$faker->boolean(), ]; $container = $this->buildContainer( IntegrationTestCase::buildRequest('post', 'index.php', ['r' => 'client/saveEdit/100'], $data) ); IntegrationTestCase::runApp($container); $this->expectOutputString('{"status":"OK","description":"Client updated","data":null}'); } /** * @throws ContainerExceptionInterface * @throws Exception * @throws NotFoundExceptionInterface */ #[Test] #[BodyChecker('outputCheckerSearch')] public function search() { $clientGenerator = ClientGenerator::factory(); $this->addDatabaseMapperResolver( Client::class, QueryResult::withTotalNumRows( [ $clientGenerator->buildClient(), $clientGenerator->buildClient() ], 2 ) ); $container = $this->buildContainer( IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'client/search', 'search' => 'test']) ); IntegrationTestCase::runApp($container); } /** * @throws ContainerExceptionInterface * @throws Exception * @throws NotFoundExceptionInterface */ #[Test] #[BodyChecker('outputCheckerView')] public function view() { $this->addDatabaseMapperResolver( Client::class, new QueryResult([ClientGenerator::factory()->buildClient()]) ); $container = $this->buildContainer( IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'client/view/100']) ); IntegrationTestCase::runApp($container); } /** * @param string $output * @return void */ private function outputCheckerCreate(string $output): void { $json = json_decode($output); $crawler = new Crawler($json->data->html); $filter = $crawler->filterXPath( '//div[@id="box-popup"]//form[@name="frmClients"]//select|//input' )->extract(['_name']); self::assertCount(4, $filter); } /** * @param string $output * @return void */ private function outputCheckerEdit(string $output): void { $json = json_decode($output); $crawler = new Crawler($json->data->html); $filter = $crawler->filterXPath( '//div[@id="box-popup"]//form[@name="frmClients"]//select|//input' )->extract(['_name']); self::assertCount(4, $filter); self::assertEquals('OK', $json->status); } /** * @param string $output * @return void */ private function outputCheckerSearch(string $output): void { $json = json_decode($output); $crawler = new Crawler($json->data->html); $filter = $crawler->filterXPath( '//table/tbody[@id="data-rows-tblClients"]//tr[string-length(@data-item-id) > 0]' )->extract(['data-item-id']); self::assertCount(2, $filter); self::assertEquals('OK', $json->status); } /** * @param string $output * @return void */ private function outputCheckerView(string $output): void { $json = json_decode($output); $crawler = new Crawler($json->data->html); $filter = $crawler->filterXPath( '//div[@id="box-popup"]//form[@name="frmClients"]//select|//input' )->extract(['_name']); self::assertCount(4, $filter); self::assertEquals('OK', $json->status); } }