Skip to content

Commit

Permalink
Add ExportTemplateControllerTest test
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Jul 20, 2020
1 parent ea17130 commit baf4d09
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
134 changes: 134 additions & 0 deletions test/classes/Controllers/ExportTemplateControllerTest.php
@@ -0,0 +1,134 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Tests\Controllers;

use PhpMyAdmin\Config;
use PhpMyAdmin\Controllers\ExportTemplateController;
use PhpMyAdmin\Export\Template as ExportTemplate;
use PhpMyAdmin\Export\TemplateModel;
use PhpMyAdmin\Relation;
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Tests\Stubs\Response;

class ExportTemplateControllerTest extends AbstractTestCase
{
/** @var ExportTemplateController */
private $controller;

/** @var Response */
private $response;

/** @var Template */
private $template;

protected function setUp(): void
{
global $dbi, $PMA_Config;

$this->setGlobalDbi();

$PMA_Config = new Config();
$PMA_Config->enableBc();

$_SESSION = [' PMA_token ' => 'token'];
$GLOBALS['server'] = 1;
$GLOBALS['text_dir'] = 'ltr';
$GLOBALS['PMA_PHP_SELF'] = '';

$_SESSION['relation'][$GLOBALS['server']] = [
'PMA_VERSION' => PMA_VERSION,
'exporttemplateswork' => true,
'db' => 'db',
'export_templates' => 'table',
];

$this->response = new Response();
$this->template = new Template();

$this->controller = new ExportTemplateController(
$this->response,
$dbi,
$this->template,
new TemplateModel($dbi),
new Relation($dbi, $this->template)
);
}

public function testCreate(): void
{
global $cfg;

$cfg['Server']['user'] = 'user';
$_POST['exportType'] = 'type';
$_POST['templateName'] = 'name';
$_POST['templateData'] = 'data';

$this->controller->create();

$templates = [
ExportTemplate::fromArray([
'id' => 1,
'username' => 'user1',
'exportType' => 'type1',
'name' => 'name1',
'data' => 'data1',
]),
ExportTemplate::fromArray([
'id' => 2,
'username' => 'user2',
'exportType' => 'type2',
'name' => 'name2',
'data' => 'data2',
]),
];

$options = $this->template->render('display/export/template_options', [
'templates' => $templates,
'selected_template' => null,
]);

$this->assertTrue($this->response->hasSuccessState());
$this->assertEquals(['data' => $options], $this->response->getJSONResult());
}

public function testDelete(): void
{
global $cfg;

$cfg['Server']['user'] = 'user';
$_POST['templateId'] = '1';

$this->controller->delete();

$this->assertTrue($this->response->hasSuccessState());
}

public function testLoad(): void
{
global $cfg;

$cfg['Server']['user'] = 'user';
$_POST['templateId'] = '1';

$this->controller->load();

$this->assertTrue($this->response->hasSuccessState());
$this->assertEquals(['data' => 'data1'], $this->response->getJSONResult());
}

public function testUpdate(): void
{
global $cfg;

$cfg['Server']['user'] = 'user';
$_POST['templateId'] = '1';
$_POST['templateData'] = 'data';

$this->controller->update();

$this->assertTrue($this->response->hasSuccessState());
}
}
30 changes: 30 additions & 0 deletions test/classes/Stubs/DbiDummy.php
Expand Up @@ -1938,6 +1938,36 @@ private function init(): void
['slow_query_log', 'OFF'],
],
],
[
'query' => 'INSERT INTO `db`.`table` (`username`, `export_type`, `template_name`, `template_data`)'
. ' VALUES (\'user\', \'type\', \'name\', \'data\');',
'result' => [],
],
[
'query' => 'SELECT * FROM `db`.`table` WHERE `username` = \'user\''
. ' AND `export_type` = \'type\' ORDER BY `template_name`;',
'columns' => ['id', 'username', 'export_type', 'template_name', 'template_data'],
'result' => [
['1', 'user1', 'type1', 'name1', 'data1'],
['2', 'user2', 'type2', 'name2', 'data2'],
],
],
[
'query' => 'DELETE FROM `db`.`table` WHERE `id` = 1 AND `username` = \'user\';',
'result' => [],
],
[
'query' => 'SELECT * FROM `db`.`table` WHERE `id` = 1 AND `username` = \'user\';',
'columns' => ['id', 'username', 'export_type', 'template_name', 'template_data'],
'result' => [
['1', 'user1', 'type1', 'name1', 'data1'],
],
],
[
'query' => 'UPDATE `db`.`table` SET `template_data` = \'data\''
. ' WHERE `id` = 1 AND `username` = \'user\';',
'result' => [],
],
];
/**
* Current database.
Expand Down

0 comments on commit baf4d09

Please sign in to comment.