Skip to content

Commit

Permalink
minimize test code that triggers exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Feb 24, 2023
1 parent 3b1d539 commit d09f38e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 61 deletions.
18 changes: 10 additions & 8 deletions tests/DateScalarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ abstract class DateScalarTest extends TestCase
*/
public function testThrowsIfSerializingInvalidDates($value): void
{
$this->expectException(InvariantViolation::class);
$dateScalar = $this->scalarInstance();

$this->scalarInstance()->serialize($value);
$this->expectException(InvariantViolation::class);
$dateScalar->serialize($value);
}

/**
Expand All @@ -31,9 +32,10 @@ public function testThrowsIfSerializingInvalidDates($value): void
*/
public function testThrowsIfParseValueInvalidDate($value): void
{
$this->expectException(Error::class);
$dateScalar = $this->scalarInstance();

$this->scalarInstance()->parseValue($value);
$this->expectException(Error::class);
$dateScalar->parseValue($value);
}

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

public function testThrowsIfParseLiteralNonString(): void
{
$this->expectException(Error::class);
$dateScalar = $this->scalarInstance();
$intValueNode = new IntValueNode([]);

$this->scalarInstance()->parseLiteral(
new IntValueNode([])
);
$this->expectException(Error::class);
$dateScalar->parseLiteral($intValueNode);
}

public function testSerializesDateTimeInterfaceInstance(): void
Expand Down
30 changes: 14 additions & 16 deletions tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@ final class EmailTest extends TestCase
{
public function testSerializeThrowsIfUnserializableValueIsGiven(): void
{
$this->expectExceptionObject(new InvariantViolation(
'The given value can not be coerced to a string: object.'
));
$email = new Email();
$object = new class() {};

(new Email())->serialize(
new class() {
}
);
$this->expectExceptionObject(new InvariantViolation('The given value can not be coerced to a string: object.'));
$email->serialize($object);
}

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

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

public function testSerializePassesWhenEmailIsValid(): void
Expand All @@ -39,10 +36,10 @@ public function testSerializePassesWhenEmailIsValid(): void

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

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

public function testParseValuePassesIfEmailIsValid(): void
Expand All @@ -55,10 +52,11 @@ public function testParseValuePassesIfEmailIsValid(): void

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

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

public function testParseLiteralPassesIfEmailIsValid(): void
Expand Down
21 changes: 13 additions & 8 deletions tests/JSONTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ final class JSONTest extends TestCase

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

(new JSON())->serialize(self::INVALID_UTF8_SEQUENCE);
$this->expectException(JsonException::class);
$json->serialize(self::INVALID_UTF8_SEQUENCE);
}

public function testSerializeThrowsIfJSONIsInvalid(): void
{
$this->expectException(JsonException::class);
$json = new JSON();

(new JSON())->serialize(self::INVALID_UTF8_SEQUENCE);
$this->expectException(JsonException::class);
$json->serialize(self::INVALID_UTF8_SEQUENCE);
}

public function testSerializePassesWhenJSONIsValid(): void
Expand All @@ -35,9 +37,10 @@ public function testSerializePassesWhenJSONIsValid(): void

public function testParseValueThrowsIfJSONIsInvalid(): void
{
$this->expectException(Error::class);
$json = new JSON();

(new JSON())->parseValue('foo');
$this->expectException(Error::class);
$json->parseValue('foo');
}

public function testParseValuePassesIfJSONIsValid(): void
Expand All @@ -50,9 +53,11 @@ public function testParseValuePassesIfJSONIsValid(): void

public function testParseLiteralThrowsIfNotValidJSON(): void
{
$this->expectException(Error::class);
$json = new JSON();
$stringValueNode = new StringValueNode(['value' => 'foo']);

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

public function testParseLiteralPassesIfJSONIsValid(): void
Expand Down
25 changes: 12 additions & 13 deletions tests/RegexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@ public function testCreateNamedRegexClass(Regex $regex): void
*/
public function testSerializeThrowsIfUnserializableValueIsGiven(Regex $regex): void
{
$this->expectException(InvariantViolation::class);
$object = new class() {};

$regex->serialize(
new class() {
}
);
$this->expectException(InvariantViolation::class);
$regex->serialize($object);
}

/**
Expand Down Expand Up @@ -115,11 +113,11 @@ public function __toString(): string
*/
public function testParseValueThrowsIfValueCantBeString(Regex $regex): void
{
$object = new class() {};

$this->expectException(Error::class);
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/can not be coerced to a string/');

$regex->parseValue(new class() {
});
$regex->parseValue($object);
}

/**
Expand All @@ -129,7 +127,6 @@ public function testParseValueThrowsIfValueDoesNotMatch(Regex $regex): void
{
$this->expectException(Error::class);
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/did not match the regex/');

$regex->parseValue('');
}

Expand All @@ -149,21 +146,23 @@ public function testParseValuePassesOnMatch(Regex $regex): void
*/
public function testParseLiteralThrowsIfNotString(Regex $regex): void
{
$intValueNode = new IntValueNode([]);

$this->expectException(Error::class);
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/' . NodeKind::INT . '/');

$regex->parseLiteral(new IntValueNode([]));
$regex->parseLiteral($intValueNode);
}

/**
* @dataProvider regexClassProvider
*/
public function testParseLiteralThrowsIfValueDoesNotMatch(Regex $regex): void
{
$stringValueNode = new StringValueNode(['value' => 'asdf']);

$this->expectException(Error::class);
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/did not match the regex/');

$regex->parseLiteral(new StringValueNode(['value' => 'asdf']));
$regex->parseLiteral($stringValueNode);
}

/**
Expand Down
29 changes: 13 additions & 16 deletions tests/StringScalarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,18 @@ public function testCreateNamedStringScalarClass(StringScalar $stringScalar): vo
*/
public function testSerializeThrowsIfUnserializableValueIsGiven(StringScalar $stringScalar): void
{
$this->expectException(InvariantViolation::class);
$object = new class() {};

$stringScalar->serialize(
new class() {
}
);
$this->expectException(InvariantViolation::class);
$stringScalar->serialize($object);
}

/**
* @dataProvider stringClassProvider
*/
public function testSerializeThrowsIfStringScalarIsNotValid(StringScalar $stringScalar): void
{
$this->expectException(InvariantViolation::class);
$this->expectExceptionMessage('The given string "bar" is not a valid MyStringScalar.');

$this->expectExceptionObject(new InvariantViolation('The given string "bar" is not a valid MyStringScalar.'));
$stringScalar->serialize('bar');
}

Expand Down Expand Up @@ -120,11 +116,11 @@ public function __toString(): string
*/
public function testParseValueThrowsIfValueCantBeString(StringScalar $stringScalar): void
{
$object = new class() {};

$this->expectException(Error::class);
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/can not be coerced to a string/');

$stringScalar->parseValue(new class() {
});
$stringScalar->parseValue($object);
}

/**
Expand Down Expand Up @@ -154,21 +150,22 @@ public function testParseValuePassesOnMatch(StringScalar $stringScalar): void
*/
public function testParseLiteralThrowsIfNotString(StringScalar $stringScalar): void
{
$intValueNode = new IntValueNode([]);

$this->expectException(Error::class);
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/' . NodeKind::INT . '/');

$stringScalar->parseLiteral(new IntValueNode([]));
$stringScalar->parseLiteral($intValueNode);
}

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

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

/**
Expand Down

0 comments on commit d09f38e

Please sign in to comment.