diff --git a/app/modules/web/Controllers/Account/SearchController.php b/app/modules/web/Controllers/Account/SearchController.php index cbcc8013..ead87362 100644 --- a/app/modules/web/Controllers/Account/SearchController.php +++ b/app/modules/web/Controllers/Account/SearchController.php @@ -24,14 +24,16 @@ namespace SP\Modules\Web\Controllers\Account; - use Exception; use SP\Core\Application; use SP\Core\Events\Event; +use SP\Domain\Core\Exceptions\SPException; use SP\Modules\Web\Controllers\Helpers\Account\AccountSearchHelper; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\WebControllerHelper; +use function SP\processException; + /** * SearchController */ @@ -39,24 +41,17 @@ final class SearchController extends AccountControllerBase { use JsonTrait; - private AccountSearchHelper $accountSearchHelper; - public function __construct( - Application $application, - WebControllerHelper $webControllerHelper, - AccountSearchHelper $accountSearchHelper + Application $application, + WebControllerHelper $webControllerHelper, + private readonly AccountSearchHelper $accountSearchHelper ) { - parent::__construct( - $application, - $webControllerHelper - ); - - $this->accountSearchHelper = $accountSearchHelper; + parent::__construct($application, $webControllerHelper); } /** - * @return bool - * @throws \JsonException + * @return bool|null + * @throws SPException */ public function searchAction(): ?bool { diff --git a/app/modules/web/Controllers/Helpers/Account/AccountSearchHelper.php b/app/modules/web/Controllers/Helpers/Account/AccountSearchHelper.php index 886f3bda..2b811cf2 100644 --- a/app/modules/web/Controllers/Helpers/Account/AccountSearchHelper.php +++ b/app/modules/web/Controllers/Helpers/Account/AccountSearchHelper.php @@ -24,8 +24,6 @@ namespace SP\Modules\Web\Controllers\Helpers\Account; -use DI\DependencyException; -use DI\NotFoundException; use SP\Core\Application; use SP\Domain\Account\Adapters\AccountSearchItem; use SP\Domain\Account\Dtos\AccountSearchFilterDto; @@ -191,8 +189,6 @@ final class AccountSearchHelper extends HelperBase /** * Obtener los resultados de una búsqueda * - * @throws DependencyException - * @throws NotFoundException * @throws ConstraintException * @throws QueryException * @throws SPException diff --git a/app/modules/web/themes/material-blue/views/grid/datagrid-nav-full.inc b/app/modules/web/themes/material-blue/views/grid/datagrid-nav-full.inc index 5043bede..1c510d04 100644 --- a/app/modules/web/themes/material-blue/views/grid/datagrid-nav-full.inc +++ b/app/modules/web/themes/material-blue/views/grid/datagrid-nav-full.inc @@ -1,11 +1,13 @@ getPager(); class="mdl-tooltip mdl-tooltip--top">getIconLast()->getTitle(); ?> - \ No newline at end of file + diff --git a/lib/SP/Domain/User/Dtos/UserDataDto.php b/lib/SP/Domain/User/Dtos/UserDataDto.php index b2ab3ee2..b018a49d 100644 --- a/lib/SP/Domain/User/Dtos/UserDataDto.php +++ b/lib/SP/Domain/User/Dtos/UserDataDto.php @@ -26,6 +26,7 @@ declare(strict_types=1); namespace SP\Domain\User\Dtos; +use SP\Domain\Common\Dtos\Dto; use SP\Domain\Core\Exceptions\SPException; use SP\Domain\User\Models\User; use SP\Domain\User\Models\UserPreferences; @@ -33,14 +34,14 @@ use SP\Domain\User\Models\UserPreferences; /** * Class UserDataDto */ -final readonly class UserDataDto +final class UserDataDto extends Dto { - private ?UserPreferences $preferences; + protected ?UserPreferences $preferences; /** * @throws SPException */ - public function __construct(private ?User $user = null) + public function __construct(private readonly ?User $user = null) { $this->preferences = $this->user?->hydrate(UserPreferences::class); } diff --git a/lib/SP/Html/DataGrid/DataGridDataBase.php b/lib/SP/Html/DataGrid/DataGridDataBase.php index 1405c040..5c1c1041 100644 --- a/lib/SP/Html/DataGrid/DataGridDataBase.php +++ b/lib/SP/Html/DataGrid/DataGridDataBase.php @@ -1,4 +1,5 @@ data->toArray()))); + $queryResult = new self( + SplFixedArray::fromArray(array_map($callable, $this->data->toArray())), + $this->affectedNumRows, + $this->lastId + ); + $queryResult->totalNumRows = $this->totalNumRows; + + return $queryResult; } } diff --git a/tests/SP/Modules/Web/Controllers/Account/SearchControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/SearchControllerTest.php new file mode 100644 index 00000000..cb710ea8 --- /dev/null +++ b/tests/SP/Modules/Web/Controllers/Account/SearchControllerTest.php @@ -0,0 +1,103 @@ +. + */ + +declare(strict_types=1); + +namespace SP\Tests\Modules\Web\Controllers\Account; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\MockObject\Exception; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use SP\Domain\Account\Models\AccountSearchView; +use SP\Domain\Core\Exceptions\InvalidClassException; +use SP\Domain\User\Dtos\UserDataDto; +use SP\Infrastructure\Database\QueryResult; +use SP\Infrastructure\File\FileException; +use SP\Mvc\View\OutputHandlerInterface; +use SP\Tests\Generators\AccountDataGenerator; +use SP\Tests\Generators\UserDataGenerator; +use SP\Tests\IntegrationTestCase; +use Symfony\Component\DomCrawler\Crawler; + +/** + * Class SearchControllerTest + */ +#[Group('integration')] +class SearchControllerTest extends IntegrationTestCase +{ + + /** + * @throws NotFoundExceptionInterface + * @throws Exception + * @throws InvalidClassException + * @throws FileException + * @throws ContainerExceptionInterface + */ + public function testSearchAction() + { + $accountSearchView = AccountDataGenerator::factory()->buildAccountSearchView(); + + $this->addDatabaseResolver( + AccountSearchView::class, + QueryResult::withTotalNumRows([$accountSearchView], 1) + ); + + $definitions = $this->getModuleDefinitions(); + + $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@id="res-content"]/div' + )->extract(['id']); + + assert(!empty($output)); + assert(count($filter) === 4); + + $this->assertTrue(true); + }); + + $container = $this->buildContainer( + $definitions, + $this->buildRequest( + 'post', + 'index.php', + ['r' => 'account/search'], + ['search' => $accountSearchView->getName()] + ) + ); + + $this->expectOutputRegex( + '/\{"status":0,"description":null,"data":\{"html":".*"\},"messages":\[\]\}/' + ); + + $this->runApp($container); + } + + protected function getUserDataDto(): UserDataDto + { + $userPreferences = UserDataGenerator::factory()->buildUserPreferencesData()->mutate(['topNavbar' => true]); + return parent::getUserDataDto()->set('preferences', $userPreferences); + } +} diff --git a/tests/SP/Stubs/OutputHandlerStub.php b/tests/SP/Stubs/OutputHandlerStub.php index d0c2b796..e2ebf1e6 100644 --- a/tests/SP/Stubs/OutputHandlerStub.php +++ b/tests/SP/Stubs/OutputHandlerStub.php @@ -46,8 +46,10 @@ final readonly class OutputHandlerStub implements OutputHandlerInterface ob_start(); $callback(); - ($this->outputChecker)(ob_get_clean()); + $out = ob_get_clean(); - return ''; + ($this->outputChecker)($out); + + return $out; } }