AlertTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace tests\unit\widgets;
  3. use app\widgets\Alert;
  4. use Yii;
  5. class AlertTest extends \Codeception\Test\Unit
  6. {
  7. public function testSingleErrorMessage()
  8. {
  9. $message = 'This is an error message';
  10. Yii::$app->session->setFlash('error', $message);
  11. $renderingResult = Alert::widget();
  12. expect($renderingResult)->stringContainsString($message);
  13. expect($renderingResult)->stringContainsString('alert-danger');
  14. expect($renderingResult)->stringNotContainsString('alert-success');
  15. expect($renderingResult)->stringNotContainsString('alert-info');
  16. expect($renderingResult)->stringNotContainsString('alert-warning');
  17. }
  18. public function testMultipleErrorMessages()
  19. {
  20. $firstMessage = 'This is the first error message';
  21. $secondMessage = 'This is the second error message';
  22. Yii::$app->session->setFlash('error', [$firstMessage, $secondMessage]);
  23. $renderingResult = Alert::widget();
  24. expect($renderingResult)->stringContainsString($firstMessage);
  25. expect($renderingResult)->stringContainsString($secondMessage);
  26. expect($renderingResult)->stringContainsString('alert-danger');
  27. expect($renderingResult)->stringNotContainsString('alert-success');
  28. expect($renderingResult)->stringNotContainsString('alert-info');
  29. expect($renderingResult)->stringNotContainsString('alert-warning');
  30. }
  31. public function testSingleDangerMessage()
  32. {
  33. $message = 'This is a danger message';
  34. Yii::$app->session->setFlash('danger', $message);
  35. $renderingResult = Alert::widget();
  36. expect($renderingResult)->stringContainsString($message);
  37. expect($renderingResult)->stringContainsString('alert-danger');
  38. expect($renderingResult)->stringNotContainsString('alert-success');
  39. expect($renderingResult)->stringNotContainsString('alert-info');
  40. expect($renderingResult)->stringNotContainsString('alert-warning');
  41. }
  42. public function testMultipleDangerMessages()
  43. {
  44. $firstMessage = 'This is the first danger message';
  45. $secondMessage = 'This is the second danger message';
  46. Yii::$app->session->setFlash('danger', [$firstMessage, $secondMessage]);
  47. $renderingResult = Alert::widget();
  48. expect($renderingResult)->stringContainsString($firstMessage);
  49. expect($renderingResult)->stringContainsString($secondMessage);
  50. expect($renderingResult)->stringContainsString('alert-danger');
  51. expect($renderingResult)->stringNotContainsString('alert-success');
  52. expect($renderingResult)->stringNotContainsString('alert-info');
  53. expect($renderingResult)->stringNotContainsString('alert-warning');
  54. }
  55. public function testSingleSuccessMessage()
  56. {
  57. $message = 'This is a success message';
  58. Yii::$app->session->setFlash('success', $message);
  59. $renderingResult = Alert::widget();
  60. expect($renderingResult)->stringContainsString($message);
  61. expect($renderingResult)->stringContainsString('alert-success');
  62. expect($renderingResult)->stringNotContainsString('alert-danger');
  63. expect($renderingResult)->stringNotContainsString('alert-info');
  64. expect($renderingResult)->stringNotContainsString('alert-warning');
  65. }
  66. public function testMultipleSuccessMessages()
  67. {
  68. $firstMessage = 'This is the first danger message';
  69. $secondMessage = 'This is the second danger message';
  70. Yii::$app->session->setFlash('success', [$firstMessage, $secondMessage]);
  71. $renderingResult = Alert::widget();
  72. expect($renderingResult)->stringContainsString($firstMessage);
  73. expect($renderingResult)->stringContainsString($secondMessage);
  74. expect($renderingResult)->stringContainsString('alert-success');
  75. expect($renderingResult)->stringNotContainsString('alert-danger');
  76. expect($renderingResult)->stringNotContainsString('alert-info');
  77. expect($renderingResult)->stringNotContainsString('alert-warning');
  78. }
  79. public function testSingleInfoMessage()
  80. {
  81. $message = 'This is an info message';
  82. Yii::$app->session->setFlash('info', $message);
  83. $renderingResult = Alert::widget();
  84. expect($renderingResult)->stringContainsString($message);
  85. expect($renderingResult)->stringContainsString('alert-info');
  86. expect($renderingResult)->stringNotContainsString('alert-danger');
  87. expect($renderingResult)->stringNotContainsString('alert-success');
  88. expect($renderingResult)->stringNotContainsString('alert-warning');
  89. }
  90. public function testMultipleInfoMessages()
  91. {
  92. $firstMessage = 'This is the first info message';
  93. $secondMessage = 'This is the second info message';
  94. Yii::$app->session->setFlash('info', [$firstMessage, $secondMessage]);
  95. $renderingResult = Alert::widget();
  96. expect($renderingResult)->stringContainsString($firstMessage);
  97. expect($renderingResult)->stringContainsString($secondMessage);
  98. expect($renderingResult)->stringContainsString('alert-info');
  99. expect($renderingResult)->stringNotContainsString('alert-danger');
  100. expect($renderingResult)->stringNotContainsString('alert-success');
  101. expect($renderingResult)->stringNotContainsString('alert-warning');
  102. }
  103. public function testSingleWarningMessage()
  104. {
  105. $message = 'This is a warning message';
  106. Yii::$app->session->setFlash('warning', $message);
  107. $renderingResult = Alert::widget();
  108. expect($renderingResult)->stringContainsString($message);
  109. expect($renderingResult)->stringContainsString('alert-warning');
  110. expect($renderingResult)->stringNotContainsString('alert-danger');
  111. expect($renderingResult)->stringNotContainsString('alert-success');
  112. expect($renderingResult)->stringNotContainsString('alert-info');
  113. }
  114. public function testMultipleWarningMessages()
  115. {
  116. $firstMessage = 'This is the first warning message';
  117. $secondMessage = 'This is the second warning message';
  118. Yii::$app->session->setFlash('warning', [$firstMessage, $secondMessage]);
  119. $renderingResult = Alert::widget();
  120. expect($renderingResult)->stringContainsString($firstMessage);
  121. expect($renderingResult)->stringContainsString($secondMessage);
  122. expect($renderingResult)->stringContainsString('alert-warning');
  123. expect($renderingResult)->stringNotContainsString('alert-danger');
  124. expect($renderingResult)->stringNotContainsString('alert-success');
  125. expect($renderingResult)->stringNotContainsString('alert-info');
  126. }
  127. public function testSingleMixedMessages() {
  128. $errorMessage = 'This is an error message';
  129. $dangerMessage = 'This is a danger message';
  130. $successMessage = 'This is a success message';
  131. $infoMessage = 'This is a info message';
  132. $warningMessage = 'This is a warning message';
  133. Yii::$app->session->setFlash('error', $errorMessage);
  134. Yii::$app->session->setFlash('danger', $dangerMessage);
  135. Yii::$app->session->setFlash('success', $successMessage);
  136. Yii::$app->session->setFlash('info', $infoMessage);
  137. Yii::$app->session->setFlash('warning', $warningMessage);
  138. $renderingResult = Alert::widget();
  139. expect($renderingResult)->stringContainsString($errorMessage);
  140. expect($renderingResult)->stringContainsString($dangerMessage);
  141. expect($renderingResult)->stringContainsString($successMessage);
  142. expect($renderingResult)->stringContainsString($infoMessage);
  143. expect($renderingResult)->stringContainsString($warningMessage);
  144. expect($renderingResult)->stringContainsString('alert-danger');
  145. expect($renderingResult)->stringContainsString('alert-success');
  146. expect($renderingResult)->stringContainsString('alert-info');
  147. expect($renderingResult)->stringContainsString('alert-warning');
  148. }
  149. public function testMultipleMixedMessages() {
  150. $firstErrorMessage = 'This is the first error message';
  151. $secondErrorMessage = 'This is the second error message';
  152. $firstDangerMessage = 'This is the first danger message';
  153. $secondDangerMessage = 'This is the second';
  154. $firstSuccessMessage = 'This is the first success message';
  155. $secondSuccessMessage = 'This is the second success message';
  156. $firstInfoMessage = 'This is the first info message';
  157. $secondInfoMessage = 'This is the second info message';
  158. $firstWarningMessage = 'This is the first warning message';
  159. $secondWarningMessage = 'This is the second warning message';
  160. Yii::$app->session->setFlash('error', [$firstErrorMessage, $secondErrorMessage]);
  161. Yii::$app->session->setFlash('danger', [$firstDangerMessage, $secondDangerMessage]);
  162. Yii::$app->session->setFlash('success', [$firstSuccessMessage, $secondSuccessMessage]);
  163. Yii::$app->session->setFlash('info', [$firstInfoMessage, $secondInfoMessage]);
  164. Yii::$app->session->setFlash('warning', [$firstWarningMessage, $secondWarningMessage]);
  165. $renderingResult = Alert::widget();
  166. expect($renderingResult)->stringContainsString($firstErrorMessage);
  167. expect($renderingResult)->stringContainsString($secondErrorMessage);
  168. expect($renderingResult)->stringContainsString($firstDangerMessage);
  169. expect($renderingResult)->stringContainsString($secondDangerMessage);
  170. expect($renderingResult)->stringContainsString($firstSuccessMessage);
  171. expect($renderingResult)->stringContainsString($secondSuccessMessage);
  172. expect($renderingResult)->stringContainsString($firstInfoMessage);
  173. expect($renderingResult)->stringContainsString($secondInfoMessage);
  174. expect($renderingResult)->stringContainsString($firstWarningMessage);
  175. expect($renderingResult)->stringContainsString($secondWarningMessage);
  176. expect($renderingResult)->stringContainsString('alert-danger');
  177. expect($renderingResult)->stringContainsString('alert-success');
  178. expect($renderingResult)->stringContainsString('alert-info');
  179. expect($renderingResult)->stringContainsString('alert-warning');
  180. }
  181. public function testFlashIntegrity()
  182. {
  183. $errorMessage = 'This is an error message';
  184. $unrelatedMessage = 'This is a message that is not related to the alert widget';
  185. Yii::$app->session->setFlash('error', $errorMessage);
  186. Yii::$app->session->setFlash('unrelated', $unrelatedMessage);
  187. Alert::widget();
  188. // Simulate redirect
  189. Yii::$app->session->close();
  190. Yii::$app->session->open();
  191. expect(Yii::$app->session->getFlash('error'))->null();
  192. expect(Yii::$app->session->getFlash('unrelated'))->equals($unrelatedMessage);
  193. }
  194. }