Skip to content

Commit 9968127

Browse files
committed
Better enforcement of FilterResponse properties
1 parent 938762f commit 9968127

2 files changed

Lines changed: 57 additions & 7 deletions

File tree

src/FilterResponse.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
namespace TraderInteractive;
44

5+
use InvalidArgumentException;
56
use TraderInteractive\Exceptions\ReadOnlyViolationException;
67

78
/**
89
* This object contains the various data returned by a filter action.
910
*
10-
* @property-read bool $success TRUE if the filter was successful or FALSE if errors were encountered.
11-
* @property-read mixed $filteredValue The input values after being filtered.
12-
* @property-read array $errors Any errors encountered during the filter process.
13-
* @property-read string|null $errorMessage An error message generated from the errors. NULL if no errors.
14-
* @property-read mixed $unknowns The values that were unknown during filtering.
11+
* @property bool $success TRUE if the filter was successful or FALSE if errors were encountered.
12+
* @property mixed $filteredValue The input values after being filtered.
13+
* @property array $errors Any errors encountered during the filter process.
14+
* @property string|null $errorMessage An error message generated from the errors. NULL if no errors.
15+
* @property mixed $unknowns The values that were unknown during filtering.
1516
*/
1617
final class FilterResponse
1718
{
@@ -42,12 +43,20 @@ public function __construct(
4243

4344
public function __get($name)
4445
{
45-
return $this->response[$name];
46+
if (array_key_exists($name, $this->response)) {
47+
return $this->response[$name];
48+
}
49+
50+
throw new InvalidArgumentException("Property '{$name}' does not exist");
4651
}
4752

4853
public function __set($name, $value)
4954
{
50-
throw new ReadOnlyViolationException("Property {$name} is read-only");
55+
if (array_key_exists($name, $this->response)) {
56+
throw new ReadOnlyViolationException("Property '{$name}' is read-only");
57+
}
58+
59+
throw new InvalidArgumentException("Property '{$name}' does not exist");
5160
}
5261

5362
/**

tests/FilterResponseTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace TraderInteractiveTest;
44

5+
use InvalidArgumentException;
56
use PHPUnit\Framework\TestCase;
7+
use TraderInteractive\Exceptions\ReadOnlyViolationException;
68
use TraderInteractive\FilterResponse;
79

810
/**
@@ -65,6 +67,45 @@ public function constructDefault()
6567
$this->assertSame([], $response->unknowns);
6668
}
6769

70+
/**
71+
* @test
72+
* @covers ::__construct
73+
*/
74+
public function gettingInvalidPropertyThrowsException()
75+
{
76+
$this->expectException(InvalidArgumentException::class);
77+
$this->expectExceptionMessage("Property 'foo' does not exist");
78+
79+
$response = new FilterResponse([]);
80+
$response->foo;
81+
}
82+
83+
/**
84+
* @test
85+
* @covers ::__construct
86+
*/
87+
public function settingValidPropertyThrowsAnException()
88+
{
89+
$this->expectException(ReadOnlyViolationException::class);
90+
$this->expectExceptionMessage("Property 'success' is read-only");
91+
92+
$response = new FilterResponse([]);
93+
$response->success = false;
94+
}
95+
96+
/**
97+
* @test
98+
* @covers ::__construct
99+
*/
100+
public function settingInvalidPropertyThrowsAnException()
101+
{
102+
$this->expectException(InvalidArgumentException::class);
103+
$this->expectExceptionMessage("Property 'foo' does not exist");
104+
105+
$response = new FilterResponse([]);
106+
$response->foo = false;
107+
}
108+
68109
/**
69110
* @test
70111
* @covers ::toArray

0 commit comments

Comments
 (0)