Improve unit test for ResponseRenderer::setAjax()

Also tests the setAjax() method of Header, Footer and Console classes.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
Maurício Meneghini Fauth 2023-05-31 00:26:48 -03:00
parent b9084615f6
commit 3f587ccce4
No known key found for this signature in database
GPG Key ID: 6A16FD38AFC89CC8
9 changed files with 114 additions and 49 deletions

View File

@ -31,7 +31,7 @@ class Console
*
* @var bool
*/
private $isAjax;
private $isAjax = false;
/** @var Relation */
private $relation;

View File

@ -36,7 +36,7 @@ class Footer
*
* @var bool
*/
private $isAjax;
private $isAjax = false;
/**
* Whether to only close the BODY and HTML tags
* or also include scripts, errors and links

View File

@ -76,7 +76,7 @@ class Header
*
* @var bool
*/
private $isAjax;
private $isAjax = false;
/**
* Whether to display anything
*
@ -107,7 +107,6 @@ class Header
$this->template = new Template();
$this->isEnabled = true;
$this->isAjax = false;
$this->bodyId = '';
$this->title = '';
$this->console = new Console();

View File

@ -995,11 +995,6 @@
<code>$tabs</code>
</PossiblyNullIterator>
</file>
<file src="libraries/classes/Console.php">
<PropertyNotSetInConstructor occurrences="1">
<code>$isAjax</code>
</PropertyNotSetInConstructor>
</file>
<file src="libraries/classes/Controllers/AbstractController.php">
<MixedArgument occurrences="1">
<code>$db</code>
@ -6820,9 +6815,6 @@
<code>array{revision: string, revisionUrl: string, branch: string, branchUrl: string}|[]</code>
<code>is_array($info) ? $info : []</code>
</MixedReturnTypeCoercion>
<PropertyNotSetInConstructor occurrences="1">
<code>$isAjax</code>
</PropertyNotSetInConstructor>
<RedundantCast occurrences="3">
<code>(string) $GLOBALS['db']</code>
<code>(string) $GLOBALS['table']</code>

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace PhpMyAdmin\Tests;
use PhpMyAdmin\Console;
use ReflectionProperty;
/**
* @covers \PhpMyAdmin\Console
@ -16,4 +17,17 @@ class ConsoleTest extends AbstractTestCase
$console = new Console();
$this->assertEquals(['console.js'], $console->getScripts());
}
public function testSetAjax(): void
{
$isAjax = new ReflectionProperty(Console::class, 'isAjax');
$isAjax->setAccessible(true);
$console = new Console();
$this->assertFalse($isAjax->getValue($console));
$console->setAjax(true);
$this->assertTrue($isAjax->getValue($console));
$console->setAjax(false);
$this->assertFalse($isAjax->getValue($console));
}
}

View File

@ -7,6 +7,7 @@ namespace PhpMyAdmin\Tests;
use ArrayIterator;
use PhpMyAdmin\ErrorHandler;
use PhpMyAdmin\Footer;
use ReflectionProperty;
use function json_encode;
@ -116,10 +117,7 @@ class FooterTest extends AbstractTestCase
);
}
/**
* Test for footer when ajax enabled
*/
public function testAjax(): void
public function testGetDisplayWhenAjaxIsEnabled(): void
{
$footer = new Footer();
$footer->setAjax(true);
@ -167,4 +165,17 @@ class FooterTest extends AbstractTestCase
$footer->getDisplay()
);
}
public function testSetAjax(): void
{
$isAjax = new ReflectionProperty(Footer::class, 'isAjax');
$isAjax->setAccessible(true);
$footer = new Footer();
$this->assertFalse($isAjax->getValue($footer));
$footer->setAjax(true);
$this->assertTrue($isAjax->getValue($footer));
$footer->setAjax(false);
$this->assertFalse($isAjax->getValue($footer));
}
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace PhpMyAdmin\Tests;
use PhpMyAdmin\Console;
use PhpMyAdmin\Core;
use PhpMyAdmin\Header;
use ReflectionProperty;
@ -245,4 +246,26 @@ class HeaderTest extends AbstractTestCase
],
];
}
public function testSetAjax(): void
{
$header = new Header();
$consoleReflection = new ReflectionProperty(Header::class, 'console');
$consoleReflection->setAccessible(true);
$console = $consoleReflection->getValue($header);
$this->assertInstanceOf(Console::class, $console);
$isAjax = new ReflectionProperty(Header::class, 'isAjax');
$isAjax->setAccessible(true);
$consoleIsAjax = new ReflectionProperty(Console::class, 'isAjax');
$consoleIsAjax->setAccessible(true);
$this->assertFalse($isAjax->getValue($header));
$this->assertFalse($consoleIsAjax->getValue($console));
$header->setAjax(true);
$this->assertTrue($isAjax->getValue($header));
$this->assertTrue($consoleIsAjax->getValue($console));
$header->setAjax(false);
$this->assertFalse($isAjax->getValue($header));
$this->assertFalse($consoleIsAjax->getValue($console));
}
}

View File

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests;
use PhpMyAdmin\Footer;
use PhpMyAdmin\Header;
use PhpMyAdmin\ResponseRenderer;
use ReflectionProperty;
/**
* @covers \PhpMyAdmin\ResponseRenderer
*/
class ResponseRendererTest extends AbstractTestCase
{
protected function setUp(): void
{
parent::setUp();
$GLOBALS['lang'] = 'en';
$GLOBALS['server'] = 1;
$GLOBALS['text_dir'] = 'ltr';
$GLOBALS['PMA_PHP_SELF'] = 'index.php';
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSetAjax(): void
{
$_REQUEST = [];
$response = ResponseRenderer::getInstance();
$header = $response->getHeader();
$footerReflection = new ReflectionProperty(ResponseRenderer::class, 'footer');
$footerReflection->setAccessible(true);
$footer = $footerReflection->getValue($response);
$this->assertInstanceOf(Footer::class, $footer);
$headerIsAjax = new ReflectionProperty(Header::class, 'isAjax');
$headerIsAjax->setAccessible(true);
$footerIsAjax = new ReflectionProperty(Footer::class, 'isAjax');
$footerIsAjax->setAccessible(true);
$this->assertFalse($response->isAjax());
$this->assertFalse($headerIsAjax->getValue($header));
$this->assertFalse($footerIsAjax->getValue($footer));
$response->setAjax(true);
$this->assertTrue($response->isAjax());
$this->assertTrue($headerIsAjax->getValue($header));
$this->assertTrue($footerIsAjax->getValue($footer));
$response->setAjax(false);
$this->assertFalse($response->isAjax());
$this->assertFalse($headerIsAjax->getValue($header));
$this->assertFalse($footerIsAjax->getValue($footer));
}
}

View File

@ -1,33 +0,0 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests;
use PhpMyAdmin\ResponseRenderer;
/**
* @covers \PhpMyAdmin\ResponseRenderer
*/
class ResponseTest extends AbstractTestCase
{
protected function setUp(): void
{
parent::setUp();
$GLOBALS['lang'] = 'en';
$GLOBALS['server'] = 1;
$GLOBALS['text_dir'] = 'ltr';
$GLOBALS['PMA_PHP_SELF'] = 'index.php';
}
public function testSetAjax(): void
{
$_REQUEST = [];
$response = ResponseRenderer::getInstance();
$response->setAjax(true);
$this->assertTrue($response->isAjax());
$response->setAjax(false);
$this->assertFalse($response->isAjax());
}
}