From 95c98ae2fa3bbada9a88adaf6da0e25b5a76055d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sun, 4 Aug 2024 17:48:03 +0200 Subject: [PATCH] test(IT): Rework output handler call 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 | 23 ++------ .../AccessManager/IndexControllerTest.php | 5 +- .../Account/CopyControllerTest.php | 5 +- .../Account/CreateControllerTest.php | 5 +- .../Account/DeleteControllerTest.php | 5 +- .../Account/EditControllerTest.php | 5 +- tests/SP/Stubs/OutputHandlerStub.php | 53 +++++++++++++++++++ 7 files changed, 72 insertions(+), 29 deletions(-) create mode 100644 tests/SP/Stubs/OutputHandlerStub.php diff --git a/tests/SP/IntegrationTestCase.php b/tests/SP/IntegrationTestCase.php index 05fd8053..019b47ec 100644 --- a/tests/SP/IntegrationTestCase.php +++ b/tests/SP/IntegrationTestCase.php @@ -31,7 +31,6 @@ use Faker\Factory; use Faker\Generator; use Klein\Request; use PHPUnit\Framework\MockObject\Exception; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\Stub; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerExceptionInterface; @@ -69,6 +68,7 @@ use SP\Modules\Web\Bootstrap; use SP\Mvc\View\OutputHandlerInterface; use SP\Tests\Generators\UserDataGenerator; use SP\Tests\Generators\UserProfileDataGenerator; +use SP\Tests\Stubs\OutputHandlerStub; use function DI\autowire; use function DI\factory; @@ -235,26 +235,11 @@ abstract class IntegrationTestCase extends TestCase /** * @param callable $outputChecker - * @return MockObject|OutputHandlerInterface - * @throws Exception + * @return OutputHandlerInterface */ - protected function setupOutputHandler(callable $outputChecker): OutputHandlerInterface|MockObject + protected function setupOutputHandler(callable $outputChecker): OutputHandlerInterface { - $outputHandler = $this->createMock(OutputHandlerInterface::class); - $outputHandler->expects($this->once()) - ->method('bufferedContent') - ->with( - self::callback( - static function (callable $callback) use ($outputChecker) { - ob_start(); - $callback(); - - return $outputChecker(ob_get_clean()); - } - ) - ); - - return $outputHandler; + return new OutputHandlerStub($outputChecker(...)->bindTo($this)); } /** diff --git a/tests/SP/Modules/Web/Controllers/AccessManager/IndexControllerTest.php b/tests/SP/Modules/Web/Controllers/AccessManager/IndexControllerTest.php index bf4ab8b1..7028bd57 100644 --- a/tests/SP/Modules/Web/Controllers/AccessManager/IndexControllerTest.php +++ b/tests/SP/Modules/Web/Controllers/AccessManager/IndexControllerTest.php @@ -56,13 +56,14 @@ class IndexControllerTest extends IntegrationTestCase { $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(static function (string $output) { + $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { $crawler = new Crawler($output); $filter = $crawler->filterXPath( '//div[contains(@id, \'tabs-\')]//form' )->extract(['id']); - return !empty($output) && count($filter) === 5; + $this->assertNotEmpty($output); + $this->assertCount(5, $filter); }); $container = $this->buildContainer( diff --git a/tests/SP/Modules/Web/Controllers/Account/CopyControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/CopyControllerTest.php index fe8f4b96..bac496a4 100644 --- a/tests/SP/Modules/Web/Controllers/Account/CopyControllerTest.php +++ b/tests/SP/Modules/Web/Controllers/Account/CopyControllerTest.php @@ -56,13 +56,14 @@ class CopyControllerTest extends IntegrationTestCase public function testCopyAction() { $definitions = $this->getModuleDefinitions(); - $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(static function (string $output) { + $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']); - return !empty($output) && count($filter) === 3; + $this->assertNotEmpty($output); + $this->assertCount(3, $filter); }); $container = $this->buildContainer( diff --git a/tests/SP/Modules/Web/Controllers/Account/CreateControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/CreateControllerTest.php index 253c466b..d8868756 100644 --- a/tests/SP/Modules/Web/Controllers/Account/CreateControllerTest.php +++ b/tests/SP/Modules/Web/Controllers/Account/CreateControllerTest.php @@ -55,13 +55,14 @@ class CreateControllerTest extends IntegrationTestCase { $definitions = $this->getModuleDefinitions(); $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler( - static function (string $output) { + 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']); - return !empty($output) && count($filter) === 3; + $this->assertNotEmpty($output); + $this->assertCount(3, $filter); } ); diff --git a/tests/SP/Modules/Web/Controllers/Account/DeleteControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/DeleteControllerTest.php index 0e30df82..7f8da930 100644 --- a/tests/SP/Modules/Web/Controllers/Account/DeleteControllerTest.php +++ b/tests/SP/Modules/Web/Controllers/Account/DeleteControllerTest.php @@ -59,13 +59,14 @@ class DeleteControllerTest extends IntegrationTestCase { $definitions = $this->getModuleDefinitions(); $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler( - static function (string $output) { + 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']); - return !empty($output) && count($filter) === 2; + $this->assertNotEmpty($output); + $this->assertCount(2, $filter); } ); diff --git a/tests/SP/Modules/Web/Controllers/Account/EditControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/EditControllerTest.php index 750f13fe..c71fb9a1 100644 --- a/tests/SP/Modules/Web/Controllers/Account/EditControllerTest.php +++ b/tests/SP/Modules/Web/Controllers/Account/EditControllerTest.php @@ -58,13 +58,14 @@ class EditControllerTest extends IntegrationTestCase { $definitions = $this->getModuleDefinitions(); $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler( - static function (string $output) { + 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']); - return !empty($output) && count($filter) === 3; + $this->assertNotEmpty($output); + $this->assertCount(3, $filter); } ); diff --git a/tests/SP/Stubs/OutputHandlerStub.php b/tests/SP/Stubs/OutputHandlerStub.php new file mode 100644 index 00000000..d0c2b796 --- /dev/null +++ b/tests/SP/Stubs/OutputHandlerStub.php @@ -0,0 +1,53 @@ +. + */ + +declare(strict_types=1); + +namespace SP\Tests\Stubs; + +use Closure; +use SP\Mvc\View\OutputHandlerInterface; + +/** + * Class OutputHandlerStub + */ +final readonly class OutputHandlerStub implements OutputHandlerInterface +{ + public function __construct(private Closure $outputChecker) + { + } + + /** + * @inheritDoc + */ + public function bufferedContent(callable $callback): string + { + ob_start(); + $callback(); + + ($this->outputChecker)(ob_get_clean()); + + return ''; + } +}