test(IT): Rework output handler call

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-08-04 17:48:03 +02:00
parent 3f2f708df9
commit 95c98ae2fa
7 changed files with 72 additions and 29 deletions

View File

@@ -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));
}
/**

View File

@@ -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(

View File

@@ -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(

View File

@@ -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);
}
);

View File

@@ -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);
}
);

View File

@@ -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);
}
);

View File

@@ -0,0 +1,53 @@
<?php
/**
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
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 '';
}
}