|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace spec\Http\Message; |
| 4 | + |
| 5 | +use Http\Message\Cookie; |
| 6 | +use PhpSpec\ObjectBehavior; |
| 7 | + |
| 8 | +class CookieJarSpec extends ObjectBehavior |
| 9 | +{ |
| 10 | + function it_is_initializable() |
| 11 | + { |
| 12 | + $this->shouldHaveType('Http\Message\CookieJar'); |
| 13 | + } |
| 14 | + |
| 15 | + function it_is_an_iterator_aggregate() |
| 16 | + { |
| 17 | + $this->getIterator()->shouldHaveType('Iterator'); |
| 18 | + } |
| 19 | + |
| 20 | + function it_has_a_cookie() |
| 21 | + { |
| 22 | + $cookie = new Cookie('name', 'value'); |
| 23 | + |
| 24 | + $this->addCookie($cookie); |
| 25 | + |
| 26 | + $this->hasCookie($cookie)->shouldReturn(true); |
| 27 | + $this->hasCookies()->shouldReturn(true); |
| 28 | + } |
| 29 | + |
| 30 | + function it_accepts_a_cookie() |
| 31 | + { |
| 32 | + $cookie = new Cookie('name', 'value'); |
| 33 | + $cookie2 = new Cookie('name', 'value2'); |
| 34 | + |
| 35 | + $this->addCookie($cookie); |
| 36 | + $this->addCookie($cookie2); |
| 37 | + |
| 38 | + $this->hasCookie($cookie)->shouldReturn(false); |
| 39 | + $this->hasCookie($cookie2)->shouldReturn(true); |
| 40 | + } |
| 41 | + |
| 42 | + function it_removes_a_cookie_with_an_empty_value() |
| 43 | + { |
| 44 | + $cookie = new Cookie('name', 'value'); |
| 45 | + $cookie2 = new Cookie('name'); |
| 46 | + |
| 47 | + $this->addCookie($cookie); |
| 48 | + $this->addCookie($cookie2); |
| 49 | + |
| 50 | + $this->hasCookie($cookie)->shouldReturn(false); |
| 51 | + $this->hasCookie($cookie2)->shouldReturn(false); |
| 52 | + } |
| 53 | + |
| 54 | + function it_removes_a_cookie_with_a_lower_expiration_time() |
| 55 | + { |
| 56 | + $cookie = new Cookie('name', 'value', 100); |
| 57 | + $cookie2 = new Cookie('name', 'value', 1000); |
| 58 | + |
| 59 | + $this->addCookie($cookie); |
| 60 | + $this->addCookie($cookie2); |
| 61 | + |
| 62 | + $this->hasCookie($cookie)->shouldReturn(false); |
| 63 | + $this->hasCookie($cookie2)->shouldReturn(true); |
| 64 | + } |
| 65 | + |
| 66 | + function it_removes_a_cookie() |
| 67 | + { |
| 68 | + $cookie = new Cookie('name', 'value', 100); |
| 69 | + |
| 70 | + $this->addCookie($cookie); |
| 71 | + $this->removeCookie($cookie); |
| 72 | + |
| 73 | + $this->hasCookie($cookie)->shouldReturn(false); |
| 74 | + } |
| 75 | + |
| 76 | + function it_returns_all_cookies() |
| 77 | + { |
| 78 | + $cookie = new Cookie('name', 'value'); |
| 79 | + $cookie2 = new Cookie('name2', 'value'); |
| 80 | + |
| 81 | + $this->addCookie($cookie); |
| 82 | + $this->addCookie($cookie2); |
| 83 | + |
| 84 | + $this->getCookies()->shouldBeAnArrayOfInstance('Http\Message\Cookie'); |
| 85 | + } |
| 86 | + |
| 87 | + function it_returns_the_matching_cookies() |
| 88 | + { |
| 89 | + $cookie = new Cookie('name', 'value'); |
| 90 | + $cookie2 = new Cookie('name', 'value2'); |
| 91 | + |
| 92 | + $this->addCookie($cookie); |
| 93 | + |
| 94 | + $this->getMatchingCookies($cookie2)->shouldBeAnArrayOfInstance('Http\Message\Cookie'); |
| 95 | + } |
| 96 | + |
| 97 | + function it_sets_cookies() |
| 98 | + { |
| 99 | + $cookie = new Cookie('name', 'value'); |
| 100 | + |
| 101 | + $this->setCookies([$cookie]); |
| 102 | + |
| 103 | + $this->hasCookie($cookie)->shouldReturn(true); |
| 104 | + $this->hasCookies()->shouldReturn(true); |
| 105 | + $this->count()->shouldReturn(1); |
| 106 | + } |
| 107 | + |
| 108 | + function it_accepts_cookies() |
| 109 | + { |
| 110 | + $cookie = new Cookie('name', 'value'); |
| 111 | + $cookie2 = new Cookie('name2', 'value'); |
| 112 | + |
| 113 | + $this->addCookie($cookie); |
| 114 | + $this->addCookies([$cookie2]); |
| 115 | + |
| 116 | + $this->hasCookie($cookie)->shouldReturn(true); |
| 117 | + $this->hasCookie($cookie2)->shouldReturn(true); |
| 118 | + $this->hasCookies()->shouldReturn(true); |
| 119 | + $this->count()->shouldReturn(2); |
| 120 | + } |
| 121 | + |
| 122 | + function it_removes_cookies() |
| 123 | + { |
| 124 | + $cookie = new Cookie('name', 'value'); |
| 125 | + $cookie2 = new Cookie('name2', 'value'); |
| 126 | + |
| 127 | + $this->addCookies([$cookie, $cookie2]); |
| 128 | + $this->removeCookies([$cookie2]); |
| 129 | + |
| 130 | + $this->hasCookie($cookie)->shouldReturn(true); |
| 131 | + $this->hasCookie($cookie2)->shouldReturn(false); |
| 132 | + $this->hasCookies()->shouldReturn(true); |
| 133 | + $this->count()->shouldReturn(1); |
| 134 | + } |
| 135 | + |
| 136 | + function it_removes_matching_cookies() |
| 137 | + { |
| 138 | + $cookie = new Cookie('name', 'value'); |
| 139 | + $cookie2 = new Cookie('name2', 'value', 0, 'php-http.org'); |
| 140 | + |
| 141 | + $this->addCookies([$cookie, $cookie2]); |
| 142 | + |
| 143 | + $this->removeMatchingCookies('name2', 'php-http.org', '/'); |
| 144 | + |
| 145 | + $this->hasCookie($cookie)->shouldReturn(true); |
| 146 | + $this->hasCookie($cookie2)->shouldReturn(false); |
| 147 | + $this->hasCookies()->shouldReturn(true); |
| 148 | + $this->count()->shouldReturn(1); |
| 149 | + } |
| 150 | + |
| 151 | + function it_clears_cookies() |
| 152 | + { |
| 153 | + $cookie = new Cookie('name', 'value', 0, 'php-http.org'); |
| 154 | + $cookie2 = new Cookie('name2', 'value'); |
| 155 | + |
| 156 | + $this->addCookies([$cookie, $cookie2]); |
| 157 | + |
| 158 | + $this->clear(); |
| 159 | + |
| 160 | + $this->hasCookies()->shouldReturn(false); |
| 161 | + $this->count()->shouldReturn(0); |
| 162 | + } |
| 163 | + |
| 164 | + public function getMatchers() |
| 165 | + { |
| 166 | + return [ |
| 167 | + 'beAnArrayOfInstance' => function ($subject, $instance) { |
| 168 | + foreach ($subject as $element) { |
| 169 | + if (!$element instanceof $instance) { |
| 170 | + return false; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return true; |
| 175 | + }, |
| 176 | + ]; |
| 177 | + } |
| 178 | +} |
0 commit comments