PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/NotificationBagTest.php

https://github.com/mikedfunk/notification
PHP | 921 lines | 778 code | 143 blank | 0 comment | 0 complexity | 43efea6b6d6b32b251201a81d42110ac MD5 | raw file
  1. <?php
  2. use Mockery as m;
  3. require_once 'Mocks/NotificationsBagMock.php';
  4. class NotificationBagTest extends PHPUnit_Framework_TestCase
  5. {
  6. public function tearDown()
  7. {
  8. m::close();
  9. }
  10. public function testIsConstructed()
  11. {
  12. $notificationBag = $this->getNotificationBag();
  13. $this->assertEquals('test', $notificationBag->getName());
  14. $this->assertEquals(array(), $notificationBag->getTypes());
  15. $this->assertNull($notificationBag->getDefaultFormat());
  16. }
  17. public function testAddType()
  18. {
  19. $notificationBag = $this->getNotificationBag();
  20. $this->assertEquals(array(), $notificationBag->getTypes());
  21. $notificationBag->addType('warning');
  22. $this->assertEquals(array('warning'), $notificationBag->getTypes());
  23. }
  24. public function testAddTypesArray()
  25. {
  26. $notificationBag = $this->getNotificationBag();
  27. $this->assertEquals(array(), $notificationBag->getTypes());
  28. $notificationBag->addType(array('info', 'danger'));
  29. $this->assertEquals(array('info', 'danger'), $notificationBag->getTypes());
  30. }
  31. public function testAddTypesArrayAsIndividualParam()
  32. {
  33. $notificationBag = $this->getNotificationBag();
  34. $this->assertEquals(array(), $notificationBag->getTypes());
  35. $notificationBag->addType('info', 'danger');
  36. $this->assertEquals(array('info', 'danger'), $notificationBag->getTypes());
  37. }
  38. public function testAddExistingType()
  39. {
  40. $notificationBag = $this->getNotificationBag();
  41. $notificationBag->addType('warning');
  42. $this->assertEquals(array('warning'), $notificationBag->getTypes());
  43. $notificationBag->addType('warning');
  44. $this->assertEquals(array('warning'), $notificationBag->getTypes());
  45. }
  46. public function testCheckIfTypeIsAvailable()
  47. {
  48. $notificationBag = $this->getNotificationBag();
  49. $this->assertFalse($notificationBag->typeIsAvailable('warning'));
  50. $notificationBag->addType('warning');
  51. $this->assertTrue($notificationBag->typeIsAvailable('warning'));
  52. }
  53. public function testClearTypes()
  54. {
  55. $notificationBag = $this->getNotificationBag();
  56. $this->assertEquals(array(), $notificationBag->getTypes());
  57. $notificationBag->addType('warning');
  58. $this->assertEquals(array('warning'), $notificationBag->getTypes());
  59. $notificationBag->clearTypes();
  60. $this->assertEquals(array(), $notificationBag->getTypes());
  61. }
  62. public function testExtractType()
  63. {
  64. $notificationBag = $this->getNotificationBag();
  65. $this->assertFalse($notificationBag->extractType('info'));
  66. $notificationBag->addType(array('info', 'success'));
  67. $this->assertEquals(array('info', 'add'), $notificationBag->extractType('info'));
  68. $this->assertEquals(array('info', 'instant'), $notificationBag->extractType('infoInstant'));
  69. $this->assertEquals(array('info', 'clear'), $notificationBag->extractType('clearInfo'));
  70. $this->assertEquals(array('info', 'show'), $notificationBag->extractType('showInfo'));
  71. $this->assertEquals(array('success', 'add'), $notificationBag->extractType('success'));
  72. }
  73. public function testExtractTypeInvalid()
  74. {
  75. $notificationBag = $this->getNotificationBag();
  76. $notificationBag->addType(array('info', 'success'));
  77. $this->assertFalse($notificationBag->extractType('ShowInfo'));
  78. $this->assertFalse($notificationBag->extractType('infoinstant'));
  79. }
  80. public function testSetDefaultFormat()
  81. {
  82. $notificationBag = $this->getNotificationBag();
  83. $notificationBag->setDefaultFormat(':type - :message');
  84. $this->assertEquals(':type - :message', $notificationBag->getDefaultFormat());
  85. }
  86. public function testSetFormatForType()
  87. {
  88. $notificationBag = $this->getNotificationBag();
  89. $notificationBag->addType('success');
  90. $this->assertFalse($notificationBag->getFormat('success'));
  91. $notificationBag->setFormat('success', 'OK - :message');
  92. $this->assertEquals('OK - :message', $notificationBag->getFormat('success'));
  93. }
  94. public function testSetFormatsArray()
  95. {
  96. $notificationBag = $this->getNotificationBag();
  97. $notificationBag->addType(array('success', 'info'));
  98. $this->assertFalse($notificationBag->getFormat('success'));
  99. $this->assertFalse($notificationBag->getFormat('info'));
  100. $notificationBag->setFormats(
  101. array(
  102. 'success' => 'OK - :message',
  103. 'info' => 'INFO - :message',
  104. )
  105. );
  106. $this->assertEquals('OK - :message', $notificationBag->getFormat('success'));
  107. $this->assertEquals('INFO - :message', $notificationBag->getFormat('info'));
  108. }
  109. public function testSetFormatForNonExistingType()
  110. {
  111. $notificationBag = $this->getNotificationBag();
  112. $this->assertFalse($notificationBag->getFormat('success'));
  113. $notificationBag->setFormat('success', 'OK - :message');
  114. $this->assertFalse($notificationBag->getFormat('success'));
  115. }
  116. public function testGetFormatWhenJustDefaultIsSet()
  117. {
  118. $notificationBag = $this->getNotificationBag();
  119. $notificationBag->addType('success');
  120. $this->assertFalse($notificationBag->getFormat('success'));
  121. $notificationBag->setDefaultFormat(':type - :message');
  122. $this->assertEquals(':type - :message', $notificationBag->getFormat('success'));
  123. }
  124. public function testClearFormat()
  125. {
  126. $notificationBag = $this->getNotificationBag();
  127. $notificationBag->addType(array('success', 'info'));
  128. $this->assertFalse(false, $notificationBag->getFormat('success'));
  129. $this->assertFalse(false, $notificationBag->getFormat('info'));
  130. $notificationBag->setFormats(
  131. array(
  132. 'success' => 'OK - :message',
  133. 'info' => 'INFO - :message',
  134. )
  135. );
  136. $this->assertEquals('OK - :message', $notificationBag->getFormat('success'));
  137. $this->assertEquals('INFO - :message', $notificationBag->getFormat('info'));
  138. $notificationBag->clearFormat('success');
  139. $this->assertFalse($notificationBag->getFormat('success'));
  140. $this->assertEquals('INFO - :message', $notificationBag->getFormat('info'));
  141. }
  142. public function testClearAllFormats()
  143. {
  144. $notificationBag = $this->getNotificationBag();
  145. $notificationBag->addType(array('success', 'info'));
  146. $this->assertFalse(false, $notificationBag->getFormat('success'));
  147. $this->assertFalse(false, $notificationBag->getFormat('info'));
  148. $notificationBag->setFormats(
  149. array(
  150. 'success' => 'OK - :message',
  151. 'info' => 'INFO - :message',
  152. )
  153. );
  154. $this->assertEquals('OK - :message', $notificationBag->getFormat('success'));
  155. $this->assertEquals('INFO - :message', $notificationBag->getFormat('info'));
  156. $notificationBag->clearFormats();
  157. $this->assertFalse($notificationBag->getFormat('success'));
  158. $this->assertFalse($notificationBag->getFormat('info'));
  159. }
  160. public function testAddMessageWithCustomFormat()
  161. {
  162. $notificationBag = $this->getNotificationBag();
  163. $notificationBag->addType('success');
  164. $this->assertCount(0, $notificationBag);
  165. $notificationBag->add('success', 'test', false, 'customFormat');
  166. $this->assertCount(1, $notificationBag);
  167. $notifications = $notificationBag->all();
  168. $this->assertEquals('customFormat', $notifications[0]->getFormat());
  169. }
  170. public function testAddInstantMessageUsingNamedMethod()
  171. {
  172. $notificationBag = $this->getNotificationBag();
  173. $notificationBag->addType('success');
  174. $this->assertCount(0, $notificationBag);
  175. $notificationBag->successInstant('test');
  176. $this->assertCount(1, $notificationBag);
  177. }
  178. public function testAddMessageForNonExistingType()
  179. {
  180. $notificationBag = $this->getNotificationBag();
  181. $this->assertCount(0, $notificationBag);
  182. $notificationBag->add('success', 'test', false);
  183. $this->assertCount(0, $notificationBag);
  184. }
  185. public function testAddMessagesForMultipleTypes()
  186. {
  187. $notificationBag = $this->getNotificationBag();
  188. $notificationBag->addType(array('success', 'info'));
  189. $this->assertCount(0, $notificationBag);
  190. $notificationBag->add('success', 'test', false);
  191. $notificationBag->add('info', 'test', false);
  192. $this->assertCount(2, $notificationBag);
  193. }
  194. public function testAddMessagesForMultipleTypesUsingNamedMethods()
  195. {
  196. $notificationBag = $this->getNotificationBag();
  197. $notificationBag->addType(array('success', 'info'));
  198. $this->assertCount(0, $notificationBag);
  199. $notificationBag->add('success', 'test', false);
  200. $notificationBag->add('info', 'test', false);
  201. $this->assertCount(2, $notificationBag);
  202. }
  203. public function testAddInstantMessageWithMessageInstance()
  204. {
  205. $notificationBag = $this->getNotificationBag();
  206. $notificationBag->addType('info');
  207. $this->assertCount(0, $notificationBag);
  208. $message = $this->getMessage();
  209. $message->shouldReceive('setType')->with('info')->andReturn($message);
  210. $message->shouldReceive('isFlashable')->andReturn(false);
  211. $message->shouldReceive('getPosition')->andReturn(null);
  212. $message->shouldReceive('getAlias')->andReturn(null);
  213. $message->shouldReceive('getFormat')->andReturn(':message');
  214. $notificationBag->add('info', $message, false);
  215. $this->assertCount(1, $notificationBag);
  216. }
  217. public function testAddFlashMessageWithMessageInstance()
  218. {
  219. $notificationBag = $this->getNotificationBag();
  220. $notificationBag->addType('info');
  221. $message = $this->getMessage();
  222. $message->shouldReceive('setType')->with('info')->andReturn($message);
  223. $message->shouldReceive('isFlashable')->andReturn(true);
  224. $message->shouldReceive('getPosition')->andReturn(null);
  225. $message->shouldReceive('getAlias')->andReturn(null);
  226. $message->shouldReceive('getFormat')->andReturn(':message');
  227. $this->assertCount(0, $notificationBag);
  228. $notificationBag->add('info', $message);
  229. $this->assertCount(0, $notificationBag);
  230. }
  231. public function testAddInstantMessageWithMessageInstanceUsingNamedMethods()
  232. {
  233. $notificationBag = $this->getNotificationBag();
  234. $notificationBag->addType('info');
  235. $this->assertCount(0, $notificationBag);
  236. $message = $this->getMessage();
  237. $message->shouldReceive('setType')->with('info')->andReturn($message);
  238. $message->shouldReceive('isFlashable')->andReturn(false);
  239. $message->shouldReceive('getPosition')->andReturn(null);
  240. $message->shouldReceive('getAlias')->andReturn(null);
  241. $message->shouldReceive('getFormat')->andReturn(':message');
  242. $notificationBag->infoInstant($message);
  243. $this->assertCount(1, $notificationBag);
  244. }
  245. public function testAddInstantMessageWithMessageInstanceUsingNamedMethodsOverrideFlashStatus()
  246. {
  247. $notificationBag = $this->getNotificationBag();
  248. $notificationBag->addType('info');
  249. $this->assertCount(0, $notificationBag);
  250. $message = $this->getMessage();
  251. $message->shouldReceive('setType')->with('info')->andReturn($message);
  252. $message->shouldReceive('isFlashable')->andReturn(true, false);
  253. $message->shouldReceive('setFlashable')->with(false)->andReturn($message);
  254. $message->shouldReceive('getPosition')->andReturn(null);
  255. $message->shouldReceive('getAlias')->andReturn(null);
  256. $message->shouldReceive('getFormat')->andReturn(':message');
  257. $notificationBag->infoInstant($message);
  258. $this->assertCount(1, $notificationBag);
  259. }
  260. public function testAddInstantMessageWithMessageInstanceSetFormatIfNotSet()
  261. {
  262. $notificationBag = $this->getNotificationBag();
  263. $notificationBag->addType('info');
  264. $notificationBag->setDefaultFormat(':message');
  265. $this->assertCount(0, $notificationBag);
  266. $message = $this->getMessage();
  267. $message->shouldReceive('setType')->with('info')->andReturn($message);
  268. $message->shouldReceive('isFlashable')->andReturn(false);
  269. $message->shouldReceive('getFormat')->andReturn(null);
  270. $message->shouldReceive('setFormat')->once()->with(':message')->andReturn($message);
  271. $message->shouldReceive('getPosition')->andReturn(null);
  272. $message->shouldReceive('getAlias')->andReturn(null);
  273. $notificationBag->add('info', $message, false);
  274. $this->assertCount(1, $notificationBag);
  275. }
  276. public function testAddInstantMessageWithMessageInstanceAndOverrideFormat()
  277. {
  278. $notificationBag = $this->getNotificationBag();
  279. $notificationBag->addType('info');
  280. $this->assertCount(0, $notificationBag);
  281. $message = $this->getMessage();
  282. $message->shouldReceive('setType')->with('info')->andReturn($message);
  283. $message->shouldReceive('isFlashable')->andReturn(false);
  284. $message->shouldReceive('getFormat')->andReturn('m');
  285. $message->shouldReceive('setFormat')->with(':message')->andReturn($message);
  286. $message->shouldReceive('getPosition')->andReturn(null);
  287. $message->shouldReceive('getAlias')->andReturn(null);
  288. $notificationBag->add('info', $message, false, ':message');
  289. $this->assertCount(1, $notificationBag);
  290. }
  291. public function testAddMessageAtPosition()
  292. {
  293. $notificationBag = $this->getNotificationBag();
  294. $notificationBag->addType('info');
  295. $this->assertCount(0, $notificationBag);
  296. $message = $this->getMessage();
  297. $message->shouldReceive('setType')->with('info')->andReturn($message);
  298. $message->shouldReceive('isFlashable')->andReturn(false);
  299. $message->shouldReceive('getPosition')->andReturn(5);
  300. $message->shouldReceive('getAlias')->andReturn(null);
  301. $message->shouldReceive('getFormat')->andReturn(':message');
  302. $notificationBag->add('info', $message, false);
  303. $this->assertCount(1, $notificationBag);
  304. $this->assertEquals($message, $notificationBag->getAtPosition(5));
  305. }
  306. public function testAddMessagesAtSamePosition()
  307. {
  308. $notificationBag = $this->getNotificationBag();
  309. $notificationBag->addType('info');
  310. $this->assertCount(0, $notificationBag);
  311. $message1 = $this->getMessage();
  312. $message1->shouldReceive('setType')->with('info')->andReturn($message1);
  313. $message1->shouldReceive('isFlashable')->andReturn(false);
  314. $message1->shouldReceive('getPosition')->andReturn(5);
  315. $message1->shouldReceive('getAlias')->andReturn(null);
  316. $message1->shouldReceive('getFormat')->andReturn(':message');
  317. $message2 = $this->getMessage();
  318. $message2->shouldReceive('setType')->with('info')->andReturn($message2);
  319. $message2->shouldReceive('isFlashable')->andReturn(false);
  320. $message2->shouldReceive('getPosition')->andReturn(5);
  321. $message2->shouldReceive('getAlias')->andReturn(null);
  322. $message2->shouldReceive('getFormat')->andReturn(':message');
  323. $notificationBag->add('info', $message1, false);
  324. $notificationBag->add('info', $message2, false);
  325. $this->assertCount(2, $notificationBag);
  326. $this->assertEquals($message2, $notificationBag->getAtPosition(5));
  327. $this->assertEquals($message1, $notificationBag->getAtPosition(6));
  328. }
  329. public function testAddMessageWithAlias()
  330. {
  331. $notificationBag = $this->getNotificationBag();
  332. $notificationBag->addType('info');
  333. $this->assertCount(0, $notificationBag);
  334. $message = $this->getMessage();
  335. $message->shouldReceive('setType')->with('info')->andReturn($message);
  336. $message->shouldReceive('isFlashable')->andReturn(false);
  337. $message->shouldReceive('getPosition')->andReturn(null);
  338. $message->shouldReceive('getAlias')->andReturn('test_alias');
  339. $message->shouldReceive('getFormat')->andReturn(':message');
  340. $notificationBag->add('info', $message, false);
  341. $this->assertCount(1, $notificationBag);
  342. $this->assertEquals($message, $notificationBag->getAliased('test_alias'));
  343. }
  344. public function testAddMessagesWithSameAlias()
  345. {
  346. $notificationBag = $this->getNotificationBag();
  347. $notificationBag->addType(array('info', 'danger'));
  348. $this->assertCount(0, $notificationBag);
  349. $message1 = $this->getMessage();
  350. $message1->shouldReceive('setType')->with('info')->andReturn($message1);
  351. $message1->shouldReceive('isFlashable')->andReturn(false);
  352. $message1->shouldReceive('getPosition')->andReturn(null);
  353. $message1->shouldReceive('getAlias')->andReturn('test_alias');
  354. $message1->shouldReceive('getFormat')->andReturn(':message');
  355. $message2 = $this->getMessage();
  356. $message2->shouldReceive('setType')->with('danger')->andReturn($message2);
  357. $message2->shouldReceive('isFlashable')->andReturn(false);
  358. $message2->shouldReceive('getPosition')->andReturn(null);
  359. $message2->shouldReceive('getAlias')->andReturn('test_alias');
  360. $message2->shouldReceive('getFormat')->andReturn(':message');
  361. $notificationBag->add('info', $message1, false);
  362. $notificationBag->add('danger', $message2, false);
  363. $this->assertCount(1, $notificationBag);
  364. $this->assertEquals($message2, $notificationBag->getAliased('test_alias'));
  365. }
  366. public function testAddMessageWithAliasAndPosition()
  367. {
  368. $notificationBag = $this->getNotificationBag();
  369. $notificationBag->addType('info');
  370. $this->assertCount(0, $notificationBag);
  371. $message = $this->getMessage();
  372. $message->shouldReceive('setType')->with('info')->andReturn($message);
  373. $message->shouldReceive('isFlashable')->andReturn(false);
  374. $message->shouldReceive('getPosition')->andReturn(5);
  375. $message->shouldReceive('getAlias')->andReturn('test_alias');
  376. $message->shouldReceive('getFormat')->andReturn(':message');
  377. $notificationBag->add('info', $message, false);
  378. $this->assertCount(1, $notificationBag);
  379. $this->assertEquals($message, $notificationBag->getAtPosition(5));
  380. $this->assertEquals($message, $notificationBag->getAliased('test_alias'));
  381. }
  382. public function testAddMessagesWithSameAliasDifferentPositions()
  383. {
  384. $notificationBag = $this->getNotificationBag();
  385. $notificationBag->addType(array('info', 'danger'));
  386. $this->assertCount(0, $notificationBag);
  387. $message1 = $this->getMessage();
  388. $message1->shouldReceive('setType')->with('info')->andReturn($message1);
  389. $message1->shouldReceive('isFlashable')->andReturn(false);
  390. $message1->shouldReceive('getPosition')->andReturn(5);
  391. $message1->shouldReceive('getAlias')->andReturn('test_alias');
  392. $message1->shouldReceive('getFormat')->andReturn(':message');
  393. $message2 = $this->getMessage();
  394. $message2->shouldReceive('setType')->with('danger')->andReturn($message2);
  395. $message2->shouldReceive('isFlashable')->andReturn(false);
  396. $message2->shouldReceive('getPosition')->andReturn(9);
  397. $message2->shouldReceive('getAlias')->andReturn('test_alias');
  398. $message2->shouldReceive('getFormat')->andReturn(':message');
  399. $notificationBag->add('info', $message1, false);
  400. $notificationBag->add('danger', $message2, false);
  401. $this->assertCount(1, $notificationBag);
  402. $this->assertEquals($message2, $notificationBag->getAtPosition(9));
  403. $this->assertEquals($message2, $notificationBag->getAliased('test_alias'));
  404. }
  405. public function testAddArrayOfMessagesForGivenType()
  406. {
  407. $notificationBag = $this->getNotificationBag();
  408. $notificationBag->addType('info');
  409. $this->assertCount(0, $notificationBag);
  410. $notificationBag->add('info', array('test1', 'test2'), false);
  411. $this->assertCount(2, $notificationBag);
  412. $notifications = $notificationBag->all();
  413. $this->assertEquals('test1', $notifications[0]->getMessage());
  414. $this->assertEquals('test2', $notifications[1]->getMessage());
  415. }
  416. public function testAddArrayOfMessagesForGivenTypeOverrideFormat()
  417. {
  418. $notificationBag = $this->getNotificationBag();
  419. $notificationBag->addType('info');
  420. $this->assertCount(0, $notificationBag);
  421. $notificationBag->add('info', array('test1', 'test2'), false, 'custom');
  422. $this->assertCount(2, $notificationBag);
  423. $notifications = $notificationBag->all();
  424. $this->assertEquals('test1', $notifications[0]->getMessage());
  425. $this->assertEquals('custom', $notifications[0]->getFormat());
  426. $this->assertEquals('test2', $notifications[1]->getMessage());
  427. $this->assertEquals('custom', $notifications[1]->getFormat());
  428. }
  429. public function testAddArrayOfMessagesWithFormats()
  430. {
  431. $notificationBag = $this->getNotificationBag();
  432. $notificationBag->addType('info');
  433. $this->assertCount(0, $notificationBag);
  434. $notificationBag->add('info', array(array('test1', 'custom1'), array('test2', 'custom2')), false);
  435. $this->assertCount(2, $notificationBag);
  436. $notifications = $notificationBag->all();
  437. $this->assertEquals('test1', $notifications[0]->getMessage());
  438. $this->assertEquals('custom1', $notifications[0]->getFormat());
  439. $this->assertEquals('test2', $notifications[1]->getMessage());
  440. $this->assertEquals('custom2', $notifications[1]->getFormat());
  441. }
  442. public function testAddArrayOfMessagesWithFormatsOverrideFormat()
  443. {
  444. $notificationBag = $this->getNotificationBag();
  445. $notificationBag->addType('info');
  446. $this->assertCount(0, $notificationBag);
  447. $notificationBag->add('info', array(array('test1', 'custom1'), 'test2'), false, 'c');
  448. $this->assertCount(2, $notificationBag);
  449. $notifications = $notificationBag->all();
  450. $this->assertEquals('test1', $notifications[0]->getMessage());
  451. $this->assertEquals('custom1', $notifications[0]->getFormat());
  452. $this->assertEquals('test2', $notifications[1]->getMessage());
  453. $this->assertEquals('c', $notifications[1]->getFormat());
  454. }
  455. public function testAddArrayOfMessagesWithAssociativeArrayAsParams()
  456. {
  457. $notificationBag = $this->getNotificationBag();
  458. $notificationBag->addType('info');
  459. $this->assertCount(0, $notificationBag);
  460. $messages = array(
  461. array(
  462. 'message' => 'm1',
  463. 'format' => 'f1',
  464. 'alias' => 'a1',
  465. 'position' => 5
  466. ),
  467. array(
  468. 'message' => 'm2',
  469. 'format' => 'f2',
  470. 'alias' => 'a2',
  471. 'position' => 9
  472. ),
  473. );
  474. $notificationBag->add('info', $messages, false);
  475. $this->assertCount(2, $notificationBag);
  476. }
  477. public function testAddArrayOfMessagesInstances()
  478. {
  479. $notificationBag = $this->getNotificationBag();
  480. $notificationBag->addType('info');
  481. $this->assertCount(0, $notificationBag);
  482. $messages = array();
  483. $messages[0] = $this->getMessage();
  484. $messages[0]->shouldReceive('setType')->with('info')->andReturn($messages[0]);
  485. $messages[0]->shouldReceive('isFlashable')->andReturn(false);
  486. $messages[0]->shouldReceive('getPosition')->andReturn(null);
  487. $messages[0]->shouldReceive('getAlias')->andReturn(null);
  488. $messages[0]->shouldReceive('getFormat')->andReturn(':message');
  489. $notificationBag->add('info', $messages[0], false);
  490. $messages[1] = $this->getMessage();
  491. $messages[1]->shouldReceive('setType')->with('info')->andReturn($messages[1]);
  492. $messages[1]->shouldReceive('isFlashable')->andReturn(false);
  493. $messages[1]->shouldReceive('getPosition')->andReturn(null);
  494. $messages[1]->shouldReceive('getAlias')->andReturn(null);
  495. $messages[1]->shouldReceive('getFormat')->andReturn(':message');
  496. $notificationBag->add('info', $messages[1], false);
  497. $this->assertCount(2, $notificationBag);
  498. $notifications = $notificationBag->all();
  499. $this->assertEquals($messages[0], $notifications[0]);
  500. $this->assertEquals($messages[1], $notifications[1]);
  501. }
  502. public function testGetInstantMessagesForGivenType()
  503. {
  504. $notificationBag = $this->getNotificationBag();
  505. $notificationBag->addType(array('success', 'info'));
  506. $this->assertCount(0, $notificationBag);
  507. $notificationBag->successInstant('test');
  508. $notificationBag->successInstant('test2');
  509. $notificationBag->infoInstant('test');
  510. $this->assertCount(3, $notificationBag);
  511. $this->assertCount(2, $notificationBag->get('success'));
  512. $this->assertCount(1, $notificationBag->get('info'));
  513. }
  514. public function testGetInstantMessagesForGivenTypeWhenMessageHasPosition()
  515. {
  516. $notificationBag = $this->getNotificationBag();
  517. $notificationBag->addType(array('info', 'danger'));
  518. $this->assertCount(0, $notificationBag);
  519. $message = $this->getMessage();
  520. $message->shouldReceive('setType')->with('info')->andReturn($message);
  521. $message->shouldReceive('getType')->andReturn('info');
  522. $message->shouldReceive('isFlashable')->andReturn(false);
  523. $message->shouldReceive('getPosition')->andReturn(5);
  524. $message->shouldReceive('getAlias')->andReturn(null);
  525. $message->shouldReceive('getFormat')->andReturn(':message');
  526. $notificationBag->add('info', $message, false);
  527. $notificationBag->add('danger', 'test', false);
  528. $this->assertCount(2, $notificationBag);
  529. $this->assertCount(1, $notificationBag->get('info'));
  530. $this->assertEquals($message, $notificationBag->get('info')->getAtPosition(5));
  531. }
  532. public function testClearMessagesForGivenType()
  533. {
  534. $notificationBag = $this->getNotificationBag();
  535. $notificationBag->addType(array('success', 'info'));
  536. $this->assertCount(0, $notificationBag->get('success'));
  537. $this->assertCount(0, $notificationBag->get('info'));
  538. $notificationBag->add('success', 'test', false);
  539. $notificationBag->add('info', 'test', false);
  540. $this->assertCount(1, $notificationBag->get('success'));
  541. $this->assertCount(1, $notificationBag->get('info'));
  542. $notificationBag->clear('success');
  543. $this->assertCount(0, $notificationBag->get('success'));
  544. $this->assertCount(1, $notificationBag->get('info'));
  545. $notificationBag->clear('info');
  546. $this->assertCount(0, $notificationBag->get('success'));
  547. $this->assertCount(0, $notificationBag->get('info'));
  548. }
  549. public function testClearMessagesForGivenTypeUsingNamedMethod()
  550. {
  551. $notificationBag = $this->getNotificationBag();
  552. $notificationBag->addType(array('success', 'info'));
  553. $this->assertCount(0, $notificationBag->get('success'));
  554. $this->assertCount(0, $notificationBag->get('info'));
  555. $notificationBag->add('success', 'test', false);
  556. $notificationBag->add('info', 'test', false);
  557. $this->assertCount(1, $notificationBag->get('success'));
  558. $this->assertCount(1, $notificationBag->get('info'));
  559. $notificationBag->clearSuccess();
  560. $this->assertCount(0, $notificationBag->get('success'));
  561. $this->assertCount(1, $notificationBag->get('info'));
  562. $notificationBag->clearInfo();
  563. $this->assertCount(0, $notificationBag->get('success'));
  564. $this->assertCount(0, $notificationBag->get('info'));
  565. }
  566. public function testClearAllMessages()
  567. {
  568. $notificationBag = $this->getNotificationBag();
  569. $notificationBag->addType(array('success', 'info'));
  570. $this->assertCount(0, $notificationBag->get('success'));
  571. $this->assertCount(0, $notificationBag->get('info'));
  572. $notificationBag->add('success', 'test', false);
  573. $notificationBag->add('info', 'test', false);
  574. $this->assertCount(1, $notificationBag->get('success'));
  575. $this->assertCount(1, $notificationBag->get('info'));
  576. $notificationBag->clearAll();
  577. $this->assertCount(0, $notificationBag->get('success'));
  578. $this->assertCount(0, $notificationBag->get('info'));
  579. }
  580. public function testClearAllMessageWithoutGivenType()
  581. {
  582. $notificationBag = $this->getNotificationBag();
  583. $notificationBag->addType(array('success', 'info'));
  584. $this->assertCount(0, $notificationBag->get('success'));
  585. $this->assertCount(0, $notificationBag->get('info'));
  586. $notificationBag->add('success', 'test', false);
  587. $notificationBag->add('info', 'test', false);
  588. $this->assertCount(1, $notificationBag->get('success'));
  589. $this->assertCount(1, $notificationBag->get('info'));
  590. $notificationBag->clear();
  591. $this->assertCount(0, $notificationBag->get('success'));
  592. $this->assertCount(0, $notificationBag->get('info'));
  593. }
  594. public function testGetAllMessages()
  595. {
  596. $notificationBag = $this->getNotificationBag();
  597. $notificationBag->addType(array('success', 'info'));
  598. $this->assertCount(0, $notificationBag);
  599. $notificationBag->add('success', 'test', false);
  600. $notificationBag->add('info', 'test', false);
  601. $this->assertCount(2, $notificationBag->all());
  602. }
  603. public function testGetFirstMessage()
  604. {
  605. $notificationBag = $this->getNotificationBag();
  606. $notificationBag->addType(array('success', 'info'));
  607. $this->assertCount(0, $notificationBag);
  608. $this->assertNull($notificationBag->first());
  609. $notificationBag->add('success', 'test', false);
  610. $notificationBag->add('info', 'test', false);
  611. $this->assertCount(2, $notificationBag);
  612. $this->assertEquals('success', $notificationBag->first()->getType());
  613. $this->assertEquals('test', $notificationBag->first()->getMessage());
  614. }
  615. public function testShowMessagesForGivenType()
  616. {
  617. $notificationBag = $this->getNotificationBag();
  618. $notificationBag->addType(array('success', 'info'));
  619. $notificationBag->setDefaultFormat(':type - :message');
  620. $this->assertCount(0, $notificationBag);
  621. $notificationBag->add('success', 'test', false);
  622. $notificationBag->add('info', 'test', false);
  623. $this->assertCount(2, $notificationBag);
  624. $this->assertEquals('success - test', $notificationBag->show('success'));
  625. $this->assertEquals('info - test', $notificationBag->show('info'));
  626. }
  627. public function testShowMessagesForGivenTypeWithCustomFormat()
  628. {
  629. $notificationBag = $this->getNotificationBag();
  630. $notificationBag->addType('success');
  631. $notificationBag->setDefaultFormat(':type - :message');
  632. $this->assertCount(0, $notificationBag);
  633. $notificationBag->add('success', 'test', false);
  634. $this->assertCount(1, $notificationBag);
  635. $this->assertEquals('test - OK', $notificationBag->show('success', ':message - OK'));
  636. }
  637. public function testShowMessagesForGivenTypeUsingNamedMethods()
  638. {
  639. $notificationBag = $this->getNotificationBag();
  640. $notificationBag->addType(array('success', 'info'));
  641. $notificationBag->setDefaultFormat(':type - :message');
  642. $this->assertCount(0, $notificationBag);
  643. $notificationBag->add('success', 'test', false);
  644. $notificationBag->add('info', 'test', false);
  645. $this->assertCount(2, $notificationBag);
  646. $this->assertEquals('success - test', $notificationBag->showSuccess());
  647. $this->assertEquals('info - test', $notificationBag->showInfo());
  648. }
  649. public function testShowAllMessages()
  650. {
  651. $notificationBag = $this->getNotificationBag();
  652. $notificationBag->addType(array('success', 'info'));
  653. $notificationBag->setDefaultFormat(':type - :message');
  654. $this->assertCount(0, $notificationBag);
  655. $notificationBag->add('success', 'test', false);
  656. $notificationBag->add('info', 'test', false);
  657. $this->assertCount(2, $notificationBag);
  658. $this->assertEquals('success - testinfo - test', $notificationBag->show());
  659. $this->assertEquals('success - testinfo - test', $notificationBag->showAll());
  660. }
  661. public function testAddTypesForGroupedRendering()
  662. {
  663. $notificationBag = $this->getNotificationBag();
  664. $notificationBag->addType(array('success', 'info'));
  665. $this->assertEquals(array(), $notificationBag->getGroupingForRender());
  666. $notificationBag->addToGrouping('success');
  667. $this->assertEquals(array('success'), $notificationBag->getGroupingForRender());
  668. $notificationBag->addToGrouping('info');
  669. $this->assertEquals(array('success', 'info'), $notificationBag->getGroupingForRender());
  670. }
  671. public function testAddAndRemoveTypesForGroupedRendering()
  672. {
  673. $notificationBag = $this->getNotificationBag();
  674. $notificationBag->addType(array('success', 'info'));
  675. $this->assertEquals(array(), $notificationBag->getGroupingForRender());
  676. $notificationBag->addToGrouping('success');
  677. $this->assertEquals(array('success'), $notificationBag->getGroupingForRender());
  678. $notificationBag->addToGrouping('info');
  679. $this->assertEquals(array('success', 'info'), $notificationBag->getGroupingForRender());
  680. $notificationBag->removeFromGrouping('success');
  681. $this->assertEquals(array('info'), $notificationBag->getGroupingForRender());
  682. }
  683. public function testAddTypesForGroupedRenderingInvalidType()
  684. {
  685. $notificationBag = $this->getNotificationBag();
  686. $this->assertEquals(array(), $notificationBag->getGroupingForRender());
  687. $notificationBag->addToGrouping('success');
  688. $this->assertEquals(array(), $notificationBag->getGroupingForRender());
  689. }
  690. public function testShowGroupedMessages()
  691. {
  692. $notificationBag = $this->getNotificationBag();
  693. $notificationBag->addType(array('success', 'info'));
  694. $notificationBag->setDefaultFormat(':type - :message');
  695. $notificationBag->add('success', 'test', false);
  696. $notificationBag->add('info', 'test2', false);
  697. $this->assertEquals('success - test', $notificationBag->group('success')->show());
  698. $this->assertEquals('info - test2success - test', $notificationBag->group('info', 'success')->show());
  699. }
  700. public function testSetNotificationInstance()
  701. {
  702. $notificationBag = $this->getNotificationBag();
  703. $this->assertNull($notificationBag->getEventDispatcher());
  704. $notificationBag->setNotification($notification = m::mock('Krucas\Notification\Notification'));
  705. $this->assertEquals($notification, $notificationBag->getNotification());
  706. $notificationBag->unsetNotification();
  707. $this->assertNull($notificationBag->getNotification());
  708. }
  709. public function testToString()
  710. {
  711. $notificationBag = $this->getNotificationBag();
  712. $notificationBag->addType('info');
  713. $notificationBag->setDefaultFormat(':type - :message');
  714. $notificationBag->add('info', 'ok', false);
  715. $this->assertEquals('info - ok', (string) $notificationBag);
  716. }
  717. public function testToArray()
  718. {
  719. $notificationBag = $this->getNotificationBag();
  720. $notificationBag->addType('info');
  721. $notificationBag->setDefaultFormat(':message');
  722. $notificationBag->add('info', 'test', false);
  723. $this->assertEquals(
  724. array(
  725. 'container' => 'test',
  726. 'format' => ':message',
  727. 'types' => array('info'),
  728. 'notifications' => array(
  729. array(
  730. 'message' => 'test',
  731. 'format' => ':message',
  732. 'type' => 'info',
  733. 'flashable' => false,
  734. 'alias' => null,
  735. 'position' => null
  736. )
  737. )
  738. ),
  739. $notificationBag->toArray()
  740. );
  741. }
  742. public function testToJson()
  743. {
  744. $notificationBag = $this->getNotificationBag();
  745. $notificationBag->addType('info');
  746. $notificationBag->setDefaultFormat(':message');
  747. $notificationBag->add('info', 'test', false);
  748. $this->assertEquals(
  749. json_encode(
  750. array(
  751. 'container' => 'test',
  752. 'format' => ':message',
  753. 'types' => array('info'),
  754. 'notifications' => array(
  755. array(
  756. 'message' => 'test',
  757. 'format' => ':message',
  758. 'type' => 'info',
  759. 'flashable' => false,
  760. 'alias' => null,
  761. 'position' => null
  762. )
  763. )
  764. )
  765. ),
  766. $notificationBag->toJson()
  767. );
  768. }
  769. protected function getNotificationBag()
  770. {
  771. return new NotificationsBagMock('test');
  772. }
  773. protected function getMessage()
  774. {
  775. $message = m::mock('Krucas\Notification\Message');
  776. return $message;
  777. }
  778. }