CookieCollectionTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice.
  7. *
  8. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. * @link http://cakephp.org CakePHP(tm) Project
  10. * @since 3.5.0
  11. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  12. */
  13. namespace Cake\Test\TestCase\Http\Cookie;
  14. use Cake\Http\Cookie\Cookie;
  15. use Cake\Http\Cookie\CookieCollection;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * Cookie collection test.
  19. */
  20. class CookieCollectionTest extends TestCase
  21. {
  22. /**
  23. * Test constructor
  24. *
  25. * @return void
  26. */
  27. public function testConstructorWithEmptyArray()
  28. {
  29. $collection = new CookieCollection([]);
  30. $this->assertCount(0, $collection);
  31. }
  32. /**
  33. * Test valid cookies
  34. *
  35. * @return void
  36. */
  37. public function testConstructorWithCookieArray()
  38. {
  39. $cookies = [
  40. new Cookie('one', 'one'),
  41. new Cookie('two', 'two')
  42. ];
  43. $collection = new CookieCollection($cookies);
  44. $this->assertCount(2, $collection);
  45. }
  46. /**
  47. * Test iteration
  48. *
  49. * @return void
  50. */
  51. public function testIteration()
  52. {
  53. $cookies = [
  54. new Cookie('remember_me', 'a'),
  55. new Cookie('gtm', 'b'),
  56. new Cookie('three', 'tree')
  57. ];
  58. $collection = new CookieCollection($cookies);
  59. $names = [];
  60. foreach ($collection as $cookie) {
  61. $names[] = $cookie->getName();
  62. }
  63. $this->assertSame(['remember_me', 'gtm', 'three'], $names);
  64. }
  65. /**
  66. * Test adding cookies
  67. *
  68. * @return void
  69. */
  70. public function testAdd()
  71. {
  72. $cookies = [];
  73. $collection = new CookieCollection($cookies);
  74. $this->assertCount(0, $collection);
  75. $remember = new Cookie('remember_me', 'a');
  76. $new = $collection->add($remember);
  77. $this->assertNotSame($new, $collection->add($remember));
  78. $this->assertCount(0, $collection, 'Original instance not modified');
  79. $this->assertCount(1, $new);
  80. $this->assertFalse($collection->has('remember_me'), 'Original instance not modified');
  81. $this->assertTrue($new->has('remember_me'));
  82. $this->assertSame($remember, $new->get('remember_me'));
  83. $rememberNo = new Cookie('remember_me', 'no');
  84. $second = $new->add($remember)->add($rememberNo);
  85. $this->assertCount(1, $second);
  86. $this->assertNotSame($second, $new);
  87. $this->assertSame($rememberNo, $second->get('remember_me'));
  88. }
  89. /**
  90. * Test has()
  91. *
  92. * @return void
  93. */
  94. public function testHas()
  95. {
  96. $cookies = [
  97. new Cookie('remember_me', 'a'),
  98. new Cookie('gtm', 'b')
  99. ];
  100. $collection = new CookieCollection($cookies);
  101. $this->assertFalse($collection->has('nope'));
  102. $this->assertTrue($collection->has('remember_me'));
  103. $this->assertTrue($collection->has('REMEMBER_me'), 'case insensitive cookie names');
  104. }
  105. /**
  106. * Test removing cookies
  107. *
  108. * @return void
  109. */
  110. public function testRemove()
  111. {
  112. $cookies = [
  113. new Cookie('remember_me', 'a'),
  114. new Cookie('gtm', 'b')
  115. ];
  116. $collection = new CookieCollection($cookies);
  117. $this->assertInstanceOf(Cookie::class, $collection->get('REMEMBER_me'), 'case insensitive cookie names');
  118. $new = $collection->remove('remember_me');
  119. $this->assertTrue($collection->has('remember_me'), 'old instance not modified');
  120. $this->assertNotSame($new, $collection);
  121. $this->assertFalse($new->has('remember_me'), 'should be removed');
  122. $this->assertNull($new->get('remember_me'), 'should be removed');
  123. }
  124. /**
  125. * Test getting cookies by name
  126. *
  127. * @return void
  128. */
  129. public function testGetByName()
  130. {
  131. $cookies = [
  132. new Cookie('remember_me', 'a'),
  133. new Cookie('gtm', 'b')
  134. ];
  135. $collection = new CookieCollection($cookies);
  136. $this->assertNull($collection->get('nope'));
  137. $this->assertInstanceOf(Cookie::class, $collection->get('REMEMBER_me'), 'case insensitive cookie names');
  138. $this->assertInstanceOf(Cookie::class, $collection->get('remember_me'));
  139. $this->assertSame($cookies[0], $collection->get('remember_me'));
  140. }
  141. /**
  142. * Test that the constructor takes only an array of objects implementing
  143. * the CookieInterface
  144. *
  145. * @expectedException \InvalidArgumentException
  146. * @expectedExceptionMessage Expected `Cake\Http\Cookie\CookieCollection[]` as $cookies but instead got `array` at index 1
  147. * @return void
  148. */
  149. public function testConstructorWithInvalidCookieObjects()
  150. {
  151. $array = [
  152. new Cookie('one', 'one'),
  153. []
  154. ];
  155. new CookieCollection($array);
  156. }
  157. }