Skip to content

Commit 9b35831

Browse files
committed
test: Fix test cases according to AttributeException
1 parent 4ad2eb5 commit 9b35831

20 files changed

+74
-52
lines changed

src/Datafile/Conditions/AfterCondition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public function __construct(string $attribute, DateTimeImmutable $value)
2424

2525
public function isSatisfiedBy(array $context): bool
2626
{
27+
$valueFromContext = $this->getValueFromContext($context, $this->attribute);
2728
try {
28-
$valueFromContext = $this->getValueFromContext($context, $this->attribute);
2929
if ($valueFromContext === null) {
3030
return false;
3131
}

src/Datafile/Conditions/BeforeCondition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public function __construct(string $attribute, DateTimeImmutable $value)
2323

2424
public function isSatisfiedBy(array $context): bool
2525
{
26+
$valueFromContext = $this->getValueFromContext($context, $this->attribute);
2627
try {
27-
$valueFromContext = $this->getValueFromContext($context, $this->attribute);
2828
if ($valueFromContext === null) {
2929
return false;
3030
}

src/Datafile/Conditions/NotCondition.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ public function __construct(ConditionInterface $specification)
1818

1919
public function isSatisfiedBy(array $context): bool
2020
{
21-
try {
22-
return $this->specification->isSatisfiedBy($context) === false;
23-
} catch (AttributeException $e) {
24-
return false;
25-
}
21+
return $this->specification->isSatisfiedBy($context) === false;
2622
}
2723
}

tests/Datafile/Conditions/AfterConditionTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Featurevisor\Tests\Datafile\Conditions;
66

77
use DateTimeImmutable;
8+
use Featurevisor\Datafile\AttributeException;
89
use Featurevisor\Datafile\Conditions\AfterCondition;
910
use PHPUnit\Framework\TestCase;
1011

@@ -75,13 +76,14 @@ public function testAfterConditionWithNestedAttributeBefore(): void
7576

7677
public function testAfterConditionWithMissingAttribute(): void
7778
{
79+
$this->expectException(AttributeException::class);
80+
7881
$context = [
7982
'other_attribute' => '2023-01-15',
8083
];
81-
8284
$condition = new AfterCondition('date', new DateTimeImmutable('2023-01-01'));
8385

84-
self::assertFalse($condition->isSatisfiedBy($context));
86+
$condition->isSatisfiedBy($context);
8587
}
8688

8789
public function testAfterConditionWithInvalidDateFormat(): void

tests/Datafile/Conditions/BeforeConditionTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Featurevisor\Tests\Datafile\Conditions;
66

77
use DateTimeImmutable;
8+
use Featurevisor\Datafile\AttributeException;
89
use Featurevisor\Datafile\Conditions\BeforeCondition;
910
use PHPUnit\Framework\TestCase;
1011

@@ -75,13 +76,14 @@ public function testBeforeConditionWithNestedAttributeAfter(): void
7576

7677
public function testBeforeConditionWithMissingAttribute(): void
7778
{
79+
$this->expectException(AttributeException::class);
80+
7881
$context = [
7982
'other_attribute' => '2022-12-15',
8083
];
81-
8284
$condition = new BeforeCondition('date', new DateTimeImmutable('2023-01-01'));
8385

84-
self::assertFalse($condition->isSatisfiedBy($context));
86+
$condition->isSatisfiedBy($context);
8587
}
8688

8789
public function testBeforeConditionWithInvalidDateFormat(): void

tests/Datafile/Conditions/ContainsConditionTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Featurevisor\Tests\Datafile\Conditions;
66

7+
use Featurevisor\Datafile\AttributeException;
78
use Featurevisor\Datafile\Conditions\ContainsCondition;
89
use PHPUnit\Framework\TestCase;
910

@@ -33,13 +34,14 @@ public function testContainsConditionWithSimpleAttributeNotContaining(): void
3334

3435
public function testContainsConditionWithMissingAttribute(): void
3536
{
37+
$this->expectException(AttributeException::class);
38+
3639
$context = [
3740
'other_attribute' => 'iPhone 12',
3841
];
39-
4042
$condition = new ContainsCondition('device', 'iPhone');
4143

42-
self::assertFalse($condition->isSatisfiedBy($context));
44+
$condition->isSatisfiedBy($context);
4345
}
4446

4547
public function testContainsConditionWithNestedAttributeContaining(): void

tests/Datafile/Conditions/EndsWithConditionTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Featurevisor\Tests\Datafile\Conditions;
66

7+
use Featurevisor\Datafile\AttributeException;
78
use Featurevisor\Datafile\Conditions\EndsWithCondition;
89
use PHPUnit\Framework\TestCase;
910

@@ -33,13 +34,14 @@ public function testEndsWithConditionWithSimpleAttributeNotEndingWith(): void
3334

3435
public function testEndsWithConditionWithMissingAttribute(): void
3536
{
37+
$this->expectException(AttributeException::class);
38+
3639
$context = [
3740
'other_attribute' => 'My iPhone',
3841
];
39-
4042
$condition = new EndsWithCondition('device', 'iPhone');
4143

42-
self::assertFalse($condition->isSatisfiedBy($context));
44+
$condition->isSatisfiedBy($context);
4345
}
4446

4547
public function testEndsWithConditionWithNestedAttributeEndingWith(): void

tests/Datafile/Conditions/EqualsConditionTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Featurevisor\Tests\Datafile\Conditions;
66

7+
use Featurevisor\Datafile\AttributeException;
78
use Featurevisor\Datafile\Conditions\EqualsCondition;
89
use PHPUnit\Framework\TestCase;
910

@@ -33,13 +34,14 @@ public function testEqualsConditionWithSimpleAttributeNotMatching(): void
3334

3435
public function testEqualsConditionWithMissingAttribute(): void
3536
{
37+
$this->expectException(AttributeException::class);
38+
3639
$context = [
3740
'other_attribute' => 'value',
3841
];
39-
4042
$condition = new EqualsCondition('country', 'us');
4143

42-
self::assertFalse($condition->isSatisfiedBy($context));
44+
$condition->isSatisfiedBy($context);
4345
}
4446

4547
public function testEqualsConditionWithNestedAttributeMatching(): void
@@ -74,17 +76,18 @@ public function testEqualsConditionWithNestedAttributeNotMatching(): void
7476

7577
public function testEqualsConditionWithMissingNestedAttribute(): void
7678
{
79+
$this->expectException(AttributeException::class);
80+
7781
$context = [
7882
'user' => [
7983
'profile' => [
8084
'name' => 'John',
8185
],
8286
],
8387
];
84-
8588
$condition = new EqualsCondition('user.profile.country', 'us');
8689

87-
self::assertFalse($condition->isSatisfiedBy($context));
90+
$condition->isSatisfiedBy($context);
8891
}
8992

9093
public function testEqualsConditionWithDifferentTypes(): void

tests/Datafile/Conditions/GreaterThanConditionTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Featurevisor\Tests\Datafile\Conditions;
66

7+
use Featurevisor\Datafile\AttributeException;
78
use Featurevisor\Datafile\Conditions\GreaterThanCondition;
89
use PHPUnit\Framework\TestCase;
910

@@ -44,13 +45,14 @@ public function testGreaterThanConditionWithSimpleAttributeLess(): void
4445

4546
public function testGreaterThanConditionWithMissingAttribute(): void
4647
{
48+
$this->expectException(AttributeException::class);
49+
4750
$context = [
4851
'other_attribute' => 25,
4952
];
50-
5153
$condition = new GreaterThanCondition('age', 21);
5254

53-
self::assertFalse($condition->isSatisfiedBy($context));
55+
$condition->isSatisfiedBy($context);
5456
}
5557

5658
public function testGreaterThanConditionWithNestedAttributeGreater(): void

tests/Datafile/Conditions/InConditionTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Featurevisor\Tests\Datafile\Conditions;
66

7+
use Featurevisor\Datafile\AttributeException;
78
use Featurevisor\Datafile\Conditions\InCondition;
89
use Featurevisor\Datafile\Conditions\NotCondition;
910
use PHPUnit\Framework\TestCase;
@@ -34,13 +35,14 @@ public function testInConditionWithSimpleAttributeNotInArray(): void
3435

3536
public function testInConditionWithMissingAttribute(): void
3637
{
38+
$this->expectException(AttributeException::class);
39+
3740
$context = [
3841
'other_attribute' => 'us',
3942
];
40-
4143
$condition = new InCondition('country', ['us', 'ca', 'uk']);
4244

43-
self::assertFalse($condition->isSatisfiedBy($context));
45+
$condition->isSatisfiedBy($context);
4446
}
4547

4648
public function testInConditionWithNestedAttributeInArray(): void
@@ -112,12 +114,13 @@ public function testInConditionWithInvalidValueType(): void
112114

113115
public function testNotInConditionWithMissingAttribute(): void
114116
{
117+
$this->expectException(AttributeException::class);
118+
115119
$context = [
116120
'country' => 'us',
117121
];
118-
119122
$condition = new NotCondition(new InCondition('continent', ['europe']));
120123

121-
self::assertFalse($condition->isSatisfiedBy($context));
124+
$condition->isSatisfiedBy($context);
122125
}
123126
}

0 commit comments

Comments
 (0)