PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/unit/Flash/Direct/Helper/FlashBase.php

http://github.com/phalcon/cphalcon
PHP | 276 lines | 142 code | 39 blank | 95 comment | 6 complexity | e67c57eb4c5291d4fdd781b1845dec82 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of the Phalcon Framework.
  4. *
  5. * (c) Phalcon Team <team@phalcon.io>
  6. *
  7. * For the full copyright and license information, please view the LICENSE.txt
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Phalcon\Test\Unit\Flash\Direct\Helper;
  12. use Phalcon\Escaper;
  13. use Phalcon\Flash\AbstractFlash;
  14. use Phalcon\Flash\Direct;
  15. use UnitTester;
  16. class FlashBase
  17. {
  18. private $classes = [];
  19. private $notHtml = false;
  20. private $notImplicit = false;
  21. private $default = [
  22. 'success' => 'successMessage',
  23. 'notice' => 'noticeMessage',
  24. 'warning' => 'warningMessage',
  25. 'error' => 'errorMessage',
  26. ];
  27. /**
  28. * Executed before each test
  29. *
  30. * @param UnitTester $I
  31. * @return void
  32. */
  33. public function _before(UnitTester $I): void
  34. {
  35. }
  36. /**
  37. * Tests warning (implicit flush)
  38. *
  39. * @param UnitTester $I
  40. *
  41. * @author Phalcon Team <team@phalcon.io>
  42. * @since 2014-10-04
  43. */
  44. public function testFlashDirectImplicitFlushHtml(UnitTester $I)
  45. {
  46. $functions = [
  47. 'error',
  48. 'success',
  49. 'notice',
  50. 'warning',
  51. ];
  52. foreach ($functions as $function) {
  53. $this->stringTest($I, $function);
  54. }
  55. }
  56. /**
  57. * Tests a string with implicit flush Html
  58. *
  59. * @param UnitTester $I
  60. * @param string $function
  61. *
  62. * @author Phalcon Team <team@phalcon.io>
  63. * @since 2014-10-04
  64. */
  65. private function stringTest(UnitTester $I, string $function)
  66. {
  67. $flash = new Direct(new Escaper());
  68. $flash->setCssClasses($this->classes);
  69. $message = 'sample message';
  70. if ($this->notHtml) {
  71. $flash->setAutomaticHtml(false);
  72. $expected = $message;
  73. } else {
  74. $expected = sprintf(
  75. '<div%s>%s</div>' . PHP_EOL,
  76. $this->getClass($function),
  77. $message
  78. );
  79. }
  80. if ($this->notImplicit) {
  81. $flash->setImplicitFlush(false);
  82. $actual = $flash->$function($message);
  83. } else {
  84. $actual = $this->getObResponse($flash, $function, $message);
  85. }
  86. $I->assertEquals($expected, $actual);
  87. }
  88. /**
  89. * Private function to get the class of the message depending on the classes
  90. * set
  91. *
  92. * @param string $key
  93. * @return string
  94. *
  95. * @author Phalcon Team <team@phalcon.io>
  96. * @since 2014-10-04
  97. */
  98. private function getClass(string $key): string
  99. {
  100. $template = ' class="%s"';
  101. if ([] === $this->classes) {
  102. $class = '';
  103. } else {
  104. $classes = (is_null($this->classes)) ?
  105. $this->default :
  106. $this->classes;
  107. $class = sprintf($template, $classes[$key]);
  108. }
  109. return $class;
  110. }
  111. /**
  112. * Private function to start the ob, call the function, get the
  113. * contents and clean the ob
  114. *
  115. * @param AbstractFlash $flash
  116. * @param string $function
  117. * @param string $message
  118. * @return string
  119. *
  120. * @author Phalcon Team <team@phalcon.io>
  121. * @since 2014-10-04
  122. */
  123. private function getObResponse(AbstractFlash $flash, string $function, string $message): string
  124. {
  125. ob_start();
  126. $flash->$function($message);
  127. $actual = ob_get_contents();
  128. ob_end_clean();
  129. return $actual;
  130. }
  131. /**
  132. * Tests warning (no implicit flush)
  133. *
  134. * @param UnitTester $I
  135. *
  136. * @author Phalcon Team <team@phalcon.io>
  137. * @since 2014-10-04
  138. */
  139. public function testFlashDirectNoImplicitFlushHtml(UnitTester $I)
  140. {
  141. $functions = [
  142. 'error',
  143. 'success',
  144. 'notice',
  145. 'warning',
  146. ];
  147. foreach ($functions as $function) {
  148. $this->notImplicit = true;
  149. $this->stringTest($I, $function);
  150. $this->notImplicit = false;
  151. }
  152. }
  153. /**
  154. * Tests warning (implicit flush no html)
  155. *
  156. * @param UnitTester $I
  157. *
  158. * @author Phalcon Team <team@phalcon.io>
  159. * @since 2014-10-04
  160. */
  161. public function testFlashDirectImplicitFlushNoHtml(UnitTester $I)
  162. {
  163. $functions = [
  164. 'error',
  165. 'success',
  166. 'notice',
  167. 'warning',
  168. ];
  169. foreach ($functions as $function) {
  170. $this->notHtml = true;
  171. $this->stringTest($I, $function);
  172. $this->notHtml = false;
  173. }
  174. }
  175. /**
  176. * Tests error (no implicit flush no html)
  177. *
  178. * @param UnitTester $I
  179. *
  180. * @author Phalcon Team <team@phalcon.io>
  181. * @since 2014-10-04
  182. */
  183. public function testFlashDirectNoImplicitFlushNoHtml(UnitTester $I)
  184. {
  185. $functions = [
  186. 'error',
  187. 'success',
  188. 'notice',
  189. 'warning',
  190. ];
  191. foreach ($functions as $function) {
  192. $this->notHtml = true;
  193. $this->notImplicit = true;
  194. $this->stringTest($I, $function);
  195. $this->notHtml = false;
  196. $this->notImplicit = false;
  197. }
  198. }
  199. /**
  200. * Tests auto escaping
  201. *
  202. * @param UnitTester $I
  203. *
  204. * @author Phalcon Team <team@phalcon.io>
  205. * @issue https://github.com/phalcon/cphalcon/issues/11448
  206. * @since 2016-06-15
  207. */
  208. public function testFlashDirectWithAutoEscaping(UnitTester $I)
  209. {
  210. $flash = new Direct(new Escaper());
  211. $flash->setCssClasses($this->classes);
  212. $flash->setAutomaticHtml(false);
  213. $flash->setImplicitFlush(false);
  214. $I->assertEquals(
  215. '&lt;h1&gt;Hello World!&lt;/h1&gt;',
  216. $flash->success('<h1>Hello World!</h1>')
  217. );
  218. $flash->setAutoescape(false);
  219. $I->assertEquals(
  220. '<h1>Hello World!</h1>',
  221. $flash->success('<h1>Hello World!</h1>')
  222. );
  223. }
  224. /**
  225. * Sets the custom classes for the tests
  226. *
  227. * @param array $classes
  228. * @return void
  229. *
  230. * @author Phalcon Team <team@phalcon.io>
  231. * @since 2014-10-04
  232. */
  233. protected function setClasses(array $classes): void
  234. {
  235. $this->classes = $classes;
  236. }
  237. }