Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 80fea20

Browse files
author
Jonathon Hill
committed
fix: Session::__get() should trigger error when data does not exist
1 parent 0fe1c63 commit 80fea20

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

‎behat.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
default:
22
suites:
3+
access:
4+
paths:
5+
- 'features/access.feature'
6+
contexts:
7+
- 'Compwright\PhpSession\BehaviorTest\AccessContext'
38
concurrency:
49
paths:
510
- 'features/concurrency.feature'

‎features/access.feature

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Feature: Session access
2+
Scenario: Check data does not exist
3+
When data does not exist
4+
Then property check returns false
5+
Scenario: Check data exist
6+
When data exists
7+
Then property check returns true
8+
Scenario: Read data that exists
9+
When data exists
10+
Then property read returns data
11+
Scenario: Read data that does not exist
12+
When data does not exist
13+
Then property read triggers error
14+
And property read returns null
15+
Scenario: Null coalesce when data does not exist
16+
When data does not exist
17+
Then property read with null coalesce returns null
18+
Scenario: Write data
19+
When data does not exist
20+
Then property write succeeds

‎src/Session.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public function __get(string $name)
5050
throw new RuntimeException('Session not initialized');
5151
}
5252

53-
return $this->contents[$name] ?? null;
53+
// @phpstan-ignore-next-line
54+
return $this->contents[$name];
5455
}
5556

5657
public function __isset(string $name): bool

‎tests/behavior/AccessContext.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /