psr7-sessions /
storageless
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
| 4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
| 5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||
| 6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||
| 7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||
| 8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||
| 9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
| 10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
| 11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
| 12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
| 13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 14 | * |
||
| 15 | * This software consists of voluntary contributions made by many individuals |
||
| 16 | * and is licensed under the MIT license. |
||
| 17 | */ |
||
| 18 | |||
| 19 | declare(strict_types=1); |
||
| 20 | |||
| 21 | namespace PSR7SessionsTest\Storageless\Session; |
||
| 22 | |||
| 23 | use PHPUnit\Framework\MockObject\MockObject; |
||
| 24 | use PHPUnit\Framework\TestCase; |
||
| 25 | use PSR7Sessions\Storageless\Session\LazySession; |
||
| 26 | use PSR7Sessions\Storageless\Session\SessionInterface; |
||
| 27 | use stdClass; |
||
| 28 | use function assert; |
||
| 29 | use function is_callable; |
||
| 30 | use function uniqid; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @covers \PSR7Sessions\Storageless\Session\LazySession |
||
| 34 | */ |
||
| 35 | final class LazySessionTest extends TestCase |
||
| 36 | { |
||
| 37 | /** @var SessionInterface&MockObject */ |
||
| 38 | private SessionInterface $wrappedSession; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 39 | |||
| 40 | /** @var callable&MockObject */ |
||
| 41 | private MockObject $sessionLoader; |
||
| 42 | |||
| 43 | private LazySession $lazySession; |
||
| 44 | |||
| 45 | protected function setUp() : void |
||
| 46 | { |
||
| 47 | $this->wrappedSession = $this->createMock(SessionInterface::class); |
||
| 48 | $sessionLoader = $this->getMockBuilder(stdClass::class)->setMethods(['__invoke'])->getMock(); |
||
| 49 | assert(is_callable($sessionLoader)); |
||
| 50 | $this->sessionLoader = $sessionLoader; |
||
| 51 | $this->lazySession = LazySession::fromContainerBuildingCallback($this->sessionLoader); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function testLazyNonInitializedSessionIsAlwaysNotChanged() : void |
||
| 55 | { |
||
| 56 | $this->sessionLoader->expects(self::never())->method('__invoke'); |
||
| 57 | |||
| 58 | self::assertFalse($this->lazySession->hasChanged()); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function testHasChanged() : void |
||
| 62 | { |
||
| 63 | $this->wrappedSessionWillBeLoaded(); |
||
| 64 | |||
| 65 | $this->wrappedSession->expects(self::at(1))->method('hasChanged')->willReturn(true); |
||
| 66 | $this->wrappedSession->expects(self::at(2))->method('hasChanged')->willReturn(false); |
||
| 67 | |||
| 68 | $this->forceWrappedSessionInitialization(); |
||
| 69 | |||
| 70 | self::assertTrue($this->lazySession->hasChanged()); |
||
| 71 | self::assertFalse($this->lazySession->hasChanged()); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function testHas() : void |
||
| 75 | { |
||
| 76 | $this->wrappedSessionWillBeLoaded(); |
||
| 77 | |||
| 78 | $this->wrappedSession->expects(self::exactly(2))->method('has')->willReturnMap([ |
||
| 79 | ['foo', false], |
||
| 80 | ['bar', true], |
||
| 81 | ]); |
||
| 82 | |||
| 83 | self::assertFalse($this->lazySession->has('foo')); |
||
| 84 | self::assertTrue($this->lazySession->has('bar')); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function testGet() : void |
||
| 88 | { |
||
| 89 | $this->wrappedSessionWillBeLoaded(); |
||
| 90 | |||
| 91 | $this->wrappedSession->expects(self::exactly(3))->method('get')->willReturnMap([ |
||
| 92 | ['foo', null, 'bar'], |
||
| 93 | ['baz', null, 'tab'], |
||
| 94 | ['baz', 'default', 'taz'], |
||
| 95 | ]); |
||
| 96 | |||
| 97 | self::assertSame('bar', $this->lazySession->get('foo')); |
||
| 98 | self::assertSame('tab', $this->lazySession->get('baz')); |
||
| 99 | self::assertSame('taz', $this->lazySession->get('baz', 'default')); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function testRemove() : void |
||
| 103 | { |
||
| 104 | $this->wrappedSessionWillBeLoaded(); |
||
| 105 | |||
| 106 | $this->wrappedSession->expects(self::exactly(2))->method('remove')->with(self::logicalOr('foo', 'bar')); |
||
| 107 | |||
| 108 | $this->lazySession->remove('foo'); |
||
| 109 | $this->lazySession->remove('bar'); |
||
| 110 | } |
||
| 111 | |||
| 112 | public function testClear() : void |
||
| 113 | { |
||
| 114 | $this->wrappedSessionWillBeLoaded(); |
||
| 115 | |||
| 116 | $this->wrappedSession->expects(self::exactly(2))->method('clear'); |
||
| 117 | |||
| 118 | $this->lazySession->clear(); |
||
| 119 | $this->lazySession->clear(); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function testSet() : void |
||
| 123 | { |
||
| 124 | $this->wrappedSessionWillBeLoaded(); |
||
| 125 | |||
| 126 | $this |
||
| 127 | ->wrappedSession |
||
| 128 | ->expects(self::exactly(2)) |
||
| 129 | ->method('set') |
||
| 130 | ->with(self::logicalOr('foo', 'baz'), self::logicalOr('bar', 'tab')); |
||
| 131 | |||
| 132 | $this->lazySession->set('foo', 'bar'); |
||
| 133 | $this->lazySession->set('baz', 'tab'); |
||
| 134 | } |
||
| 135 | |||
| 136 | public function testIsEmpty() : void |
||
| 137 | { |
||
| 138 | $this->wrappedSessionWillBeLoaded(); |
||
| 139 | |||
| 140 | $this->wrappedSession->expects(self::at(0))->method('isEmpty')->willReturn(true); |
||
| 141 | $this->wrappedSession->expects(self::at(1))->method('isEmpty')->willReturn(false); |
||
| 142 | |||
| 143 | self::assertTrue($this->lazySession->isEmpty()); |
||
| 144 | self::assertFalse($this->lazySession->isEmpty()); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function testJsonSerialize() : void |
||
| 148 | { |
||
| 149 | $this->wrappedSessionWillBeLoaded(); |
||
| 150 | |||
| 151 | $this->wrappedSession->expects(self::at(0))->method('jsonSerialize')->willReturn((object) ['foo' => 'bar']); |
||
| 152 | $this->wrappedSession->expects(self::at(1))->method('jsonSerialize')->willReturn((object) ['baz' => 'tab']); |
||
| 153 | |||
| 154 | self::assertEquals((object) ['foo' => 'bar'], $this->lazySession->jsonSerialize()); |
||
| 155 | self::assertEquals((object) ['baz' => 'tab'], $this->lazySession->jsonSerialize()); |
||
| 156 | } |
||
| 157 | |||
| 158 | private function wrappedSessionWillBeLoaded() : void |
||
| 159 | { |
||
| 160 | $this->sessionLoader->expects(self::once())->method('__invoke')->willReturn($this->wrappedSession); |
||
| 161 | } |
||
| 162 | |||
| 163 | private function forceWrappedSessionInitialization() : void |
||
| 164 | { |
||
| 165 | // no-op operation that is known to trigger session lazy-loading |
||
| 166 | $this->lazySession->remove(uniqid('nonExisting', true)); |
||
| 167 | } |
||
| 168 | } |
||
| 169 |