Skip to content

Commit 7ac637f

Browse files
author
Yani
committed
add ArrayAccess tests
1 parent 1e4f15d commit 7ac637f

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

features/access.feature

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@ Feature: Session access
22
Scenario: Check data does not exist
33
When data does not exist
44
Then property check returns false
5+
And array access check returns false
56
Scenario: Check data exist
67
When data exists
78
Then property check returns true
9+
And array access check returns true
810
Scenario: Read data that exists
911
When data exists
1012
Then property read returns data
13+
And array access read returns data
1114
Scenario: Read data that does not exist
1215
When data does not exist
1316
Then property read triggers error
1417
And property read returns null
18+
And array access read triggers error
19+
And array access read returns null
1520
Scenario: Null coalesce when data does not exist
1621
When data does not exist
1722
Then property read with null coalesce returns null
18-
Scenario: Write data
23+
And array access read with null coalesce returns null
24+
Scenario: Write property data
1925
When data does not exist
2026
Then property write succeeds
27+
Scenario: Write array access data
28+
When data does not exist
29+
Then array access write succeeds

tests/behavior/AccessContext.php

+68
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,72 @@ public function propertyWriteSucceeds(): void
101101
Assert::assertCount(1, $this->session);
102102
Assert::assertTrue(isset($this->session->bar));
103103
}
104+
105+
/**
106+
* @Then array access check returns true
107+
*/
108+
public function arrayAccessCheckReturnsTrue(): void
109+
{
110+
Assert::assertTrue(isset($this->session['foo']));
111+
}
112+
113+
/**
114+
* @Then array access check returns false
115+
*/
116+
public function arrayAccessCheckReturnsFalse(): void
117+
{
118+
Assert::assertFalse(isset($this->session['foo']));
119+
}
120+
121+
/**
122+
* @Then array access read returns data
123+
*/
124+
public function arrayAccessReadReturnsData(): void
125+
{
126+
Assert::assertEquals('bar', $this->session['foo']);
127+
}
128+
129+
/**
130+
* @Then array access read triggers error
131+
*/
132+
public function arrayAccessReadTriggersNoticeError(): void
133+
{
134+
try {
135+
$errorThrown = false;
136+
$bar = $this->session['bar'];
137+
// @phpstan-ignore-next-line
138+
} catch (Throwable $e) {
139+
$errorThrown = true;
140+
} finally {
141+
Assert::assertTrue($errorThrown);
142+
}
143+
}
144+
145+
/**
146+
* @Then array access read returns null
147+
*/
148+
public function arrayAccessReadReturnsNull(): void
149+
{
150+
$bar = @$this->session['foo'];
151+
Assert::assertSame(null, $bar);
152+
}
153+
154+
/**
155+
* @Then array access read with null coalesce returns null
156+
*/
157+
public function arrayAccessReadWithNullCoalesceReturnsNull(): void
158+
{
159+
$bar = $this->session['foo'] ?? null;
160+
Assert::assertSame(null, $bar);
161+
}
162+
163+
/**
164+
* @Then array access write succeeds
165+
*/
166+
public function arrayAccessWriteSucceeds(): void
167+
{
168+
$this->session['bar'] = 'baz';
169+
Assert::assertCount(1, $this->session);
170+
Assert::assertTrue(isset($this->session['bar']));
171+
}
104172
}

0 commit comments

Comments
 (0)