Skip to content

Commit d09f38e

Browse files
committed
minimize test code that triggers exceptions
1 parent 3b1d539 commit d09f38e

File tree

5 files changed

+62
-61
lines changed

5 files changed

+62
-61
lines changed

tests/DateScalarTest.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ abstract class DateScalarTest extends TestCase
1919
*/
2020
public function testThrowsIfSerializingInvalidDates($value): void
2121
{
22-
$this->expectException(InvariantViolation::class);
22+
$dateScalar = $this->scalarInstance();
2323

24-
$this->scalarInstance()->serialize($value);
24+
$this->expectException(InvariantViolation::class);
25+
$dateScalar->serialize($value);
2526
}
2627

2728
/**
@@ -31,9 +32,10 @@ public function testThrowsIfSerializingInvalidDates($value): void
3132
*/
3233
public function testThrowsIfParseValueInvalidDate($value): void
3334
{
34-
$this->expectException(Error::class);
35+
$dateScalar = $this->scalarInstance();
3536

36-
$this->scalarInstance()->parseValue($value);
37+
$this->expectException(Error::class);
38+
$dateScalar->parseValue($value);
3739
}
3840

3941
/**
@@ -74,11 +76,11 @@ public function testParsesLiteral(string $value, string $expected): void
7476

7577
public function testThrowsIfParseLiteralNonString(): void
7678
{
77-
$this->expectException(Error::class);
79+
$dateScalar = $this->scalarInstance();
80+
$intValueNode = new IntValueNode([]);
7881

79-
$this->scalarInstance()->parseLiteral(
80-
new IntValueNode([])
81-
);
82+
$this->expectException(Error::class);
83+
$dateScalar->parseLiteral($intValueNode);
8284
}
8385

8486
public function testSerializesDateTimeInterfaceInstance(): void

tests/EmailTest.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,19 @@ final class EmailTest extends TestCase
1212
{
1313
public function testSerializeThrowsIfUnserializableValueIsGiven(): void
1414
{
15-
$this->expectExceptionObject(new InvariantViolation(
16-
'The given value can not be coerced to a string: object.'
17-
));
15+
$email = new Email();
16+
$object = new class() {};
1817

19-
(new Email())->serialize(
20-
new class() {
21-
}
22-
);
18+
$this->expectExceptionObject(new InvariantViolation('The given value can not be coerced to a string: object.'));
19+
$email->serialize($object);
2320
}
2421

2522
public function testSerializeThrowsIfEmailIsInvalid(): void
2623
{
27-
$this->expectException(InvariantViolation::class);
28-
$this->expectExceptionMessage('The given string "foo" is not a valid Email.');
24+
$email = new Email();
2925

30-
(new Email())->serialize('foo');
26+
$this->expectExceptionObject(new InvariantViolation('The given string "foo" is not a valid Email.'));
27+
$email->serialize('foo');
3128
}
3229

3330
public function testSerializePassesWhenEmailIsValid(): void
@@ -39,10 +36,10 @@ public function testSerializePassesWhenEmailIsValid(): void
3936

4037
public function testParseValueThrowsIfEmailIsInvalid(): void
4138
{
42-
$this->expectException(Error::class);
43-
$this->expectExceptionMessage('The given string "foo" is not a valid Email.');
39+
$email = new Email();
4440

45-
(new Email())->parseValue('foo');
41+
$this->expectExceptionObject(new Error('The given string "foo" is not a valid Email.'));
42+
$email->parseValue('foo');
4643
}
4744

4845
public function testParseValuePassesIfEmailIsValid(): void
@@ -55,10 +52,11 @@ public function testParseValuePassesIfEmailIsValid(): void
5552

5653
public function testParseLiteralThrowsIfNotValidEmail(): void
5754
{
58-
$this->expectException(Error::class);
59-
$this->expectExceptionMessage('The given string "foo" is not a valid Email.');
55+
$email = new Email();
56+
$stringValueNode = new StringValueNode(['value' => 'foo']);
6057

61-
(new Email())->parseLiteral(new StringValueNode(['value' => 'foo']));
58+
$this->expectExceptionObject(new Error('The given string "foo" is not a valid Email.'));
59+
$email->parseLiteral($stringValueNode);
6260
}
6361

6462
public function testParseLiteralPassesIfEmailIsValid(): void

tests/JSONTest.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ final class JSONTest extends TestCase
1414

1515
public function testSerializeThrowsIfNonEncodableValueIsGiven(): void
1616
{
17-
$this->expectException(JsonException::class);
17+
$json = new JSON();
1818

19-
(new JSON())->serialize(self::INVALID_UTF8_SEQUENCE);
19+
$this->expectException(JsonException::class);
20+
$json->serialize(self::INVALID_UTF8_SEQUENCE);
2021
}
2122

2223
public function testSerializeThrowsIfJSONIsInvalid(): void
2324
{
24-
$this->expectException(JsonException::class);
25+
$json = new JSON();
2526

26-
(new JSON())->serialize(self::INVALID_UTF8_SEQUENCE);
27+
$this->expectException(JsonException::class);
28+
$json->serialize(self::INVALID_UTF8_SEQUENCE);
2729
}
2830

2931
public function testSerializePassesWhenJSONIsValid(): void
@@ -35,9 +37,10 @@ public function testSerializePassesWhenJSONIsValid(): void
3537

3638
public function testParseValueThrowsIfJSONIsInvalid(): void
3739
{
38-
$this->expectException(Error::class);
40+
$json = new JSON();
3941

40-
(new JSON())->parseValue('foo');
42+
$this->expectException(Error::class);
43+
$json->parseValue('foo');
4144
}
4245

4346
public function testParseValuePassesIfJSONIsValid(): void
@@ -50,9 +53,11 @@ public function testParseValuePassesIfJSONIsValid(): void
5053

5154
public function testParseLiteralThrowsIfNotValidJSON(): void
5255
{
53-
$this->expectException(Error::class);
56+
$json = new JSON();
57+
$stringValueNode = new StringValueNode(['value' => 'foo']);
5458

55-
(new JSON())->parseLiteral(new StringValueNode(['value' => 'foo']));
59+
$this->expectException(Error::class);
60+
$json->parseLiteral($stringValueNode);
5661
}
5762

5863
public function testParseLiteralPassesIfJSONIsValid(): void

tests/RegexTest.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,10 @@ public function testCreateNamedRegexClass(Regex $regex): void
6363
*/
6464
public function testSerializeThrowsIfUnserializableValueIsGiven(Regex $regex): void
6565
{
66-
$this->expectException(InvariantViolation::class);
66+
$object = new class() {};
6767

68-
$regex->serialize(
69-
new class() {
70-
}
71-
);
68+
$this->expectException(InvariantViolation::class);
69+
$regex->serialize($object);
7270
}
7371

7472
/**
@@ -115,11 +113,11 @@ public function __toString(): string
115113
*/
116114
public function testParseValueThrowsIfValueCantBeString(Regex $regex): void
117115
{
116+
$object = new class() {};
117+
118118
$this->expectException(Error::class);
119119
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/can not be coerced to a string/');
120-
121-
$regex->parseValue(new class() {
122-
});
120+
$regex->parseValue($object);
123121
}
124122

125123
/**
@@ -129,7 +127,6 @@ public function testParseValueThrowsIfValueDoesNotMatch(Regex $regex): void
129127
{
130128
$this->expectException(Error::class);
131129
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/did not match the regex/');
132-
133130
$regex->parseValue('');
134131
}
135132

@@ -149,21 +146,23 @@ public function testParseValuePassesOnMatch(Regex $regex): void
149146
*/
150147
public function testParseLiteralThrowsIfNotString(Regex $regex): void
151148
{
149+
$intValueNode = new IntValueNode([]);
150+
152151
$this->expectException(Error::class);
153152
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/' . NodeKind::INT . '/');
154-
155-
$regex->parseLiteral(new IntValueNode([]));
153+
$regex->parseLiteral($intValueNode);
156154
}
157155

158156
/**
159157
* @dataProvider regexClassProvider
160158
*/
161159
public function testParseLiteralThrowsIfValueDoesNotMatch(Regex $regex): void
162160
{
161+
$stringValueNode = new StringValueNode(['value' => 'asdf']);
162+
163163
$this->expectException(Error::class);
164164
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/did not match the regex/');
165-
166-
$regex->parseLiteral(new StringValueNode(['value' => 'asdf']));
165+
$regex->parseLiteral($stringValueNode);
167166
}
168167

169168
/**

tests/StringScalarTest.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,18 @@ public function testCreateNamedStringScalarClass(StringScalar $stringScalar): vo
6969
*/
7070
public function testSerializeThrowsIfUnserializableValueIsGiven(StringScalar $stringScalar): void
7171
{
72-
$this->expectException(InvariantViolation::class);
72+
$object = new class() {};
7373

74-
$stringScalar->serialize(
75-
new class() {
76-
}
77-
);
74+
$this->expectException(InvariantViolation::class);
75+
$stringScalar->serialize($object);
7876
}
7977

8078
/**
8179
* @dataProvider stringClassProvider
8280
*/
8381
public function testSerializeThrowsIfStringScalarIsNotValid(StringScalar $stringScalar): void
8482
{
85-
$this->expectException(InvariantViolation::class);
86-
$this->expectExceptionMessage('The given string "bar" is not a valid MyStringScalar.');
87-
83+
$this->expectExceptionObject(new InvariantViolation('The given string "bar" is not a valid MyStringScalar.'));
8884
$stringScalar->serialize('bar');
8985
}
9086

@@ -120,11 +116,11 @@ public function __toString(): string
120116
*/
121117
public function testParseValueThrowsIfValueCantBeString(StringScalar $stringScalar): void
122118
{
119+
$object = new class() {};
120+
123121
$this->expectException(Error::class);
124122
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/can not be coerced to a string/');
125-
126-
$stringScalar->parseValue(new class() {
127-
});
123+
$stringScalar->parseValue($object);
128124
}
129125

130126
/**
@@ -154,21 +150,22 @@ public function testParseValuePassesOnMatch(StringScalar $stringScalar): void
154150
*/
155151
public function testParseLiteralThrowsIfNotString(StringScalar $stringScalar): void
156152
{
153+
$intValueNode = new IntValueNode([]);
154+
157155
$this->expectException(Error::class);
158156
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/' . NodeKind::INT . '/');
159-
160-
$stringScalar->parseLiteral(new IntValueNode([]));
157+
$stringScalar->parseLiteral($intValueNode);
161158
}
162159

163160
/**
164161
* @dataProvider stringClassProvider
165162
*/
166163
public function testParseLiteralThrowsIfValueDoesNotMatch(StringScalar $stringScalar): void
167164
{
168-
$this->expectException(Error::class);
169-
$this->expectExceptionMessage('The given string "bar" is not a valid MyStringScalar.');
165+
$stringValueNode = new StringValueNode(['value' => 'bar']);
170166

171-
$stringScalar->parseLiteral(new StringValueNode(['value' => 'bar']));
167+
$this->expectExceptionObject(new Error('The given string "bar" is not a valid MyStringScalar.'));
168+
$stringScalar->parseLiteral($stringValueNode);
172169
}
173170

174171
/**

0 commit comments

Comments
 (0)