Translated using Weblate (Portuguese)
[phpmyadmin.git] / tests / unit / Stubs / ResponseRenderer.php
blob5bca9430f12960b1a6ada15aa87de5b9e2b5b06e
1 <?php
2 /**
3 * Fake response stub for testing purposes
5 * It will concatenate HTML and JSON for given calls to addHTML and addJSON
6 * respectively, what make it easy to determine whether the output is correct in test
7 * suite. Feel free to modify for any future test needs.
8 */
10 declare(strict_types=1);
12 namespace PhpMyAdmin\Tests\Stubs;
14 use PhpMyAdmin\Bookmarks\BookmarkRepository;
15 use PhpMyAdmin\Config;
16 use PhpMyAdmin\ConfigStorage\Relation;
17 use PhpMyAdmin\Console;
18 use PhpMyAdmin\Current;
19 use PhpMyAdmin\Dbal\DatabaseInterface;
20 use PhpMyAdmin\Error\ErrorHandler;
21 use PhpMyAdmin\Footer;
22 use PhpMyAdmin\Header;
23 use PhpMyAdmin\Http\Factory\ResponseFactory;
24 use PhpMyAdmin\Http\Response;
25 use PhpMyAdmin\Message;
26 use PhpMyAdmin\Template;
28 use function is_array;
29 use function json_encode;
31 class ResponseRenderer extends \PhpMyAdmin\ResponseRenderer
33 /**
34 * HTML data to be used in the response
36 protected string $htmlString = '';
38 /**
39 * An array of JSON key-value pairs
40 * to be sent back for ajax requests
42 * @var mixed[]
44 protected array $json = [];
46 /**
47 * Creates a new class instance
49 public function __construct()
51 Current::$lang = 'en';
52 $config = Config::getInstance();
53 $config->selectedServer['pmadb'] = 'phpmyadmin';
54 $template = new Template($config);
55 $dummyDbi = new DbiDummy();
56 $dummyDbi->addSelectDb('phpmyadmin');
57 $dbi = DatabaseInterface::getInstanceForTest($dummyDbi, $config);
58 $relation = new Relation($dbi);
59 $console = new Console($relation, $template, new BookmarkRepository($dbi, $relation));
61 parent::__construct(
62 $config,
63 $template,
64 new Header($template, $console, $config),
65 new Footer($template, $config),
66 ErrorHandler::getInstance(),
67 $dbi,
68 ResponseFactory::create(),
72 /**
73 * Append HTML code to the response stub
75 public function addHTML(string $content): void
77 $this->htmlString .= $content;
80 /**
81 * Add JSON code to the response stub
83 * @param array-key|array<array-key, mixed> $json Either a key (string) or an array or key-value pairs
84 * @param mixed|null $value Null, if passing an array in $json otherwise
85 * it's a string value to the key
87 public function addJSON(string|int|array $json, mixed $value = null): void
89 if (is_array($json)) {
90 foreach ($json as $key => $value) {
91 $this->addJSON($key, $value);
93 } elseif ($value instanceof Message) {
94 $this->json[$json] = $value->getDisplay();
95 } else {
96 $this->json[$json] = $value;
101 * Return the final concatenated HTML string
103 public function getHTMLResult(): string
105 return $this->htmlString;
109 * Return the final JSON array
111 * @return mixed[]
113 public function getJSONResult(): array
115 return $this->json;
119 * Current I choose to return PhpMyAdmin\Header object directly because
120 * our test has nothing about the Scripts and PhpMyAdmin\Header class.
122 public function getHeader(): Header
124 return $this->header;
128 * Set the status of an ajax response,
129 * whether it is a success or an error
131 * @param bool $state Whether the request was successfully processed
133 public function setRequestStatus(bool $state): void
135 $this->isSuccess = $state;
139 * Get the status of an ajax response.
141 public function hasSuccessState(): bool
143 return $this->isSuccess;
147 * This function is used to clear all data to this
148 * stub after any operations.
150 public function clear(): void
152 $this->isSuccess = true;
153 $this->json = [];
154 $this->htmlString = '';
158 * Set the ajax flag to indicate whether
159 * we are servicing an ajax request
161 * @param bool $isAjax Whether we are servicing an ajax request
163 public function setAjax(bool $isAjax): void
165 $this->isAjax = $isAjax;
169 * Returns true or false depending on whether
170 * we are servicing an ajax request
172 public function isAjax(): bool
174 return $this->isAjax;
177 public function getResponse(): Response
179 return $this->response;
182 public function response(): Response
184 if ($this->isAjax()) {
185 $json = $this->getJSONResult();
186 if ($this->isSuccess) {
187 $json['success'] = true;
188 } else {
189 $json['success'] = false;
190 $json['error'] = $json['message'];
191 unset($json['message']);
194 $output = (string) json_encode($json);
195 } else {
196 $output = $this->getHTMLResult();
199 $this->response->getBody()->write($output);
201 return $this->response;