From e494119be036694a9b5724b68abe8f034230495d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Thu, 22 Aug 2024 16:35:29 +0200 Subject: [PATCH] test(IT): Refactor output checker MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- tests/SP/IntegrationTestCase.php | 13 +- .../AccessManager/IndexControllerTest.php | 38 +- .../Web/Controllers/Account/AccountTest.php | 1098 +++++++++++++++++ .../Account/CopyControllerTest.php | 93 -- .../Account/CopyPassControllerTest.php | 100 -- .../Account/CopyPassHistoryControllerTest.php | 105 -- .../Account/CreateControllerTest.php | 83 -- .../Account/DeleteControllerTest.php | 91 -- .../Account/EditControllerTest.php | 85 -- .../Account/IndexControllerTest.php | 76 -- .../Account/RequestAccessControllerTest.php | 85 -- .../Account/SaveCopyControllerTest.php | 125 -- .../Account/SaveCreateControllerTest.php | 125 -- .../Account/SaveDeleteControllerTest.php | 87 -- .../Account/SaveEditControllerTest.php | 123 -- .../Account/SaveEditPassControllerTest.php | 112 -- .../Account/SaveEditRestoreControllerTest.php | 125 -- .../Account/SaveRequestControllerTest.php | 86 -- .../Account/SearchControllerTest.php | 103 -- .../Account/ViewControllerTest.php | 94 -- .../Account/ViewHistoryControllerTest.php | 102 -- .../Account/ViewLinkControllerTest.php | 104 -- .../Account/ViewPassControllerTest.php | 125 -- .../Account/ViewPassHistoryControllerTest.php | 125 -- .../AccountFile/AccountFileTest.php | 165 +-- .../AccountHistoryManagerTest.php | 93 +- .../AccountManager/AccountManagerTest.php | 10 +- tests/SP/OutputChecker.php | 6 +- 28 files changed, 1267 insertions(+), 2310 deletions(-) create mode 100644 tests/SP/Modules/Web/Controllers/Account/AccountTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/CopyControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/CopyPassControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/CopyPassHistoryControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/CreateControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/DeleteControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/EditControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/IndexControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/RequestAccessControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/SaveCopyControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/SaveCreateControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/SaveDeleteControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/SaveEditControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/SaveEditPassControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/SaveEditRestoreControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/SaveRequestControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/SearchControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/ViewControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/ViewHistoryControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/ViewLinkControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/ViewPassControllerTest.php delete mode 100644 tests/SP/Modules/Web/Controllers/Account/ViewPassHistoryControllerTest.php diff --git a/tests/SP/IntegrationTestCase.php b/tests/SP/IntegrationTestCase.php index b804dd1d..3b798bec 100644 --- a/tests/SP/IntegrationTestCase.php +++ b/tests/SP/IntegrationTestCase.php @@ -37,6 +37,7 @@ use PHPUnit\Framework\TestCase; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; +use ReflectionAttribute; use ReflectionException; use ReflectionMethod; use ReflectionObject; @@ -73,7 +74,6 @@ use SP\Infrastructure\File\FileException; use SP\Infrastructure\File\FileSystem; use SP\Modules\Web\Bootstrap; use SP\Mvc\View\OutputHandlerInterface; -use SP\OutputChecker; use SP\Tests\Generators\UserDataGenerator; use SP\Tests\Generators\UserProfileDataGenerator; use SP\Tests\Stubs\OutputHandlerStub; @@ -253,9 +253,14 @@ abstract class IntegrationTestCase extends TestCase private function getOutputChecker(): ?Closure { $reflection = new ReflectionObject($this); - foreach ($reflection->getMethods(ReflectionMethod::IS_PRIVATE) as $method) { - if (count($method->getAttributes(OutputChecker::class)) > 0) { - return $method->getClosure($this); + foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { + if ($this->name() === $method->name) { + /** @var array> $attributes */ + $attributes = $method->getAttributes(OutputChecker::class); + + if (count($attributes) === 1) { + return (new ReflectionMethod($this, $attributes[0]->newInstance()->target))->getClosure($this); + } } } diff --git a/tests/SP/Modules/Web/Controllers/AccessManager/IndexControllerTest.php b/tests/SP/Modules/Web/Controllers/AccessManager/IndexControllerTest.php index a86caf14..a1e5f150 100644 --- a/tests/SP/Modules/Web/Controllers/AccessManager/IndexControllerTest.php +++ b/tests/SP/Modules/Web/Controllers/AccessManager/IndexControllerTest.php @@ -27,6 +27,7 @@ declare(strict_types=1); namespace SP\Tests\Modules\Web\Controllers\AccessManager; use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\MockObject\Stub; use Psr\Container\ContainerExceptionInterface; @@ -35,8 +36,8 @@ use SP\Domain\Config\Ports\ConfigDataInterface; use SP\Domain\Core\Exceptions\InvalidClassException; use SP\Domain\User\Models\ProfileData; use SP\Infrastructure\File\FileException; -use SP\Mvc\View\OutputHandlerInterface; use SP\Tests\IntegrationTestCase; +use SP\Tests\OutputChecker; use Symfony\Component\DomCrawler\Crawler; /** @@ -52,24 +53,12 @@ class IndexControllerTest extends IntegrationTestCase * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public function testIndexAction() + #[Test] + #[OutputChecker('outputCheckerIndex')] + public function index() { - $definitions = $this->getModuleDefinitions(); - - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath( - '//div[contains(@id, \'tabs-\')]//form' - )->extract(['id']); - - assert(!empty($output)); - assert(count($filter) === 5); - - $this->assertTrue(true); - }); - $container = $this->buildContainer( - $definitions, + $this->getModuleDefinitions(), $this->buildRequest('get', 'index.php', ['r' => 'accessManager/index']) ); @@ -96,4 +85,19 @@ class IndexControllerTest extends IntegrationTestCase return $configData; } + + /** + * @param string $output + * @return void + */ + private function outputCheckerIndex(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[contains(@id, \'tabs-\')]//form' + )->extract(['id']); + + self::assertNotEmpty($output); + self::assertCount(5, $filter); + } } diff --git a/tests/SP/Modules/Web/Controllers/Account/AccountTest.php b/tests/SP/Modules/Web/Controllers/Account/AccountTest.php new file mode 100644 index 00000000..3a4bf1ff --- /dev/null +++ b/tests/SP/Modules/Web/Controllers/Account/AccountTest.php @@ -0,0 +1,1098 @@ +. + */ + +declare(strict_types=1); + +namespace SP\Tests\Modules\Web\Controllers\Account; + +use Defuse\Crypto\Exception\EnvironmentIsBrokenException; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\MockObject\Exception; +use PHPUnit\Framework\MockObject\Stub; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use SP\Core\Crypt\Crypt; +use SP\Core\Crypt\Vault; +use SP\Domain\Account\Adapters\AccountPassItemWithIdAndName; +use SP\Domain\Account\Dtos\PublicLinkKey; +use SP\Domain\Account\Models\Account; +use SP\Domain\Account\Models\AccountHistory; +use SP\Domain\Account\Models\AccountSearchView; +use SP\Domain\Account\Models\AccountView; +use SP\Domain\Account\Models\PublicLink; +use SP\Domain\Common\Models\Item; +use SP\Domain\Common\Models\Simple; +use SP\Domain\Config\Ports\ConfigService; +use SP\Domain\Core\Context\SessionContext; +use SP\Domain\Core\Crypt\CryptInterface; +use SP\Domain\Core\Crypt\VaultInterface; +use SP\Domain\Core\Exceptions\CryptException; +use SP\Domain\Core\Exceptions\InvalidClassException; +use SP\Domain\User\Dtos\UserDto; +use SP\Domain\User\Models\ProfileData; +use SP\Infrastructure\Database\QueryResult; +use SP\Infrastructure\File\FileException; +use SP\Tests\Generators\AccountDataGenerator; +use SP\Tests\Generators\PublicLinkDataGenerator; +use SP\Tests\Generators\UserDataGenerator; +use SP\Tests\IntegrationTestCase; +use SP\Tests\OutputChecker; +use Symfony\Component\DomCrawler\Crawler; + +/** + * Class AccountTest + */ +#[Group('integration')] +class AccountTest extends IntegrationTestCase +{ + private array $definitions; + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + #[OutputChecker('outputCheckerViewPassHistory')] + public function viewPassHistory() + { + $this->addDatabaseMapperResolver( + AccountPassItemWithIdAndName::class, + new QueryResult([ + AccountPassItemWithIdAndName::buildFromSimpleModel( + AccountDataGenerator::factory()->buildAccountDataView() + ) + ]) + ); + $crypt = $this->createStub(CryptInterface::class); + $crypt->method('decrypt')->willReturn('some_data'); + $crypt->method('encrypt')->willReturn('some_data'); + + $this->definitions[CryptInterface::class] = $crypt; + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest( + 'get', + 'index.php', + [ + 'r' => sprintf( + "account/viewPassHistory/id/%d", + self::$faker->randomNumber(3) + ) + ] + ) + ); + + $this->runApp($container); + + $this->expectOutputRegex( + '/\{"status":0,"description":null,"data":\{"useimage":false,"html":".*"\},"messages":\[\]\}/' + ); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + #[OutputChecker('outputCheckerViewPass')] + public function viewPass() + { + $this->addDatabaseMapperResolver( + AccountPassItemWithIdAndName::class, + new QueryResult([ + AccountPassItemWithIdAndName::buildFromSimpleModel( + AccountDataGenerator::factory()->buildAccountDataView() + ) + ]) + ); + $crypt = $this->createStub(CryptInterface::class); + $crypt->method('decrypt')->willReturn('some_data'); + $crypt->method('encrypt')->willReturn('some_data'); + + $this->definitions[CryptInterface::class] = $crypt; + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest( + 'get', + 'index.php', + [ + 'r' => sprintf( + "account/viewPass/id/%d", + self::$faker->randomNumber(3) + ) + ] + ) + ); + + $this->runApp($container); + + $this->expectOutputRegex( + '/\{"status":0,"description":null,"data":\{"useimage":false,"html":".*"\},"messages":\[\]\}/' + ); + } + + /** + * @throws ContainerExceptionInterface + * @throws CryptException + * @throws EnvironmentIsBrokenException + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + #[OutputChecker('outputCheckerViewLink')] + public function viewLink() + { + $account = serialize(Simple::buildFromSimpleModel(AccountDataGenerator::factory()->buildAccount())); + $publicLinkKey = new PublicLinkKey($this->passwordSalt); + + $vault = Vault::factory(new Crypt())->saveData($account, $publicLinkKey->getKey()); + + $publicLink = PublicLinkDataGenerator::factory() + ->buildPublicLink() + ->mutate( + [ + 'dateExpire' => time() + 100, + 'maxCountViews' => 3, + 'countViews' => 0, + 'hash' => $publicLinkKey->getHash(), + 'data' => $vault->getSerialized() + ] + ); + $this->addDatabaseMapperResolver(PublicLink::class, new QueryResult([$publicLink])); + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest('get', 'index.php', ['r' => 'account/viewLink/' . self::$faker->sha1()]) + ); + + $this->runApp($container); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + #[OutputChecker('outputCheckerViewHistory')] + public function viewHistory() + { + $accountHistory = AccountDataGenerator::factory() + ->buildAccountHistoryData() + ->mutate([ + 'userName' => self::$faker->userName(), + 'userGroupName' => self::$faker->userName(), + 'userEditName' => self::$faker->userName(), + 'userEditLogin' => self::$faker->userName(), + ]); + $this->addDatabaseMapperResolver( + AccountHistory::class, + new QueryResult([$accountHistory]) + ); + + $this->addDatabaseMapperResolver( + Item::class, + new QueryResult( + [ + new Item( + ['id' => self::$faker->randomNumber(3), 'name' => self::$faker->colorName()] + ) + ] + ) + ); + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest('get', 'index.php', ['r' => 'account/viewHistory/id/' . self::$faker->randomNumber(3)]) + ); + + $this->runApp($container); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + #[OutputChecker('outputCheckerView')] + public function view() + { + $this->addDatabaseMapperResolver( + AccountView::class, + new QueryResult([AccountDataGenerator::factory()->buildAccountDataView()]) + ); + + $this->addDatabaseMapperResolver( + Item::class, + new QueryResult( + [ + new Item( + ['id' => self::$faker->randomNumber(3), 'name' => self::$faker->colorName()] + ) + ] + ) + ); + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest('get', 'index.php', ['r' => 'account/view/id/' . self::$faker->randomNumber(3)]) + ); + + $this->runApp($container); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + #[OutputChecker('outputCheckerSearch')] + public function search() + { + $accountSearchView = AccountDataGenerator::factory()->buildAccountSearchView(); + + $this->addDatabaseMapperResolver( + AccountSearchView::class, + QueryResult::withTotalNumRows([$accountSearchView], 1) + ); + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest( + 'post', + 'index.php', + ['r' => 'account/search'], + ['search' => $accountSearchView->getName()] + ) + ); + + $this->expectOutputRegex( + '/\{"status":0,"description":null,"data":\{"html":".*"\},"messages":\[\]\}/' + ); + + $this->runApp($container); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + public function saveRequest() + { + $accountDataGenerator = AccountDataGenerator::factory(); + + $this->addDatabaseMapperResolver( + AccountView::class, + new QueryResult([$accountDataGenerator->buildAccountDataView()]) + ); + + $configService = self::createStub(ConfigService::class); + $configService->method('getByParam')->willReturnArgument(0); + + $this->definitions[ConfigService::class] = $configService; + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest( + 'post', + 'index.php', + ['r' => 'account/saveRequest/100'], + ['description' => self::$faker->text()] + ) + ); + + $this->runApp($container); + + $this->expectOutputString( + '{"status":0,"description":"Request done","data":{"itemId":100,"nextAction":"1"},"messages":[]}' + ); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + public function saveEditRestore() + { + $accountDataGenerator = AccountDataGenerator::factory(); + + $this->addDatabaseMapperResolver( + AccountHistory::class, + new QueryResult([$accountDataGenerator->buildAccountHistoryData()]) + ); + + $this->addDatabaseMapperResolver( + Account::class, + new QueryResult([$accountDataGenerator->buildAccount()]) + ); + + $this->addDatabaseMapperResolver( + AccountView::class, + new QueryResult([$accountDataGenerator->buildAccountDataView()]) + ); + + $configService = self::createStub(ConfigService::class); + $configService->method('getByParam')->willReturnArgument(0); + + $this->definitions[ConfigService::class] = $configService; + + $account = $accountDataGenerator->buildAccount(); + + $paramsPost = [ + 'password' => $account->getPass(), + 'password_repeat' => $account->getPass(), + ]; + + $historyId = self::$faker->randomNumber(3); + $accountId = self::$faker->randomNumber(3); + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest( + 'post', + 'index.php', + ['r' => sprintf("account/saveEditRestore/%d/%d", $historyId, $accountId)], + $paramsPost + ) + ); + + $this->runApp($container); + + $this->expectOutputString( + '{"status":0,"description":"Account restored","data":{"itemId":' + . $accountId . + ',"nextAction":"3"},"messages":[]}' + ); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + public function saveEditPass() + { + $accountDataGenerator = AccountDataGenerator::factory(); + + $this->addDatabaseMapperResolver( + Account::class, + new QueryResult([$accountDataGenerator->buildAccount()]) + ); + + $this->addDatabaseMapperResolver( + AccountView::class, + new QueryResult([$accountDataGenerator->buildAccountDataView()]) + ); + + $configService = self::createStub(ConfigService::class); + $configService->method('getByParam')->willReturnArgument(0); + + $this->definitions[ConfigService::class] = $configService; + + $account = $accountDataGenerator->buildAccount(); + + $paramsPost = [ + 'password' => $account->getPass(), + 'password_repeat' => $account->getPass(), + ]; + + $accountId = self::$faker->randomNumber(3); + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest('post', 'index.php', ['r' => 'account/saveEditPass/' . $accountId], $paramsPost) + ); + + $this->runApp($container); + + $this->expectOutputString( + '{"status":0,"description":"Password updated","data":{"itemId":' . $accountId . + ',"nextAction":"3"},"messages":[]}' + ); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + public function saveEdit() + { + $accountDataGenerator = AccountDataGenerator::factory(); + + $this->addDatabaseMapperResolver( + Account::class, + new QueryResult([$accountDataGenerator->buildAccount()]) + ); + + $this->addDatabaseMapperResolver( + AccountView::class, + new QueryResult([$accountDataGenerator->buildAccountDataView()]) + ); + + $configService = self::createStub(ConfigService::class); + $configService->method('getByParam')->willReturnArgument(0); + + $this->definitions[ConfigService::class] = $configService; + + $account = $accountDataGenerator->buildAccount(); + + $paramsPost = [ + 'name' => $account->getName(), + 'login' => $account->getLogin(), + 'client_id' => $account->getClientId(), + 'category_id' => $account->getCategoryId(), + 'password' => $account->getPass(), + 'password_repeat' => $account->getPass(), + 'owner_id' => $account->getUserId(), + 'notes' => $account->getNotes(), + 'private_enabled' => $account->getIsPrivate(), + 'private_group_enabled' => $account->getIsPrivateGroup(), + 'password_date_expire_unix' => $account->getPassDate(), + 'parent_account_id' => $account->getParentId(), + 'main_usergroup_id' => $account->getUserGroupId(), + 'other_users_view_update' => 1, + 'other_users_view' => [1, 2, 3], + 'other_users_edit_update' => 1, + 'other_users_edit' => [4, 5, 6], + 'other_usergroups_view_update' => 1, + 'other_usergroups_view' => [8, 9, 10], + 'other_usergroups_edit_update' => 1, + 'other_usergroups_edit' => [11, 12, 13], + 'tags_update' => 1, + 'tags' => [15, 16, 17], + ]; + + $accountId = self::$faker->randomNumber(3); + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest( + 'post', + 'index.php', + ['r' => 'account/saveEdit/' . $accountId], + $paramsPost + ) + ); + + $this->runApp($container); + + $this->expectOutputString( + '{"status":0,"description":"Account updated","data":{"itemId":' . $accountId . + ',"nextAction":"3"},"messages":[]}' + ); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + public function saveDelete() + { + $accountDataGenerator = AccountDataGenerator::factory(); + + $this->addDatabaseMapperResolver( + AccountView::class, + new QueryResult([$accountDataGenerator->buildAccountDataView()]) + ); + + $this->addDatabaseMapperResolver( + Account::class, + new QueryResult([$accountDataGenerator->buildAccount()]) + ); + + $configService = self::createStub(ConfigService::class); + $configService->method('getByParam')->willReturnArgument(0); + + $this->definitions[ConfigService::class] = $configService; + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest('post', 'index.php', ['r' => 'account/saveDelete/1']) + ); + + $this->runApp($container); + + $this->expectOutputString( + '{"status":0,"description":"Account removed","data":[],"messages":[]}' + ); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + #[OutputChecker('outputCheckerCopy')] + public function copy() + { + $this->addDatabaseMapperResolver( + AccountView::class, + new QueryResult([AccountDataGenerator::factory()->buildAccountDataView()]) + ); + + $this->addDatabaseMapperResolver( + Item::class, + new QueryResult( + [ + new Item( + ['id' => self::$faker->randomNumber(3), 'name' => self::$faker->colorName()] + ) + ] + ) + ); + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest('get', 'index.php', ['r' => 'account/copy/id/' . self::$faker->randomNumber(3)]) + ); + + $this->runApp($container); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + public function copyPass() + { + $this->addDatabaseMapperResolver( + AccountPassItemWithIdAndName::class, + new QueryResult([ + AccountPassItemWithIdAndName::buildFromSimpleModel( + AccountDataGenerator::factory()->buildAccountDataView() + ) + ]) + ); + $crypt = $this->createStub(CryptInterface::class); + $crypt->method('decrypt')->willReturn('some_data'); + $crypt->method('encrypt')->willReturn('some_data'); + + $this->definitions[CryptInterface::class] = $crypt; + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest('get', 'index.php', ['r' => 'account/copyPass/id/' . self::$faker->randomNumber(3)]) + ); + + $this->runApp($container); + + $this->expectOutputString('{"status":0,"description":null,"data":{"accpass":"some_data"},"messages":[]}'); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + public function copyPassHistory() + { + $this->addDatabaseMapperResolver( + AccountPassItemWithIdAndName::class, + new QueryResult([ + AccountPassItemWithIdAndName::buildFromSimpleModel( + AccountDataGenerator::factory()->buildAccountDataView() + ) + ]) + ); + + $crypt = $this->createStub(CryptInterface::class); + $crypt->method('decrypt')->willReturn('some_data'); + $crypt->method('encrypt')->willReturn('some_data'); + + $this->definitions[CryptInterface::class] = $crypt; + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest( + 'get', + 'index.php', + ['r' => 'account/copyPassHistory/id/' . self::$faker->randomNumber(3)] + ) + ); + + $this->runApp($container); + + $this->expectOutputString('{"status":0,"description":null,"data":{"accpass":"some_data"},"messages":[]}'); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + #[OutputChecker('outputCheckerCreate')] + public function create() + { + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest('get', 'index.php', ['r' => 'account/create']) + ); + + $this->runApp($container); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + #[OutputChecker('outputCheckerDelete')] + public function delete() + { + $this->addDatabaseMapperResolver( + AccountView::class, + new QueryResult([AccountDataGenerator::factory()->buildAccountDataView()]) + ); + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest('get', 'index.php', ['r' => 'account/delete/100']) + ); + + $this->runApp($container); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + #[OutputChecker('outputCheckerEdit')] + public function edit() + { + $this->addDatabaseMapperResolver( + AccountView::class, + new QueryResult([AccountDataGenerator::factory()->buildAccountDataView()]) + ); + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest('get', 'index.php', ['r' => 'account/edit/' . self::$faker->randomNumber(3)]) + ); + + $this->runApp($container); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + #[OutputChecker('outputCheckerIndex')] + public function index() + { + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest('get', 'index.php', ['r' => 'account']) + ); + + $this->runApp($container); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + #[OutputChecker('outputCheckerRequestAccess')] + public function requestAccess() + { + $this->addDatabaseMapperResolver( + AccountView::class, + new QueryResult([AccountDataGenerator::factory()->buildAccountDataView()]) + ); + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest('get', 'index.php', ['r' => 'account/requestAccess/' . self::$faker->randomNumber(3)]) + ); + + $this->runApp($container); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + public function saveCopy() + { + $accountDataGenerator = AccountDataGenerator::factory(); + + $this->addDatabaseMapperResolver( + AccountView::class, + new QueryResult([$accountDataGenerator->buildAccountDataView()]) + ); + + $crypt = $this->createStub(CryptInterface::class); + $crypt->method('decrypt')->willReturn('some_data'); + $crypt->method('encrypt')->willReturn('some_data'); + + $this->definitions[CryptInterface::class] = $crypt; + + $account = $accountDataGenerator->buildAccount(); + + $paramsPost = [ + 'name' => $account->getName(), + 'login' => $account->getLogin(), + 'client_id' => $account->getClientId(), + 'category_id' => $account->getCategoryId(), + 'password' => $account->getPass(), + 'password_repeat' => $account->getPass(), + 'owner_id' => $account->getUserId(), + 'notes' => $account->getNotes(), + 'private_enabled' => $account->getIsPrivate(), + 'private_group_enabled' => $account->getIsPrivateGroup(), + 'password_date_expire_unix' => $account->getPassDate(), + 'parent_account_id' => $account->getParentId(), + 'main_usergroup_id' => $account->getUserGroupId(), + 'other_users_view_update' => 1, + 'other_users_view' => [1, 2, 3], + 'other_users_edit_update' => 1, + 'other_users_edit' => [4, 5, 6], + 'other_usergroups_view_update' => 1, + 'other_usergroups_view' => [8, 9, 10], + 'other_usergroups_edit_update' => 1, + 'other_usergroups_edit' => [11, 12, 13], + 'tags_update' => 1, + 'tags' => [15, 16, 17], + ]; + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest('post', 'index.php', ['r' => 'account/saveCopy'], $paramsPost) + ); + + $this->runApp($container); + + $this->expectOutputString( + '{"status":0,"description":"Account created","data":{"itemId":100,"nextAction":"5"},"messages":[]}' + ); + } + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + */ + #[Test] + public function saveCreate() + { + $accountDataGenerator = AccountDataGenerator::factory(); + + $this->addDatabaseMapperResolver( + AccountView::class, + new QueryResult([$accountDataGenerator->buildAccountDataView()]) + ); + + $crypt = $this->createStub(CryptInterface::class); + $crypt->method('decrypt')->willReturn('some_data'); + $crypt->method('encrypt')->willReturn('some_data'); + + $this->definitions[CryptInterface::class] = $crypt; + + $account = $accountDataGenerator->buildAccount(); + + $paramsPost = [ + 'name' => $account->getName(), + 'login' => $account->getLogin(), + 'client_id' => $account->getClientId(), + 'category_id' => $account->getCategoryId(), + 'password' => $account->getPass(), + 'password_repeat' => $account->getPass(), + 'owner_id' => $account->getUserId(), + 'notes' => $account->getNotes(), + 'private_enabled' => $account->getIsPrivate(), + 'private_group_enabled' => $account->getIsPrivateGroup(), + 'password_date_expire_unix' => $account->getPassDate(), + 'parent_account_id' => $account->getParentId(), + 'main_usergroup_id' => $account->getUserGroupId(), + 'other_users_view_update' => 1, + 'other_users_view' => [1, 2, 3], + 'other_users_edit_update' => 1, + 'other_users_edit' => [4, 5, 6], + 'other_usergroups_view_update' => 1, + 'other_usergroups_view' => [8, 9, 10], + 'other_usergroups_edit_update' => 1, + 'other_usergroups_edit' => [11, 12, 13], + 'tags_update' => 1, + 'tags' => [15, 16, 17], + ]; + + $container = $this->buildContainer( + $this->definitions, + $this->buildRequest('post', 'index.php', ['r' => 'account/saveCreate'], $paramsPost) + ); + + $this->runApp($container); + + $this->expectOutputString( + '{"status":0,"description":"Account created","data":{"itemId":100,"nextAction":"5"},"messages":[]}' + ); + } + + /** + * @throws FileException + * @throws InvalidClassException + */ + protected function setUp(): void + { + parent::setUp(); + + $this->definitions = $this->getModuleDefinitions(); + } + + protected function getUserDataDto(): UserDto + { + $userPreferences = UserDataGenerator::factory()->buildUserPreferencesData()->mutate(['topNavbar' => true]); + return parent::getUserDataDto()->mutate(['preferences' => $userPreferences]); + } + + protected function getContext(): SessionContext|Stub + { + $vault = self::createStub(VaultInterface::class); + $vault->method('getData')->willReturn('some_data'); + + $context = parent::getContext(); + $context->method('getVault')->willReturn($vault); + + return $context; + } + + protected function getUserProfile(): ProfileData + { + return new ProfileData( + [ + 'accAdd' => true, + 'accViewPass' => true, + 'accViewHistory' => true, + 'accDelete' => true + ] + ); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerCopy(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@class="data-container"]//form[@name="frmaccount"]|//div[@class="item-actions"]//button' + )->extract(['id']); + + self::assertNotEmpty($output); + self::assertCount(3, $filter); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerViewPassHistory(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@id="box-popup" and @class="box-password-view"]//table//td[starts-with(@class,"dialog-text")]|//button' + )->extract(['_name']); + + self::assertNotEmpty($output); + self::assertCount(4, $filter); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerViewPass(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@id="box-popup" and @class="box-password-view"]//table//td[starts-with(@class,"dialog-text")]|//button' + )->extract(['_name']); + + self::assertNotEmpty($output); + self::assertCount(4, $filter); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerViewLink(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@id="actions" and @class="public-link"]//table[@class="data"]|//div[@class="item-actions"]//button' + )->extract(['id']); + + self::assertNotEmpty($output); + self::assertCount(2, $filter); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerViewHistory(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@class="data-container"]//form[@name="frmaccount"]|//div[@class="item-actions"]//button' + )->extract(['id']); + + self::assertNotEmpty($output); + self::assertCount(2, $filter); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerView(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@class="data-container"]//form[@name="frmaccount"]|//div[@class="item-actions"]//button' + )->extract(['id']); + + self::assertNotEmpty($output); + self::assertCount(2, $filter); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerSearch(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath('//div[@id="res-content"]/div')->extract(['id']); + + self::assertNotEmpty($output); + self::assertCount(4, $filter); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerRequestAccess(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@class="data-container"]//form[@name="requestmodify" and @data-action-route="account/saveRequest"]|//div[@class="item-actions"]//button' + )->extract(['id']); + + self::assertNotEmpty($output); + self::assertCount(3, $filter); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerDelete(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@class="data-container"]//form[@name="frmaccount" and @data-action-route="account/saveDelete"]|//div[@class="item-actions"]//button' + )->extract(['id']); + + self::assertNotEmpty($output); + self::assertCount(2, $filter); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerEdit(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@class="data-container"]//form[@name="frmaccount" and @data-action-route="account/saveEdit"]|//div[@class="item-actions"]//button' + )->extract(['id']); + + self::assertNotEmpty($output); + self::assertCount(3, $filter); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerIndex(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@id="searchbox"]/form[@name="frmSearch"]|//div[@id="res-content"]' + )->extract(['id']); + + self::assertNotEmpty($output); + self::assertCount(2, $filter); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerCreate(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@class="data-container"]//form[@name="frmaccount" and @data-action-route="account/saveCreate"]|//div[@class="item-actions"]//button' + )->extract(['id']); + + self::assertNotEmpty($output); + self::assertCount(3, $filter); + } +} diff --git a/tests/SP/Modules/Web/Controllers/Account/CopyControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/CopyControllerTest.php deleted file mode 100644 index 22c8fa35..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/CopyControllerTest.php +++ /dev/null @@ -1,93 +0,0 @@ -. - */ - -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\AccountView; -use SP\Domain\Common\Models\Item; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Mvc\View\OutputHandlerInterface; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; -use Symfony\Component\DomCrawler\Crawler; - -/** - * Class CopyControllerTest - */ -#[Group('integration')] -class CopyControllerTest extends IntegrationTestCase -{ - /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws FileException - * @throws InvalidClassException - * @throws ContainerExceptionInterface - */ - public function testCopyAction() - { - $this->addDatabaseMapperResolver( - AccountView::class, - new QueryResult([AccountDataGenerator::factory()->buildAccountDataView()]) - ); - - $this->addDatabaseMapperResolver( - Item::class, - new QueryResult( - [ - new Item( - ['id' => self::$faker->randomNumber(3), 'name' => self::$faker->colorName()] - ) - ] - ) - ); - $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath( - '//div[@class="data-container"]//form[@name="frmaccount"]|//div[@class="item-actions"]//button' - )->extract(['id']); - - assert(!empty($output)); - assert(count($filter) === 3); - - $this->assertTrue(true); - }); - - $container = $this->buildContainer( - $definitions, - $this->buildRequest('get', 'index.php', ['r' => 'account/copy/id/' . self::$faker->randomNumber(3)]) - ); - - $this->runApp($container); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/CopyPassControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/CopyPassControllerTest.php deleted file mode 100644 index 9c71ef28..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/CopyPassControllerTest.php +++ /dev/null @@ -1,100 +0,0 @@ -. - */ - -declare(strict_types=1); - -namespace SP\Tests\Modules\Web\Controllers\Account; - -use PHPUnit\Framework\Attributes\Group; -use PHPUnit\Framework\MockObject\Exception; -use PHPUnit\Framework\MockObject\Stub; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; -use SP\Domain\Account\Adapters\AccountPassItemWithIdAndName; -use SP\Domain\Core\Context\SessionContext; -use SP\Domain\Core\Crypt\CryptInterface; -use SP\Domain\Core\Crypt\VaultInterface; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Domain\User\Models\ProfileData; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; - -/** - * Class CopyPassControllerTest - */ -#[Group('integration')] -class CopyPassControllerTest extends IntegrationTestCase -{ - - /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws FileException - * @throws InvalidClassException - * @throws ContainerExceptionInterface - */ - public function testCopyPassAction() - { - $this->addDatabaseMapperResolver( - AccountPassItemWithIdAndName::class, - new QueryResult([ - AccountPassItemWithIdAndName::buildFromSimpleModel( - AccountDataGenerator::factory()->buildAccountDataView() - ) - ]) - ); - $crypt = $this->createStub(CryptInterface::class); - $crypt->method('decrypt')->willReturn('some_data'); - $crypt->method('encrypt')->willReturn('some_data'); - - $definitions = $this->getModuleDefinitions(); - $definitions[CryptInterface::class] = $crypt; - - $container = $this->buildContainer( - $definitions, - $this->buildRequest('get', 'index.php', ['r' => 'account/copyPass/id/' . self::$faker->randomNumber(3)]) - ); - - $this->runApp($container); - - $this->expectOutputString('{"status":0,"description":null,"data":{"accpass":"some_data"},"messages":[]}'); - } - - protected function getContext(): SessionContext|Stub - { - $vault = self::createStub(VaultInterface::class); - - $context = parent::getContext(); - $context->method('getVault')->willReturn($vault); - - return $context; - } - - protected function getUserProfile(): ProfileData - { - return new ProfileData(['accViewPass' => true,]); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/CopyPassHistoryControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/CopyPassHistoryControllerTest.php deleted file mode 100644 index 88488267..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/CopyPassHistoryControllerTest.php +++ /dev/null @@ -1,105 +0,0 @@ -. - */ - -declare(strict_types=1); - -namespace SP\Tests\Modules\Web\Controllers\Account; - -use PHPUnit\Framework\Attributes\Group; -use PHPUnit\Framework\MockObject\Exception; -use PHPUnit\Framework\MockObject\Stub; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; -use SP\Domain\Account\Adapters\AccountPassItemWithIdAndName; -use SP\Domain\Core\Context\SessionContext; -use SP\Domain\Core\Crypt\CryptInterface; -use SP\Domain\Core\Crypt\VaultInterface; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Domain\User\Models\ProfileData; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; - -/** - * Class CopyPassHistoryControllerTest - */ -#[Group('integration')] -class CopyPassHistoryControllerTest extends IntegrationTestCase -{ - - /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws FileException - * @throws InvalidClassException - * @throws ContainerExceptionInterface - */ - public function testCopyPassHistoryAction() - { - $this->addDatabaseMapperResolver( - AccountPassItemWithIdAndName::class, - new QueryResult([ - AccountPassItemWithIdAndName::buildFromSimpleModel( - AccountDataGenerator::factory()->buildAccountDataView() - ) - ]) - ); - - $crypt = $this->createStub(CryptInterface::class); - $crypt->method('decrypt')->willReturn('some_data'); - $crypt->method('encrypt')->willReturn('some_data'); - - $definitions = $this->getModuleDefinitions(); - $definitions[CryptInterface::class] = $crypt; - - $container = $this->buildContainer( - $definitions, - $this->buildRequest( - 'get', - 'index.php', - ['r' => 'account/copyPassHistory/id/' . self::$faker->randomNumber(3)] - ) - ); - - $this->runApp($container); - - $this->expectOutputString('{"status":0,"description":null,"data":{"accpass":"some_data"},"messages":[]}'); - } - - protected function getContext(): SessionContext|Stub - { - $vault = self::createStub(VaultInterface::class); - - $context = parent::getContext(); - $context->method('getVault')->willReturn($vault); - - return $context; - } - - protected function getUserProfile(): ProfileData - { - return new ProfileData(['accViewPass' => true, 'accViewHistory' => true]); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/CreateControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/CreateControllerTest.php deleted file mode 100644 index d25dadfb..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/CreateControllerTest.php +++ /dev/null @@ -1,83 +0,0 @@ -. - */ - -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\Core\Exceptions\InvalidClassException; -use SP\Domain\User\Models\ProfileData; -use SP\Infrastructure\File\FileException; -use SP\Mvc\View\OutputHandlerInterface; -use SP\Tests\IntegrationTestCase; -use Symfony\Component\DomCrawler\Crawler; - -/** - * Class CreateControllerTest - */ -#[Group('integration')] -class CreateControllerTest extends IntegrationTestCase -{ - - /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws FileException - * @throws InvalidClassException - * @throws ContainerExceptionInterface - */ - public function testCreateAction() - { - $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler( - function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath( - '//div[@class="data-container"]//form[@name="frmaccount" and @data-action-route="account/saveCreate"]|//div[@class="item-actions"]//button' - )->extract(['id']); - - assert(!empty($output)); - assert(count($filter) === 3); - - $this->assertTrue(true); - } - ); - - $container = $this->buildContainer( - $definitions, - $this->buildRequest('get', 'index.php', ['r' => 'account/create']) - ); - - $this->runApp($container); - } - - protected function getUserProfile(): ProfileData - { - return new ProfileData(['accAdd' => true,]); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/DeleteControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/DeleteControllerTest.php deleted file mode 100644 index a681b50e..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/DeleteControllerTest.php +++ /dev/null @@ -1,91 +0,0 @@ -. - */ - -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\AccountView; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Domain\User\Models\ProfileData; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Mvc\View\OutputHandlerInterface; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; -use Symfony\Component\DomCrawler\Crawler; - -/** - * Class DeleteControllerTest - */ -#[Group('integration')] -class DeleteControllerTest extends IntegrationTestCase -{ - - /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws FileException - * @throws InvalidClassException - * @throws ContainerExceptionInterface - */ - public function testDeleteAction() - { - $this->addDatabaseMapperResolver( - AccountView::class, - new QueryResult([AccountDataGenerator::factory()->buildAccountDataView()]) - ); - - $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler( - function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath( - '//div[@class="data-container"]//form[@name="frmaccount" and @data-action-route="account/saveDelete"]|//div[@class="item-actions"]//button' - )->extract(['id']); - - assert(!empty($output)); - assert(count($filter) === 2); - - $this->assertTrue(true); - } - ); - - $container = $this->buildContainer( - $definitions, - $this->buildRequest('get', 'index.php', ['r' => 'account/delete/100']) - ); - - $this->runApp($container); - } - - protected function getUserProfile(): ProfileData - { - return new ProfileData(['accDelete' => true]); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/EditControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/EditControllerTest.php deleted file mode 100644 index fd9efdc3..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/EditControllerTest.php +++ /dev/null @@ -1,85 +0,0 @@ -. - */ - -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\AccountView; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Mvc\View\OutputHandlerInterface; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; -use Symfony\Component\DomCrawler\Crawler; - -/** - * Class EditControllerTest - */ -#[Group('integration')] -class EditControllerTest extends IntegrationTestCase -{ - - /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws FileException - * @throws InvalidClassException - * @throws ContainerExceptionInterface - */ - public function testEditAction() - { - $this->addDatabaseMapperResolver( - AccountView::class, - new QueryResult([AccountDataGenerator::factory()->buildAccountDataView()]) - ); - - $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler( - function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath( - '//div[@class="data-container"]//form[@name="frmaccount" and @data-action-route="account/saveEdit"]|//div[@class="item-actions"]//button' - )->extract(['id']); - - assert(!empty($output)); - assert(count($filter) === 3); - - $this->assertTrue(true); - } - ); - - $container = $this->buildContainer( - $definitions, - $this->buildRequest('get', 'index.php', ['r' => 'account/edit/' . self::$faker->randomNumber(3)]) - ); - - $this->runApp($container); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/IndexControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/IndexControllerTest.php deleted file mode 100644 index 8106c011..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/IndexControllerTest.php +++ /dev/null @@ -1,76 +0,0 @@ -. - */ - -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\Core\Exceptions\InvalidClassException; -use SP\Infrastructure\File\FileException; -use SP\Mvc\View\OutputHandlerInterface; -use SP\Tests\IntegrationTestCase; -use Symfony\Component\DomCrawler\Crawler; - -/** - * Class IndexControllerTest - */ -#[Group('integration')] -class IndexControllerTest extends IntegrationTestCase -{ - - /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws InvalidClassException - * @throws FileException - * @throws ContainerExceptionInterface - */ - public function testIndexAction() - { - $definitions = $this->getModuleDefinitions(); - - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath( - '//div[@id="searchbox"]/form[@name="frmSearch"]|//div[@id="res-content"]' - )->extract(['id']); - - assert(!empty($output)); - assert(count($filter) === 2); - - $this->assertTrue(true); - }); - - $container = $this->buildContainer( - $definitions, - $this->buildRequest('get', 'index.php', ['r' => 'account']) - ); - - $this->runApp($container); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/RequestAccessControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/RequestAccessControllerTest.php deleted file mode 100644 index 87e0680a..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/RequestAccessControllerTest.php +++ /dev/null @@ -1,85 +0,0 @@ -. - */ - -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\AccountView; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Mvc\View\OutputHandlerInterface; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; -use Symfony\Component\DomCrawler\Crawler; - -/** - * Class RequestAccessControllerTest - */ -#[Group('integration')] -class RequestAccessControllerTest extends IntegrationTestCase -{ - - /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws InvalidClassException - * @throws FileException - * @throws ContainerExceptionInterface - */ - public function testRequestAccessAction() - { - $this->addDatabaseMapperResolver( - AccountView::class, - new QueryResult([AccountDataGenerator::factory()->buildAccountDataView()]) - ); - - $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler( - function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath( - '//div[@class="data-container"]//form[@name="requestmodify" and @data-action-route="account/saveRequest"]|//div[@class="item-actions"]//button' - )->extract(['id']); - - assert(!empty($output)); - assert(count($filter) === 3); - - $this->assertTrue(true); - } - ); - - $container = $this->buildContainer( - $definitions, - $this->buildRequest('get', 'index.php', ['r' => 'account/requestAccess/' . self::$faker->randomNumber(3)]) - ); - - $this->runApp($container); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/SaveCopyControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/SaveCopyControllerTest.php deleted file mode 100644 index df0ee142..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/SaveCopyControllerTest.php +++ /dev/null @@ -1,125 +0,0 @@ -. - */ - -declare(strict_types=1); - -namespace SP\Tests\Modules\Web\Controllers\Account; - -use PHPUnit\Framework\Attributes\Group; -use PHPUnit\Framework\MockObject\Exception; -use PHPUnit\Framework\MockObject\Stub; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; -use SP\Domain\Account\Models\AccountView; -use SP\Domain\Core\Context\SessionContext; -use SP\Domain\Core\Crypt\CryptInterface; -use SP\Domain\Core\Crypt\VaultInterface; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; - -/** - * Class SaveCopyControllerTest - */ -#[Group('integration')] -class SaveCopyControllerTest extends IntegrationTestCase -{ - - /** - * @throws ContainerExceptionInterface - * @throws Exception - * @throws NotFoundExceptionInterface - * @throws InvalidClassException - * @throws FileException - */ - public function testSaveCopyAction() - { - $accountDataGenerator = AccountDataGenerator::factory(); - - $this->addDatabaseMapperResolver( - AccountView::class, - new QueryResult([$accountDataGenerator->buildAccountDataView()]) - ); - - $crypt = $this->createStub(CryptInterface::class); - $crypt->method('decrypt')->willReturn('some_data'); - $crypt->method('encrypt')->willReturn('some_data'); - - $definitions = $this->getModuleDefinitions(); - $definitions[CryptInterface::class] = $crypt; - - $account = $accountDataGenerator->buildAccount(); - - $paramsPost = [ - 'name' => $account->getName(), - 'login' => $account->getLogin(), - 'client_id' => $account->getClientId(), - 'category_id' => $account->getCategoryId(), - 'password' => $account->getPass(), - 'password_repeat' => $account->getPass(), - 'owner_id' => $account->getUserId(), - 'notes' => $account->getNotes(), - 'private_enabled' => $account->getIsPrivate(), - 'private_group_enabled' => $account->getIsPrivateGroup(), - 'password_date_expire_unix' => $account->getPassDate(), - 'parent_account_id' => $account->getParentId(), - 'main_usergroup_id' => $account->getUserGroupId(), - 'other_users_view_update' => 1, - 'other_users_view' => [1, 2, 3], - 'other_users_edit_update' => 1, - 'other_users_edit' => [4, 5, 6], - 'other_usergroups_view_update' => 1, - 'other_usergroups_view' => [8, 9, 10], - 'other_usergroups_edit_update' => 1, - 'other_usergroups_edit' => [11, 12, 13], - 'tags_update' => 1, - 'tags' => [15, 16, 17], - ]; - - $container = $this->buildContainer( - $definitions, - $this->buildRequest('post', 'index.php', ['r' => 'account/saveCopy'], $paramsPost) - ); - - $this->runApp($container); - - $this->expectOutputString( - '{"status":0,"description":"Account created","data":{"itemId":100,"nextAction":"5"},"messages":[]}' - ); - } - - protected function getContext(): SessionContext|Stub - { - $vault = self::createStub(VaultInterface::class); - $vault->method('getData') - ->willReturn('some_data'); - - $context = parent::getContext(); - $context->method('getVault')->willReturn($vault); - - return $context; - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/SaveCreateControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/SaveCreateControllerTest.php deleted file mode 100644 index 3431c2c0..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/SaveCreateControllerTest.php +++ /dev/null @@ -1,125 +0,0 @@ -. - */ - -declare(strict_types=1); - -namespace SP\Tests\Modules\Web\Controllers\Account; - -use PHPUnit\Framework\Attributes\Group; -use PHPUnit\Framework\MockObject\Exception; -use PHPUnit\Framework\MockObject\Stub; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; -use SP\Domain\Account\Models\AccountView; -use SP\Domain\Core\Context\SessionContext; -use SP\Domain\Core\Crypt\CryptInterface; -use SP\Domain\Core\Crypt\VaultInterface; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; - -/** - * Class SaveCreateControllerTest - */ -#[Group('integration')] -class SaveCreateControllerTest extends IntegrationTestCase -{ - - /** - * @throws ContainerExceptionInterface - * @throws Exception - * @throws NotFoundExceptionInterface - * @throws InvalidClassException - * @throws FileException - */ - public function testSaveCreateAction() - { - $accountDataGenerator = AccountDataGenerator::factory(); - - $this->addDatabaseMapperResolver( - AccountView::class, - new QueryResult([$accountDataGenerator->buildAccountDataView()]) - ); - - $crypt = $this->createStub(CryptInterface::class); - $crypt->method('decrypt')->willReturn('some_data'); - $crypt->method('encrypt')->willReturn('some_data'); - - $definitions = $this->getModuleDefinitions(); - $definitions[CryptInterface::class] = $crypt; - - $account = $accountDataGenerator->buildAccount(); - - $paramsPost = [ - 'name' => $account->getName(), - 'login' => $account->getLogin(), - 'client_id' => $account->getClientId(), - 'category_id' => $account->getCategoryId(), - 'password' => $account->getPass(), - 'password_repeat' => $account->getPass(), - 'owner_id' => $account->getUserId(), - 'notes' => $account->getNotes(), - 'private_enabled' => $account->getIsPrivate(), - 'private_group_enabled' => $account->getIsPrivateGroup(), - 'password_date_expire_unix' => $account->getPassDate(), - 'parent_account_id' => $account->getParentId(), - 'main_usergroup_id' => $account->getUserGroupId(), - 'other_users_view_update' => 1, - 'other_users_view' => [1, 2, 3], - 'other_users_edit_update' => 1, - 'other_users_edit' => [4, 5, 6], - 'other_usergroups_view_update' => 1, - 'other_usergroups_view' => [8, 9, 10], - 'other_usergroups_edit_update' => 1, - 'other_usergroups_edit' => [11, 12, 13], - 'tags_update' => 1, - 'tags' => [15, 16, 17], - ]; - - $container = $this->buildContainer( - $definitions, - $this->buildRequest('post', 'index.php', ['r' => 'account/saveCreate'], $paramsPost) - ); - - $this->runApp($container); - - $this->expectOutputString( - '{"status":0,"description":"Account created","data":{"itemId":100,"nextAction":"5"},"messages":[]}' - ); - } - - protected function getContext(): SessionContext|Stub - { - $vault = self::createStub(VaultInterface::class); - $vault->method('getData') - ->willReturn('some_data'); - - $context = parent::getContext(); - $context->method('getVault')->willReturn($vault); - - return $context; - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/SaveDeleteControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/SaveDeleteControllerTest.php deleted file mode 100644 index ed687fab..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/SaveDeleteControllerTest.php +++ /dev/null @@ -1,87 +0,0 @@ -. - */ - -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\Account; -use SP\Domain\Account\Models\AccountView; -use SP\Domain\Config\Ports\ConfigService; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; - -/** - * Class SaveDeleteControllerTest - */ -#[Group('integration')] -class SaveDeleteControllerTest extends IntegrationTestCase -{ - - /** - * @throws ContainerExceptionInterface - * @throws Exception - * @throws NotFoundExceptionInterface - * @throws InvalidClassException - * @throws FileException - */ - public function testSaveDeleteAction() - { - $accountDataGenerator = AccountDataGenerator::factory(); - - $this->addDatabaseMapperResolver( - AccountView::class, - new QueryResult([$accountDataGenerator->buildAccountDataView()]) - ); - - $this->addDatabaseMapperResolver( - Account::class, - new QueryResult([$accountDataGenerator->buildAccount()]) - ); - - $configService = self::createStub(ConfigService::class); - $configService->method('getByParam')->willReturnArgument(0); - - $definitions = $this->getModuleDefinitions(); - $definitions[ConfigService::class] = $configService; - - $container = $this->buildContainer( - $definitions, - $this->buildRequest('post', 'index.php', ['r' => 'account/saveDelete/1']) - ); - - $this->runApp($container); - - $this->expectOutputString( - '{"status":0,"description":"Account removed","data":[],"messages":[]}' - ); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/SaveEditControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/SaveEditControllerTest.php deleted file mode 100644 index 25da1257..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/SaveEditControllerTest.php +++ /dev/null @@ -1,123 +0,0 @@ -. - */ - -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\Account; -use SP\Domain\Account\Models\AccountView; -use SP\Domain\Config\Ports\ConfigService; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; - -/** - * Class SaveEditControllerTest - */ -#[Group('integration')] -class SaveEditControllerTest extends IntegrationTestCase -{ - - /** - * @throws ContainerExceptionInterface - * @throws Exception - * @throws NotFoundExceptionInterface - * @throws InvalidClassException - * @throws FileException - */ - public function testSaveEditAction() - { - $accountDataGenerator = AccountDataGenerator::factory(); - - $this->addDatabaseMapperResolver( - Account::class, - new QueryResult([$accountDataGenerator->buildAccount()]) - ); - - $this->addDatabaseMapperResolver( - AccountView::class, - new QueryResult([$accountDataGenerator->buildAccountDataView()]) - ); - - $configService = self::createStub(ConfigService::class); - $configService->method('getByParam')->willReturnArgument(0); - - $definitions = $this->getModuleDefinitions(); - $definitions[ConfigService::class] = $configService; - - $account = $accountDataGenerator->buildAccount(); - - $paramsPost = [ - 'name' => $account->getName(), - 'login' => $account->getLogin(), - 'client_id' => $account->getClientId(), - 'category_id' => $account->getCategoryId(), - 'password' => $account->getPass(), - 'password_repeat' => $account->getPass(), - 'owner_id' => $account->getUserId(), - 'notes' => $account->getNotes(), - 'private_enabled' => $account->getIsPrivate(), - 'private_group_enabled' => $account->getIsPrivateGroup(), - 'password_date_expire_unix' => $account->getPassDate(), - 'parent_account_id' => $account->getParentId(), - 'main_usergroup_id' => $account->getUserGroupId(), - 'other_users_view_update' => 1, - 'other_users_view' => [1, 2, 3], - 'other_users_edit_update' => 1, - 'other_users_edit' => [4, 5, 6], - 'other_usergroups_view_update' => 1, - 'other_usergroups_view' => [8, 9, 10], - 'other_usergroups_edit_update' => 1, - 'other_usergroups_edit' => [11, 12, 13], - 'tags_update' => 1, - 'tags' => [15, 16, 17], - ]; - - $accountId = self::$faker->randomNumber(3); - - $container = $this->buildContainer( - $definitions, - $this->buildRequest( - 'post', - 'index.php', - ['r' => 'account/saveEdit/' . $accountId], - $paramsPost - ) - ); - - $this->runApp($container); - - $this->expectOutputString( - '{"status":0,"description":"Account updated","data":{"itemId":' . $accountId . - ',"nextAction":"3"},"messages":[]}' - ); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/SaveEditPassControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/SaveEditPassControllerTest.php deleted file mode 100644 index 600da1b5..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/SaveEditPassControllerTest.php +++ /dev/null @@ -1,112 +0,0 @@ -. - */ - -declare(strict_types=1); - -namespace SP\Tests\Modules\Web\Controllers\Account; - -use PHPUnit\Framework\Attributes\Group; -use PHPUnit\Framework\MockObject\Exception; -use PHPUnit\Framework\MockObject\Stub; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; -use SP\Domain\Account\Models\Account; -use SP\Domain\Account\Models\AccountView; -use SP\Domain\Config\Ports\ConfigService; -use SP\Domain\Core\Context\SessionContext; -use SP\Domain\Core\Crypt\VaultInterface; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; - -/** - * Class SaveCreateControllerTest - */ -#[Group('integration')] -class SaveEditPassControllerTest extends IntegrationTestCase -{ - - /** - * @throws ContainerExceptionInterface - * @throws Exception - * @throws NotFoundExceptionInterface - * @throws InvalidClassException - * @throws FileException - */ - public function testSaveEditPassAction() - { - $accountDataGenerator = AccountDataGenerator::factory(); - - $this->addDatabaseMapperResolver( - Account::class, - new QueryResult([$accountDataGenerator->buildAccount()]) - ); - - $this->addDatabaseMapperResolver( - AccountView::class, - new QueryResult([$accountDataGenerator->buildAccountDataView()]) - ); - - $configService = self::createStub(ConfigService::class); - $configService->method('getByParam')->willReturnArgument(0); - - $definitions = $this->getModuleDefinitions(); - $definitions[ConfigService::class] = $configService; - - $account = $accountDataGenerator->buildAccount(); - - $paramsPost = [ - 'password' => $account->getPass(), - 'password_repeat' => $account->getPass(), - ]; - - $accountId = self::$faker->randomNumber(3); - - $container = $this->buildContainer( - $definitions, - $this->buildRequest('post', 'index.php', ['r' => 'account/saveEditPass/' . $accountId], $paramsPost) - ); - - $this->runApp($container); - - $this->expectOutputString( - '{"status":0,"description":"Password updated","data":{"itemId":' . $accountId . - ',"nextAction":"3"},"messages":[]}' - ); - } - - protected function getContext(): SessionContext|Stub - { - $vault = self::createStub(VaultInterface::class); - $vault->method('getData') - ->willReturn('some_data'); - - $context = parent::getContext(); - $context->method('getVault')->willReturn($vault); - - return $context; - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/SaveEditRestoreControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/SaveEditRestoreControllerTest.php deleted file mode 100644 index 8b0b9ee0..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/SaveEditRestoreControllerTest.php +++ /dev/null @@ -1,125 +0,0 @@ -. - */ - -declare(strict_types=1); - -namespace SP\Tests\Modules\Web\Controllers\Account; - -use PHPUnit\Framework\Attributes\Group; -use PHPUnit\Framework\MockObject\Exception; -use PHPUnit\Framework\MockObject\Stub; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; -use SP\Domain\Account\Models\Account; -use SP\Domain\Account\Models\AccountHistory; -use SP\Domain\Account\Models\AccountView; -use SP\Domain\Config\Ports\ConfigService; -use SP\Domain\Core\Context\SessionContext; -use SP\Domain\Core\Crypt\VaultInterface; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; - -/** - * Class SaveEditRestoreControllerTest - */ -#[Group('integration')] -class SaveEditRestoreControllerTest extends IntegrationTestCase -{ - - /** - * @throws ContainerExceptionInterface - * @throws Exception - * @throws NotFoundExceptionInterface - * @throws InvalidClassException - * @throws FileException - */ - public function testSaveEditRestoreAction() - { - $accountDataGenerator = AccountDataGenerator::factory(); - - $this->addDatabaseMapperResolver( - AccountHistory::class, - new QueryResult([$accountDataGenerator->buildAccountHistoryData()]) - ); - - $this->addDatabaseMapperResolver( - Account::class, - new QueryResult([$accountDataGenerator->buildAccount()]) - ); - - $this->addDatabaseMapperResolver( - AccountView::class, - new QueryResult([$accountDataGenerator->buildAccountDataView()]) - ); - - $configService = self::createStub(ConfigService::class); - $configService->method('getByParam')->willReturnArgument(0); - - $definitions = $this->getModuleDefinitions(); - $definitions[ConfigService::class] = $configService; - - $account = $accountDataGenerator->buildAccount(); - - $paramsPost = [ - 'password' => $account->getPass(), - 'password_repeat' => $account->getPass(), - ]; - - $historyId = self::$faker->randomNumber(3); - $accountId = self::$faker->randomNumber(3); - - $container = $this->buildContainer( - $definitions, - $this->buildRequest( - 'post', - 'index.php', - ['r' => sprintf("account/saveEditRestore/%d/%d", $historyId, $accountId)], - $paramsPost - ) - ); - - $this->runApp($container); - - $this->expectOutputString( - '{"status":0,"description":"Account restored","data":{"itemId":' - . $accountId . - ',"nextAction":"3"},"messages":[]}' - ); - } - - protected function getContext(): SessionContext|Stub - { - $vault = self::createStub(VaultInterface::class); - $vault->method('getData') - ->willReturn('some_data'); - - $context = parent::getContext(); - $context->method('getVault')->willReturn($vault); - - return $context; - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/SaveRequestControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/SaveRequestControllerTest.php deleted file mode 100644 index 0897c023..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/SaveRequestControllerTest.php +++ /dev/null @@ -1,86 +0,0 @@ -. - */ - -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\AccountView; -use SP\Domain\Config\Ports\ConfigService; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; - -/** - * Class SaveRequestControllerTest - */ -#[Group('integration')] -class SaveRequestControllerTest extends IntegrationTestCase -{ - - /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws InvalidClassException - * @throws FileException - * @throws ContainerExceptionInterface - */ - public function testSaveRequestAction() - { - $accountDataGenerator = AccountDataGenerator::factory(); - - $this->addDatabaseMapperResolver( - AccountView::class, - new QueryResult([$accountDataGenerator->buildAccountDataView()]) - ); - - $configService = self::createStub(ConfigService::class); - $configService->method('getByParam')->willReturnArgument(0); - - $definitions = $this->getModuleDefinitions(); - $definitions[ConfigService::class] = $configService; - - $container = $this->buildContainer( - $definitions, - $this->buildRequest( - 'post', - 'index.php', - ['r' => 'account/saveRequest/100'], - ['description' => self::$faker->text()] - ) - ); - - $this->runApp($container); - - $this->expectOutputString( - '{"status":0,"description":"Request done","data":{"itemId":100,"nextAction":"1"},"messages":[]}' - ); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/SearchControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/SearchControllerTest.php deleted file mode 100644 index 32f2262f..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/SearchControllerTest.php +++ /dev/null @@ -1,103 +0,0 @@ -. - */ - -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\UserDto; -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->addDatabaseMapperResolver( - 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(): UserDto - { - $userPreferences = UserDataGenerator::factory()->buildUserPreferencesData()->mutate(['topNavbar' => true]); - return parent::getUserDataDto()->mutate(['preferences' => $userPreferences]); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/ViewControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/ViewControllerTest.php deleted file mode 100644 index c237b3a6..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/ViewControllerTest.php +++ /dev/null @@ -1,94 +0,0 @@ -. - */ - -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\AccountView; -use SP\Domain\Common\Models\Item; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Mvc\View\OutputHandlerInterface; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; -use Symfony\Component\DomCrawler\Crawler; - -/** - * Class ViewControllerTest - */ -#[Group('integration')] -class ViewControllerTest extends IntegrationTestCase -{ - - /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws InvalidClassException - * @throws FileException - * @throws ContainerExceptionInterface - */ - public function testViewAction() - { - $this->addDatabaseMapperResolver( - AccountView::class, - new QueryResult([AccountDataGenerator::factory()->buildAccountDataView()]) - ); - - $this->addDatabaseMapperResolver( - Item::class, - new QueryResult( - [ - new Item( - ['id' => self::$faker->randomNumber(3), 'name' => self::$faker->colorName()] - ) - ] - ) - ); - $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath( - '//div[@class="data-container"]//form[@name="frmaccount"]|//div[@class="item-actions"]//button' - )->extract(['id']); - - assert(!empty($output)); - assert(count($filter) === 2); - - $this->assertTrue(true); - }); - - $container = $this->buildContainer( - $definitions, - $this->buildRequest('get', 'index.php', ['r' => 'account/view/id/' . self::$faker->randomNumber(3)]) - ); - - $this->runApp($container); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/ViewHistoryControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/ViewHistoryControllerTest.php deleted file mode 100644 index 8cc74bde..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/ViewHistoryControllerTest.php +++ /dev/null @@ -1,102 +0,0 @@ -. - */ - -declare(strict_types=1); - -namespace SP\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\AccountHistory; -use SP\Domain\Common\Models\Item; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Mvc\View\OutputHandlerInterface; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; -use Symfony\Component\DomCrawler\Crawler; - -/** - * Class ViewHistoryControllerTest - */ -#[Group('integration')] -class ViewHistoryControllerTest extends IntegrationTestCase -{ - - /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws InvalidClassException - * @throws FileException - * @throws ContainerExceptionInterface - */ - public function testViewHistoryAction() - { - $accountHistory = AccountDataGenerator::factory() - ->buildAccountHistoryData() - ->mutate([ - 'userName' => self::$faker->userName(), - 'userGroupName' => self::$faker->userName(), - 'userEditName' => self::$faker->userName(), - 'userEditLogin' => self::$faker->userName(), - ]); - $this->addDatabaseMapperResolver( - AccountHistory::class, - new QueryResult([$accountHistory]) - ); - - $this->addDatabaseMapperResolver( - Item::class, - new QueryResult( - [ - new Item( - ['id' => self::$faker->randomNumber(3), 'name' => self::$faker->colorName()] - ) - ] - ) - ); - $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath( - '//div[@class="data-container"]//form[@name="frmaccount"]|//div[@class="item-actions"]//button' - )->extract(['id']); - - assert(!empty($output)); - assert(count($filter) === 2); - - $this->assertTrue(true); - }); - - $container = $this->buildContainer( - $definitions, - $this->buildRequest('get', 'index.php', ['r' => 'account/viewHistory/id/' . self::$faker->randomNumber(3)]) - ); - - $this->runApp($container); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/ViewLinkControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/ViewLinkControllerTest.php deleted file mode 100644 index da840d12..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/ViewLinkControllerTest.php +++ /dev/null @@ -1,104 +0,0 @@ -. - */ - -declare(strict_types=1); - -namespace SP\Tests\Modules\Web\Controllers\Account; - -use Defuse\Crypto\Exception\EnvironmentIsBrokenException; -use PHPUnit\Framework\Attributes\Group; -use PHPUnit\Framework\MockObject\Exception; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; -use SP\Core\Crypt\Crypt; -use SP\Core\Crypt\Vault; -use SP\Domain\Account\Dtos\PublicLinkKey; -use SP\Domain\Account\Models\PublicLink; -use SP\Domain\Common\Models\Simple; -use SP\Domain\Core\Exceptions\CryptException; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Mvc\View\OutputHandlerInterface; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\Generators\PublicLinkDataGenerator; -use SP\Tests\IntegrationTestCase; -use Symfony\Component\DomCrawler\Crawler; - -/** - * Class ViewLinkControllerTest - */ -#[Group('integration')] -class ViewLinkControllerTest extends IntegrationTestCase -{ - /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws FileException - * @throws InvalidClassException - * @throws ContainerExceptionInterface - * @throws CryptException - * @throws EnvironmentIsBrokenException - */ - public function testViewLinkAction() - { - $account = serialize(Simple::buildFromSimpleModel(AccountDataGenerator::factory()->buildAccount())); - $publicLinkKey = new PublicLinkKey($this->passwordSalt); - - $vault = Vault::factory(new Crypt())->saveData($account, $publicLinkKey->getKey()); - - $publicLink = PublicLinkDataGenerator::factory() - ->buildPublicLink() - ->mutate( - [ - 'dateExpire' => time() + 100, - 'maxCountViews' => 3, - 'countViews' => 0, - 'hash' => $publicLinkKey->getHash(), - 'data' => $vault->getSerialized() - ] - ); - $this->addDatabaseMapperResolver(PublicLink::class, new QueryResult([$publicLink])); - - $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath( - '//div[@id="actions" and @class="public-link"]//table[@class="data"]|//div[@class="item-actions"]//button' - )->extract(['id']); - - assert(!empty($output)); - assert(count($filter) === 2); - - $this->assertTrue(true); - }); - - $container = $this->buildContainer( - $definitions, - $this->buildRequest('get', 'index.php', ['r' => 'account/viewLink/' . self::$faker->sha1()]) - ); - - $this->runApp($container); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/ViewPassControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/ViewPassControllerTest.php deleted file mode 100644 index 00407875..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/ViewPassControllerTest.php +++ /dev/null @@ -1,125 +0,0 @@ -. - */ - -declare(strict_types=1); - -namespace SP\Tests\Modules\Web\Controllers\Account; - -use PHPUnit\Framework\Attributes\Group; -use PHPUnit\Framework\MockObject\Exception; -use PHPUnit\Framework\MockObject\Stub; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; -use SP\Domain\Account\Adapters\AccountPassItemWithIdAndName; -use SP\Domain\Core\Context\SessionContext; -use SP\Domain\Core\Crypt\CryptInterface; -use SP\Domain\Core\Crypt\VaultInterface; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Domain\User\Models\ProfileData; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Mvc\View\OutputHandlerInterface; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; -use Symfony\Component\DomCrawler\Crawler; - -/** - * Class ViewPassControllerTest - */ -#[Group('integration')] -class ViewPassControllerTest extends IntegrationTestCase -{ - - /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws FileException - * @throws InvalidClassException - * @throws ContainerExceptionInterface - */ - public function testViewPassAction() - { - $this->addDatabaseMapperResolver( - AccountPassItemWithIdAndName::class, - new QueryResult([ - AccountPassItemWithIdAndName::buildFromSimpleModel( - AccountDataGenerator::factory()->buildAccountDataView() - ) - ]) - ); - $crypt = $this->createStub(CryptInterface::class); - $crypt->method('decrypt')->willReturn('some_data'); - $crypt->method('encrypt')->willReturn('some_data'); - - $definitions = $this->getModuleDefinitions(); - $definitions[CryptInterface::class] = $crypt; - - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath( - '//div[@id="box-popup" and @class="box-password-view"]//table//td[starts-with(@class,"dialog-text")]|//button' - )->extract(['_name']); - - assert(!empty($output)); - assert(count($filter) === 4); - - $this->assertTrue(true); - }); - - $container = $this->buildContainer( - $definitions, - $this->buildRequest( - 'get', - 'index.php', - [ - 'r' => sprintf( - "account/viewPass/id/%d", - self::$faker->randomNumber(3) - ) - ] - ) - ); - - $this->runApp($container); - - $this->expectOutputRegex( - '/\{"status":0,"description":null,"data":\{"useimage":false,"html":".*"\},"messages":\[\]\}/' - ); - } - - protected function getContext(): SessionContext|Stub - { - $vault = self::createStub(VaultInterface::class); - - $context = parent::getContext(); - $context->method('getVault')->willReturn($vault); - - return $context; - } - - protected function getUserProfile(): ProfileData - { - return new ProfileData(['accViewPass' => true,]); - } -} diff --git a/tests/SP/Modules/Web/Controllers/Account/ViewPassHistoryControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/ViewPassHistoryControllerTest.php deleted file mode 100644 index 712d30d7..00000000 --- a/tests/SP/Modules/Web/Controllers/Account/ViewPassHistoryControllerTest.php +++ /dev/null @@ -1,125 +0,0 @@ -. - */ - -declare(strict_types=1); - -namespace SP\Modules\Web\Controllers\Account; - -use PHPUnit\Framework\Attributes\Group; -use PHPUnit\Framework\MockObject\Exception; -use PHPUnit\Framework\MockObject\Stub; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; -use SP\Domain\Account\Adapters\AccountPassItemWithIdAndName; -use SP\Domain\Core\Context\SessionContext; -use SP\Domain\Core\Crypt\CryptInterface; -use SP\Domain\Core\Crypt\VaultInterface; -use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Domain\User\Models\ProfileData; -use SP\Infrastructure\Database\QueryResult; -use SP\Infrastructure\File\FileException; -use SP\Mvc\View\OutputHandlerInterface; -use SP\Tests\Generators\AccountDataGenerator; -use SP\Tests\IntegrationTestCase; -use Symfony\Component\DomCrawler\Crawler; - -/** - * Class ViewPassHistoryControllerTest - */ -#[Group('integration')] -class ViewPassHistoryControllerTest extends IntegrationTestCase -{ - - /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws FileException - * @throws InvalidClassException - * @throws ContainerExceptionInterface - */ - public function testViewPassHistoryAction() - { - $this->addDatabaseMapperResolver( - AccountPassItemWithIdAndName::class, - new QueryResult([ - AccountPassItemWithIdAndName::buildFromSimpleModel( - AccountDataGenerator::factory()->buildAccountDataView() - ) - ]) - ); - $crypt = $this->createStub(CryptInterface::class); - $crypt->method('decrypt')->willReturn('some_data'); - $crypt->method('encrypt')->willReturn('some_data'); - - $definitions = $this->getModuleDefinitions(); - $definitions[CryptInterface::class] = $crypt; - - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath( - '//div[@id="box-popup" and @class="box-password-view"]//table//td[starts-with(@class,"dialog-text")]|//button' - )->extract(['_name']); - - assert(!empty($output)); - assert(count($filter) === 4); - - $this->assertTrue(true); - }); - - $container = $this->buildContainer( - $definitions, - $this->buildRequest( - 'get', - 'index.php', - [ - 'r' => sprintf( - "account/viewPassHistory/id/%d", - self::$faker->randomNumber(3) - ) - ] - ) - ); - - $this->runApp($container); - - $this->expectOutputRegex( - '/\{"status":0,"description":null,"data":\{"useimage":false,"html":".*"\},"messages":\[\]\}/' - ); - } - - protected function getContext(): SessionContext|Stub - { - $vault = self::createStub(VaultInterface::class); - - $context = parent::getContext(); - $context->method('getVault')->willReturn($vault); - - return $context; - } - - protected function getUserProfile(): ProfileData - { - return new ProfileData(['accViewPass' => true,]); - } -} diff --git a/tests/SP/Modules/Web/Controllers/AccountFile/AccountFileTest.php b/tests/SP/Modules/Web/Controllers/AccountFile/AccountFileTest.php index c036af92..02a21be0 100644 --- a/tests/SP/Modules/Web/Controllers/AccountFile/AccountFileTest.php +++ b/tests/SP/Modules/Web/Controllers/AccountFile/AccountFileTest.php @@ -38,9 +38,9 @@ use SP\Domain\Core\Exceptions\InvalidClassException; use SP\Infrastructure\Database\QueryData; use SP\Infrastructure\Database\QueryResult; use SP\Infrastructure\File\FileException; -use SP\Mvc\View\OutputHandlerInterface; use SP\Tests\Generators\FileDataGenerator; use SP\Tests\IntegrationTestCase; +use SP\Tests\OutputChecker; use Symfony\Component\DomCrawler\Crawler; /** @@ -49,21 +49,18 @@ use Symfony\Component\DomCrawler\Crawler; #[Group('integration')] class AccountFileTest extends IntegrationTestCase { + private array $moduleDefinitions; /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws FileException - * @throws InvalidClassException * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface */ #[Test] public function deleteSingleFile() { - $definitions = $this->getModuleDefinitions(); - $container = $this->buildContainer( - $definitions, + $this->moduleDefinitions, $this->buildRequest('post', 'index.php', ['r' => 'accountFile/delete/100']) ); @@ -75,11 +72,9 @@ class AccountFileTest extends IntegrationTestCase } /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws FileException - * @throws InvalidClassException * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface */ #[Test] public function deleteMultipleFiles() @@ -93,10 +88,8 @@ class AccountFileTest extends IntegrationTestCase return new QueryResult(); }; - $definitions = $this->getModuleDefinitions(); - $container = $this->buildContainer( - $definitions, + $this->moduleDefinitions, $this->buildRequest('post', 'index.php', ['r' => 'accountFile/delete'], ['items' => [100, 200, 300]]) ); @@ -111,8 +104,6 @@ class AccountFileTest extends IntegrationTestCase * @return void * @throws ContainerExceptionInterface * @throws Exception - * @throws FileException - * @throws InvalidClassException * @throws NotFoundExceptionInterface */ #[Test] @@ -127,10 +118,8 @@ class AccountFileTest extends IntegrationTestCase ) ); - $definitions = $this->getModuleDefinitions(); - $container = $this->buildContainer( - $definitions, + $this->moduleDefinitions, $this->buildRequest('get', 'index.php', ['r' => 'accountFile/download/100']) ); @@ -143,11 +132,10 @@ class AccountFileTest extends IntegrationTestCase * @return void * @throws ContainerExceptionInterface * @throws Exception - * @throws FileException - * @throws InvalidClassException * @throws NotFoundExceptionInterface */ #[Test] + #[OutputChecker('outputCheckerList')] public function listFiles() { $fileDataGenerator = FileDataGenerator::factory(); @@ -162,21 +150,8 @@ class AccountFileTest extends IntegrationTestCase ) ); - $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath( - '//div[@id="files-wrap"]/ul//li' - )->extract(['class']); - - assert(!empty($output)); - assert(count($filter) === 2); - - $this->assertTrue(true); - }); - $container = $this->buildContainer( - $definitions, + $this->moduleDefinitions, $this->buildRequest('get', 'index.php', ['r' => 'accountFile/list/100']) ); @@ -187,11 +162,10 @@ class AccountFileTest extends IntegrationTestCase * @return void * @throws ContainerExceptionInterface * @throws Exception - * @throws FileException - * @throws InvalidClassException * @throws NotFoundExceptionInterface */ #[Test] + #[OutputChecker('outputCheckerSearch')] public function search() { $fileDataGenerator = FileDataGenerator::factory(); @@ -207,20 +181,8 @@ class AccountFileTest extends IntegrationTestCase ) ); - $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath('//table/tbody//tr[string-length(@data-item-id) > 0]') - ->extract(['class']); - - assert(!empty($output)); - assert(count($filter) === 2); - - $this->assertTrue(true); - }); - $container = $this->buildContainer( - $definitions, + $this->moduleDefinitions, $this->buildRequest('get', 'index.php', ['r' => 'accountFile/search']) ); @@ -231,25 +193,12 @@ class AccountFileTest extends IntegrationTestCase * @return void * @throws ContainerExceptionInterface * @throws Exception - * @throws FileException - * @throws InvalidClassException * @throws NotFoundExceptionInterface */ #[Test] + #[OutputChecker('outputCheckerUpload')] public function upload() { - $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath('//table/tbody//tr[string-length(@data-item-id) > 0]') - ->extract(['class']); - - assert(!empty($output)); - assert(count($filter) === 2); - - $this->assertTrue(true); - }); - $file = sprintf('%s.txt', self::$faker->filePath()); file_put_contents($file, self::$faker->text()); @@ -264,7 +213,7 @@ class AccountFileTest extends IntegrationTestCase ]; $container = $this->buildContainer( - $definitions, + $this->moduleDefinitions, $this->buildRequest('post', 'index.php', ['r' => 'accountFile/upload/100'], [], $files) ); @@ -275,15 +224,29 @@ class AccountFileTest extends IntegrationTestCase ); } + /** + * @param string $output + * @return void + */ + + private function outputCheckerUpload(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath('//table/tbody//tr[string-length(@data-item-id) > 0]') + ->extract(['class']); + + self::assertNotEmpty($output); + self::assertCount(2, $filter); + } + /** * @return void * @throws ContainerExceptionInterface * @throws Exception - * @throws FileException - * @throws InvalidClassException * @throws NotFoundExceptionInterface */ #[Test] + #[OutputChecker('outputCheckerView')] public function view() { $fileDataGenerator = FileDataGenerator::factory(); @@ -293,19 +256,8 @@ class AccountFileTest extends IntegrationTestCase new QueryResult([File::buildFromSimpleModel($fileDataGenerator->buildFileData())]) ); - $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath('//img|//div[@class="title"]')->count(); - - assert(!empty($output)); - assert($filter === 2); - - $this->assertTrue(true); - }); - $container = $this->buildContainer( - $definitions, + $this->moduleDefinitions, $this->buildRequest('get', 'index.php', ['r' => 'accountFile/view/100']) ); @@ -321,4 +273,57 @@ class AccountFileTest extends IntegrationTestCase return $configData; } + + /** + * @throws FileException + * @throws InvalidClassException + */ + protected function setUp(): void + { + parent::setUp(); + + $this->moduleDefinitions = $this->getModuleDefinitions(); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerSearch(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath('//table/tbody//tr[string-length(@data-item-id) > 0]') + ->extract(['class']); + + self::assertNotEmpty($output); + self::assertCount(2, $filter); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerView(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath('//img|//div[@class="title"]')->count(); + + self::assertNotEmpty($output); + self::assertEquals(2, $filter); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerList(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@id="files-wrap"]/ul//li' + )->extract(['class']); + + self::assertNotEmpty($output); + self::assertCount(2, $filter); + } } diff --git a/tests/SP/Modules/Web/Controllers/AccountHistoryManager/AccountHistoryManagerTest.php b/tests/SP/Modules/Web/Controllers/AccountHistoryManager/AccountHistoryManagerTest.php index e888b7b2..8635a754 100644 --- a/tests/SP/Modules/Web/Controllers/AccountHistoryManager/AccountHistoryManagerTest.php +++ b/tests/SP/Modules/Web/Controllers/AccountHistoryManager/AccountHistoryManagerTest.php @@ -39,9 +39,9 @@ use SP\Domain\Core\Exceptions\InvalidClassException; use SP\Infrastructure\Database\QueryData; use SP\Infrastructure\Database\QueryResult; use SP\Infrastructure\File\FileException; -use SP\Mvc\View\OutputHandlerInterface; use SP\Tests\Generators\AccountDataGenerator; use SP\Tests\IntegrationTestCase; +use SP\Tests\OutputChecker; use Symfony\Component\DomCrawler\Crawler; /** @@ -50,13 +50,12 @@ use Symfony\Component\DomCrawler\Crawler; #[Group('integration')] class AccountHistoryManagerTest extends IntegrationTestCase { + private array $definitions; /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws InvalidClassException - * @throws FileException * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface */ #[Test] public function deleteSingle() @@ -66,10 +65,8 @@ class AccountHistoryManagerTest extends IntegrationTestCase new QueryResult([AccountDataGenerator::factory()->buildAccountHistoryData()]) ); - $definitions = $this->getModuleDefinitions(); - $container = $this->buildContainer( - $definitions, + $this->definitions, $this->buildRequest('get', 'index.php', ['r' => 'accountHistoryManager/delete/100']) ); @@ -79,11 +76,9 @@ class AccountHistoryManagerTest extends IntegrationTestCase } /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws InvalidClassException - * @throws FileException * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface */ #[Test] public function deleteMultiple() @@ -93,10 +88,8 @@ class AccountHistoryManagerTest extends IntegrationTestCase new QueryResult([AccountDataGenerator::factory()->buildAccountHistoryData()]) ); - $definitions = $this->getModuleDefinitions(); - $container = $this->buildContainer( - $definitions, + $this->definitions, $this->buildRequest( 'post', 'index.php', @@ -111,11 +104,9 @@ class AccountHistoryManagerTest extends IntegrationTestCase } /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws InvalidClassException - * @throws FileException * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface */ #[Test] public function restoreModified() @@ -146,10 +137,8 @@ class AccountHistoryManagerTest extends IntegrationTestCase return new QueryResult([], 1, 100); }; - $definitions = $this->getModuleDefinitions(); - $container = $this->buildContainer( - $definitions, + $this->definitions, $this->buildRequest('get', 'index.php', ['r' => 'accountHistoryManager/restore/100']) ); @@ -159,11 +148,9 @@ class AccountHistoryManagerTest extends IntegrationTestCase } /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws InvalidClassException - * @throws FileException * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface */ #[Test] public function restoreDeleted() @@ -194,10 +181,8 @@ class AccountHistoryManagerTest extends IntegrationTestCase return new QueryResult([], 1, 100); }; - $definitions = $this->getModuleDefinitions(); - $container = $this->buildContainer( - $definitions, + $this->definitions, $this->buildRequest('get', 'index.php', ['r' => 'accountHistoryManager/restore/100']) ); @@ -207,13 +192,12 @@ class AccountHistoryManagerTest extends IntegrationTestCase } /** - * @throws NotFoundExceptionInterface - * @throws Exception - * @throws InvalidClassException - * @throws FileException * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface */ #[Test] + #[OutputChecker('outputCheckerSearch')] public function search() { $accountDataGenerator = AccountDataGenerator::factory(); @@ -229,25 +213,38 @@ class AccountHistoryManagerTest extends IntegrationTestCase ) ); - $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { - $crawler = new Crawler($output); - $filter = $crawler->filterXPath( - '//table/tbody[@id="data-rows-tblAccountsHistory"]//tr[string-length(@data-item-id) > 0]' - ) - ->extract(['data-item-id']); - - assert(!empty($output)); - assert(count($filter) === 2); - - $this->assertTrue(true); - }); - $container = $this->buildContainer( - $definitions, + $this->definitions, $this->buildRequest('get', 'index.php', ['r' => 'accountHistoryManager/search']) ); $this->runApp($container); } + + /** + * @throws FileException + * @throws InvalidClassException + */ + protected function setUp(): void + { + parent::setUp(); + + $this->definitions = $this->getModuleDefinitions(); + } + + /** + * @param string $output + * @return void + */ + private function outputCheckerSearch(string $output): void + { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//table/tbody[@id="data-rows-tblAccountsHistory"]//tr[string-length(@data-item-id) > 0]' + ) + ->extract(['data-item-id']); + + self::assertNotEmpty($output); + self::assertCount(2, $filter); + } } diff --git a/tests/SP/Modules/Web/Controllers/AccountManager/AccountManagerTest.php b/tests/SP/Modules/Web/Controllers/AccountManager/AccountManagerTest.php index f99ab711..1eb83963 100644 --- a/tests/SP/Modules/Web/Controllers/AccountManager/AccountManagerTest.php +++ b/tests/SP/Modules/Web/Controllers/AccountManager/AccountManagerTest.php @@ -39,13 +39,13 @@ use SP\Domain\User\Models\User; use SP\Domain\User\Models\UserGroup; use SP\Infrastructure\Database\QueryResult; use SP\Infrastructure\File\FileException; -use SP\OutputChecker; use SP\Tests\Generators\CategoryGenerator; use SP\Tests\Generators\ClientGenerator; use SP\Tests\Generators\TagGenerator; use SP\Tests\Generators\UserDataGenerator; use SP\Tests\Generators\UserGroupGenerator; use SP\Tests\IntegrationTestCase; +use SP\Tests\OutputChecker; use Symfony\Component\DomCrawler\Crawler; /** @@ -63,6 +63,7 @@ class AccountManagerTest extends IntegrationTestCase * @throws ContainerExceptionInterface */ #[Test] + #[OutputChecker('outputCheckerBulkEdit')] public function bulkEdit() { $this->addDatabaseMapperResolver( @@ -98,15 +99,14 @@ class AccountManagerTest extends IntegrationTestCase $this->runApp($container); } - #[OutputChecker] - private function outputChecker(string $output): void + private function outputCheckerBulkEdit(string $output): void { $crawler = new Crawler($output); $filter = $crawler->filterXPath( '//div[@id="box-popup"]//form[@name="frmAccountBulkEdit"]//select|//input|//div[@class="action-in-box"]/button' )->extract(['_name']); - $this->assertNotEmpty($output); - $this->assertCount(19, $filter); + self::assertNotEmpty($output); + self::assertCount(19, $filter); } } diff --git a/tests/SP/OutputChecker.php b/tests/SP/OutputChecker.php index 0b57c7b8..54936721 100644 --- a/tests/SP/OutputChecker.php +++ b/tests/SP/OutputChecker.php @@ -24,7 +24,7 @@ declare(strict_types=1); -namespace SP; +namespace SP\Tests; use Attribute; @@ -34,5 +34,7 @@ use Attribute; #[Attribute(Attribute::TARGET_METHOD)] final class OutputChecker { - + public function __construct(public readonly string $target) + { + } }