|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Compwright\PhpSession\BehaviorTest; |
| 4 | + |
| 5 | +use Behat\Behat\Context\Context; |
| 6 | +use Compwright\PhpSession\Session; |
| 7 | +use PHPUnit\Framework\Assert; |
| 8 | +use Throwable; |
| 9 | + |
| 10 | +/** |
| 11 | + * Defines application features from the specific context. |
| 12 | + */ |
| 13 | +class AccessContext implements Context |
| 14 | +{ |
| 15 | + private Session $session; |
| 16 | + |
| 17 | + /** |
| 18 | + * @When data does not exist |
| 19 | + */ |
| 20 | + public function dataDoesNotExist(): void |
| 21 | + { |
| 22 | + $this->session = new Session('foo', 'bar', []); |
| 23 | + Assert::assertTrue($this->session->isWriteable()); |
| 24 | + Assert::assertCount(0, $this->session); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @When data exists |
| 29 | + */ |
| 30 | + public function dataExists(): void |
| 31 | + { |
| 32 | + $this->session = new Session('foo', 'bar', ['foo' => 'bar']); |
| 33 | + Assert::assertTrue($this->session->isWriteable()); |
| 34 | + Assert::assertCount(1, $this->session); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @Then property check returns false |
| 39 | + */ |
| 40 | + public function propertyCheckReturnsFalse(): void |
| 41 | + { |
| 42 | + Assert::assertFalse(isset($this->session->foo)); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @Then property check returns true |
| 47 | + */ |
| 48 | + public function propertyCheckReturnsTrue(): void |
| 49 | + { |
| 50 | + Assert::assertTrue(isset($this->session->foo)); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @Then property read returns data |
| 55 | + */ |
| 56 | + public function propertyReadReturnsData(): void |
| 57 | + { |
| 58 | + Assert::assertEquals('bar', $this->session->foo); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @Then property read triggers error |
| 63 | + */ |
| 64 | + public function propertyReadTriggersNoticeError(): void |
| 65 | + { |
| 66 | + try { |
| 67 | + $errorThrown = false; |
| 68 | + $bar = $this->session->bar; |
| 69 | + // @phpstan-ignore-next-line |
| 70 | + } catch (Throwable $e) { |
| 71 | + $errorThrown = true; |
| 72 | + } finally { |
| 73 | + Assert::assertTrue($errorThrown); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @Then property read returns null |
| 79 | + */ |
| 80 | + public function propertyReadReturnsNull(): void |
| 81 | + { |
| 82 | + $bar = @$this->session->bar; |
| 83 | + Assert::assertSame(null, $bar); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @Then property read with null coalesce returns null |
| 88 | + */ |
| 89 | + public function propertyReadWithNullCoalesceReturnsNull(): void |
| 90 | + { |
| 91 | + $bar = $this->session->bar ?? null; |
| 92 | + Assert::assertSame(null, $bar); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @Then property write succeeds |
| 97 | + */ |
| 98 | + public function propertyWriteSucceeds(): void |
| 99 | + { |
| 100 | + $this->session->bar = 'baz'; |
| 101 | + Assert::assertCount(1, $this->session); |
| 102 | + Assert::assertTrue(isset($this->session->bar)); |
| 103 | + } |
| 104 | +} |
0 commit comments