Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Responses/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ class Filter
{
private string $field;
private int|float|bool|string|null $value;
private string $operator;

public function __construct(array $data)
{
try {
$this->field = $data['field'];
$this->value = $data['value'];
$this->operator = $data['operator'];
// @phpstan-ignore-next-line PHPStan is confused
} catch (Throwable $e) {
throw new ClientException('Unrecognized response: ' . $e->getMessage(), 0, $e);
Expand All @@ -32,4 +34,9 @@ public function getValue(): int|float|bool|string|null
{
return $this->value;
}

public function getOperator(): string
{
return $this->operator;
}
}
32 changes: 30 additions & 2 deletions tests/Responses/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ class FilterTest extends TestCase
{
public function testAccessors(): void
{
$filter = new Filter(['field' => 'foo', 'value' => 'bar']);
$filter = new Filter(['field' => 'foo', 'value' => 'bar', 'operator' => '==']);
self::assertSame('foo', $filter->getField());
self::assertSame('bar', $filter->getValue());
self::assertSame('==', $filter->getOperator());
}

/**
* @dataProvider provideScalarValues
*/
public function testScalarValues(int|float|bool|string|null $value): void
{
$filter = new Filter(['field' => 'foo', 'value' => $value]);
$filter = new Filter(['field' => 'foo', 'value' => $value, 'operator' => '==']);
self::assertSame('foo', $filter->getField());
self::assertSame($value, $filter->getValue());
self::assertSame('==', $filter->getOperator());
}

public function provideScalarValues(): iterable
Expand All @@ -36,6 +38,24 @@ public function provideScalarValues(): iterable
yield 'null' => [null];
}

/**
* @dataProvider provideOperators
*/
public function testOperators(string $operator): void
{
$filter = new Filter(['field' => 'foo', 'value' => 'bar', 'operator' => $operator]);
self::assertSame($operator, $filter->getOperator());
}

public function provideOperators(): iterable
{
yield 'equal' => ['=='];
yield 'less than' => ['<'];
yield 'greater than' => ['>'];
yield 'less than or equal' => ['<='];
yield 'greater than or equal' => ['>='];
}

public function testInvalidData(): void
{
$this->expectException(ClientException::class);
Expand All @@ -45,4 +65,12 @@ public function testInvalidData(): void
$this->expectExceptionCode(0);
new Filter(['boo', 'field' => 1]);
}

public function testMissingOperator(): void
{
$this->expectException(ClientException::class);
$this->expectExceptionMessageMatches('#Unrecognized response:.*?operator#');
$this->expectExceptionCode(0);
new Filter(['field' => 'foo', 'value' => 'bar']);
}
}
2 changes: 2 additions & 0 deletions tests/Responses/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function testEmailRecipientAccessors(): void
2 => [
'field' => 'bar',
'value' => 'Kochba',
'operator' => '==',
],
],
];
Expand All @@ -34,6 +35,7 @@ public function testEmailRecipientAccessors(): void
self::assertCount(1, $subscription->getFilters());
self::assertSame('bar', $subscription->getFilters()[0]->getField());
self::assertSame('Kochba', $subscription->getFilters()[0]->getValue());
self::assertSame('==', $subscription->getFilters()[0]->getOperator());

$recipient = $subscription->getRecipient();
self::assertInstanceOf(EmailRecipient::class, $recipient);
Expand Down
6 changes: 4 additions & 2 deletions tests/SubscriptionClientFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function testGetSubscriptionHeaders(): void
'id' => 'subscription-123',
'event' => 'job-failed',
'filters' => [
['field' => 'project.id', 'value' => '123'],
['field' => 'project.id', 'value' => '123', 'operator' => '=='],
],
'recipient' => ['channel' => 'email', 'address' => 'a@example.com'],
]),
Expand Down Expand Up @@ -190,6 +190,7 @@ public function testGetSubscriptionHeaders(): void
self::assertSame('job-failed', $result->getEvent());
self::assertSame('project.id', $result->getFilters()[0]->getField());
self::assertSame('123', $result->getFilters()[0]->getValue());
self::assertSame('==', $result->getFilters()[0]->getOperator());
$recipient = $result->getRecipient();
self::assertInstanceOf(ResponseEmailRecipient::class, $recipient);
self::assertSame('email', $recipient->getChannel());
Expand All @@ -203,7 +204,7 @@ public function testListSubscriptionsHeaders(): void
'id' => 'sub-1',
'event' => 'job-failed',
'filters' => [
['field' => 'project.id', 'value' => '123'],
['field' => 'project.id', 'value' => '123', 'operator' => '=='],
],
'recipient' => ['channel' => 'email', 'address' => 'a@example.com'],
],
Expand Down Expand Up @@ -248,6 +249,7 @@ public function testListSubscriptionsHeaders(): void
self::assertSame('job-failed', $result[0]->getEvent());
self::assertSame('project.id', $result[0]->getFilters()[0]->getField());
self::assertSame('123', $result[0]->getFilters()[0]->getValue());
self::assertSame('==', $result[0]->getFilters()[0]->getOperator());
$recipient = $result[0]->getRecipient();
self::assertInstanceOf(ResponseEmailRecipient::class, $recipient);
self::assertSame('email', $recipient->getChannel());
Expand Down