diff --git a/tests/Metadata/Driver/XmlDriverTest.php b/tests/Metadata/Driver/XmlDriverTest.php index 08397da44..7e3ba729f 100644 --- a/tests/Metadata/Driver/XmlDriverTest.php +++ b/tests/Metadata/Driver/XmlDriverTest.php @@ -4,6 +4,7 @@ namespace JMS\Serializer\Tests\Metadata\Driver; +use JMS\Serializer\Exception\InvalidMetadataException; use JMS\Serializer\Metadata\Driver\XmlDriver; use JMS\Serializer\Metadata\PropertyMetadata; use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; @@ -11,16 +12,16 @@ class XmlDriverTest extends BaseDriverTest { - /** - * @expectedException \JMS\Serializer\Exception\InvalidMetadataException - * @expectedExceptionMessage Invalid XML content for metadata - */ public function testInvalidXml() { $driver = $this->getDriver(); $ref = new \ReflectionMethod($driver, 'loadMetadataFromFile'); $ref->setAccessible(true); + + $this->expectException(InvalidMetadataException::class); + $this->expectExceptionMessage('Invalid XML content for metadata'); + $ref->invoke($driver, new \ReflectionClass('stdClass'), __DIR__ . '/xml/invalid.xml'); } @@ -76,7 +77,8 @@ public function testGroupsTrim() $first = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\GroupsTrim')); self::assertArrayHasKey('amount', $first->propertyMetadata); - self::assertArraySubset(['first.test.group', 'second.test.group'], $first->propertyMetadata['currency']->groups); + self::assertContains('first.test.group', $first->propertyMetadata['currency']->groups); + self::assertContains('second.test.group', $first->propertyMetadata['currency']->groups); } public function testMultilineGroups() @@ -84,7 +86,8 @@ public function testMultilineGroups() $first = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\MultilineGroupsFormat')); self::assertArrayHasKey('amount', $first->propertyMetadata); - self::assertArraySubset(['first.test.group', 'second.test.group'], $first->propertyMetadata['currency']->groups); + self::assertContains('first.test.group', $first->propertyMetadata['currency']->groups); + self::assertContains('second.test.group', $first->propertyMetadata['currency']->groups); } protected function getDriver() diff --git a/tests/Metadata/Driver/YamlDriverTest.php b/tests/Metadata/Driver/YamlDriverTest.php index 41a76a414..1191b0b81 100644 --- a/tests/Metadata/Driver/YamlDriverTest.php +++ b/tests/Metadata/Driver/YamlDriverTest.php @@ -4,6 +4,7 @@ namespace JMS\Serializer\Tests\Metadata\Driver; +use JMS\Serializer\Exception\InvalidMetadataException; use JMS\Serializer\Metadata\Driver\YamlDriver; use JMS\Serializer\Metadata\PropertyMetadata; use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; @@ -74,11 +75,10 @@ public function testBlogPostAccessor(): void self::assertEquals($p, $m->propertyMetadata['title']); } - /** - * @expectedException \JMS\Serializer\Exception\InvalidMetadataException - */ public function testInvalidMetadataFileCausesException(): void { + $this->expectException(InvalidMetadataException::class); + $this->getDriverForSubDir('invalid_metadata')->loadMetadataForClass(new \ReflectionClass(BlogPost::class)); } diff --git a/tests/Serializer/BaseSerializationTest.php b/tests/Serializer/BaseSerializationTest.php index 78492b37c..40072c6c4 100644 --- a/tests/Serializer/BaseSerializationTest.php +++ b/tests/Serializer/BaseSerializationTest.php @@ -10,6 +10,9 @@ use JMS\Serializer\DeserializationContext; use JMS\Serializer\EventDispatcher\EventDispatcher; use JMS\Serializer\EventDispatcher\Subscriber\DoctrineProxySubscriber; +use JMS\Serializer\Exception\ExpressionLanguageRequiredException; +use JMS\Serializer\Exception\InvalidMetadataException; +use JMS\Serializer\Exception\NotAcceptableException; use JMS\Serializer\Exclusion\DepthExclusionStrategy; use JMS\Serializer\Exclusion\GroupsExclusionStrategy; use JMS\Serializer\Expression\ExpressionEvaluator; @@ -223,7 +226,6 @@ public function testDeserializeNullObject() } /** - * @expectedException \JMS\Serializer\Exception\NotAcceptableException * @dataProvider getTypes */ public function testNull($type) @@ -234,6 +236,9 @@ public function testNull($type) // this is the default, but we want to be explicit here $context = SerializationContext::create()->setSerializeNull(false); + + $this->expectException(NotAcceptableException::class); + $this->serialize(null, $context); } @@ -271,15 +276,15 @@ public function testString() } } - /** - * @expectedException \JMS\Serializer\Exception\ExpressionLanguageRequiredException - * @expectedExceptionMessage To use conditional exclude/expose in JMS\Serializer\Tests\Fixtures\PersonSecret you must configure the expression language. - */ public function testExpressionExclusionNotConfigured() { $person = new PersonSecret(); $person->gender = 'f'; $person->name = 'mike'; + + $this->expectException(ExpressionLanguageRequiredException::class); + $this->expectExceptionMessage('To use conditional exclude/expose in JMS\Serializer\Tests\Fixtures\PersonSecret you must configure the expression language.'); + $this->serialize($person); } @@ -651,7 +656,7 @@ public function testDateTime($key, $value, $type) if ($this->hasDeserializer()) { $deserialized = $this->deserialize($this->getContent($key), $type); - self::assertInternalType('object', $deserialized); + self::assertIsObject($deserialized); self::assertInstanceOf(get_class($value), $deserialized); self::assertEquals($value->getTimestamp(), $deserialized->getTimestamp()); } @@ -675,7 +680,7 @@ public function testDateTimeImmutable($key, $value, $type) if ($this->hasDeserializer()) { $deserialized = $this->deserialize($this->getContent($key), $type); - self::assertInternalType('object', $deserialized); + self::assertIsObject($deserialized); self::assertInstanceOf(get_class($value), $deserialized); self::assertEquals($value->getTimestamp(), $deserialized->getTimestamp()); } @@ -730,14 +735,14 @@ public function testBlogPost() if ($this->hasDeserializer()) { $deserialized = $this->deserialize($this->getContent('blog_post'), get_class($post)); self::assertEquals('2011-07-30T00:00:00+00:00', $this->getField($deserialized, 'createdAt')->format(\DateTime::ATOM)); - self::assertAttributeEquals('This is a nice title.', 'title', $deserialized); - self::assertAttributeSame(false, 'published', $deserialized); - self::assertAttributeSame(false, 'reviewed', $deserialized); - self::assertAttributeSame('e86ce85cdb1253e4fc6352f5cf297248bceec62b', 'etag', $deserialized); - self::assertAttributeEquals(new ArrayCollection([$comment]), 'comments', $deserialized); - self::assertAttributeEquals([$comment], 'comments2', $deserialized); - self::assertAttributeEquals($author, 'author', $deserialized); - self::assertAttributeEquals([$tag1, $tag2], 'tag', $deserialized); + self::assertSame('This is a nice title.', $this->getField($deserialized, 'title')); + self::assertFalse($this->getField($deserialized, 'published')); + self::assertFalse($this->getField($deserialized, 'reviewed')); + self::assertSame('e86ce85cdb1253e4fc6352f5cf297248bceec62b', $this->getField($deserialized, 'etag')); + self::assertEquals(new ArrayCollection([$comment]), $this->getField($deserialized, 'comments')); + self::assertEquals([$comment], $this->getField($deserialized, 'comments2')); + self::assertEquals($author, $this->getField($deserialized, 'author')); + self::assertEquals([$tag1, $tag2], $this->getField($deserialized, 'tag')); } } @@ -760,10 +765,10 @@ public function testDeserializingNull() $deserialized = $this->deserialize($this->getContent('blog_post_unauthored'), get_class($post), DeserializationContext::create()); self::assertEquals('2011-07-30T00:00:00+00:00', $this->getField($deserialized, 'createdAt')->format(\DateTime::ATOM)); - self::assertAttributeEquals('This is a nice title.', 'title', $deserialized); - self::assertAttributeSame(false, 'published', $deserialized); - self::assertAttributeSame(false, 'reviewed', $deserialized); - self::assertAttributeEquals(new ArrayCollection(), 'comments', $deserialized); + self::assertSame('This is a nice title.', $this->getField($deserialized, 'title')); + self::assertFalse($this->getField($deserialized, 'published')); + self::assertFalse($this->getField($deserialized, 'reviewed')); + self::assertEquals(new ArrayCollection(), $this->getField($deserialized, 'comments')); self::assertEquals(null, $this->getField($deserialized, 'author')); } } @@ -793,14 +798,14 @@ public function testExpressionAuthorWithContextVars() } - /** - * @expectedException \JMS\Serializer\Exception\ExpressionLanguageRequiredException - * @expectedExceptionMessage The property firstName on JMS\Serializer\Tests\Fixtures\AuthorExpressionAccess requires the expression accessor strategy to be enabled. - */ public function testExpressionAccessorStrategNotEnabled() { $author = new AuthorExpressionAccess(123, 'Ruud', 'Kamphuis'); - self::assertEquals($this->getContent('author_expression'), $this->serialize($author)); + + $this->expectException(ExpressionLanguageRequiredException::class); + $this->expectExceptionMessage('The property firstName on JMS\Serializer\Tests\Fixtures\AuthorExpressionAccess requires the expression accessor strategy to be enabled.'); + + $this->serialize($author); } public function testReadOnly() @@ -973,7 +978,7 @@ public function testLifecycleCallbacks() { $object = new ObjectWithLifecycleCallbacks(); self::assertEquals($this->getContent('lifecycle_callbacks'), $this->serialize($object)); - self::assertAttributeSame(null, 'name', $object); + self::assertNull($this->getField($object, 'name')); if ($this->hasDeserializer()) { $deserialized = $this->deserialize($this->getContent('lifecycle_callbacks'), get_class($object)); @@ -1098,9 +1103,9 @@ public function testMixedAccessTypes() if ($this->hasDeserializer()) { $object = $this->deserialize($this->getContent('mixed_access_types'), 'JMS\Serializer\Tests\Fixtures\GetSetObject'); - self::assertAttributeEquals(1, 'id', $object); - self::assertAttributeEquals('Johannes', 'name', $object); - self::assertAttributeEquals(42, 'readOnlyProperty', $object); + self::assertSame(1, $this->getField($object, 'id')); + self::assertSame('Johannes', $this->getField($object, 'name')); + self::assertSame(42, $this->getField($object, 'readOnlyProperty')); } } @@ -1195,14 +1200,13 @@ public function testAdvancedGroups() ); } - /** - * @expectedException \JMS\Serializer\Exception\InvalidMetadataException - * @expectedExceptionMessage Invalid group name "foo, bar" on "JMS\Serializer\Tests\Fixtures\InvalidGroupsObject->foo", did you mean to create multiple groups? - */ public function testInvalidGroupName() { $groupsObject = new InvalidGroupsObject(); + $this->expectException(InvalidMetadataException::class); + $this->expectExceptionMessage('Invalid group name "foo, bar" on "JMS\Serializer\Tests\Fixtures\InvalidGroupsObject->foo", did you mean to create multiple groups?'); + $this->serializer->serialize($groupsObject, $this->getFormat()); } @@ -1429,10 +1433,11 @@ public function testNestedPolymorphicInterfaces() /** * @group polymorphic - * @expectedException LogicException */ public function testPolymorphicObjectsInvalidDeserialization() { + $this->expectException(\LogicException::class); + if (!$this->hasDeserializer()) { throw new \LogicException('No deserializer'); } @@ -1498,7 +1503,7 @@ public function testDeserializingIntoExistingObject() self::assertSame($order, $deseralizedOrder); self::assertEquals(new Order(new Price(12.34)), $deseralizedOrder); - self::assertAttributeInstanceOf('JMS\Serializer\Tests\Fixtures\Price', 'cost', $deseralizedOrder); + self::assertInstanceOf(Price::class, $this->getField($deseralizedOrder, 'cost')); } public function testObjectWithNullableArrays() diff --git a/tests/Serializer/Doctrine/ObjectConstructorTest.php b/tests/Serializer/Doctrine/ObjectConstructorTest.php index 1c48e68dd..57ca1dcdf 100644 --- a/tests/Serializer/Doctrine/ObjectConstructorTest.php +++ b/tests/Serializer/Doctrine/ObjectConstructorTest.php @@ -25,6 +25,8 @@ use JMS\Serializer\Construction\ObjectConstructorInterface; use JMS\Serializer\Construction\UnserializeObjectConstructor; use JMS\Serializer\DeserializationContext; +use JMS\Serializer\Exception\InvalidArgumentException; +use JMS\Serializer\Exception\ObjectConstructionException; use JMS\Serializer\Metadata\ClassMetadata; use JMS\Serializer\Metadata\Driver\DoctrineTypeDriver; use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; @@ -151,9 +153,6 @@ public function testReference() self::assertSame($author, $authorFetched); } - /** - * @expectedException \JMS\Serializer\Exception\ObjectConstructionException - */ public function testMissingAuthorException() { $fallback = $this->getMockBuilder(ObjectConstructorInterface::class)->getMock(); @@ -162,12 +161,12 @@ public function testMissingAuthorException() $class = new ClassMetadata(Author::class); $constructor = new DoctrineObjectConstructor($this->registry, $fallback, DoctrineObjectConstructor::ON_MISSING_EXCEPTION); + + $this->expectException(ObjectConstructionException::class); + $constructor->construct($this->visitor, $class, ['id' => 5], $type, $this->context); } - /** - * @expectedException \JMS\Serializer\Exception\InvalidArgumentException - */ public function testInvalidArg() { $fallback = $this->getMockBuilder(ObjectConstructorInterface::class)->getMock(); @@ -176,6 +175,9 @@ public function testInvalidArg() $class = new ClassMetadata(Author::class); $constructor = new DoctrineObjectConstructor($this->registry, $fallback, 'foo'); + + $this->expectException(InvalidArgumentException::class); + $constructor->construct($this->visitor, $class, ['id' => 5], $type, $this->context); } diff --git a/tests/Serializer/EventDispatcher/EventDispatcherTest.php b/tests/Serializer/EventDispatcher/EventDispatcherTest.php index 917ce9a27..3f7695050 100644 --- a/tests/Serializer/EventDispatcher/EventDispatcherTest.php +++ b/tests/Serializer/EventDispatcher/EventDispatcherTest.php @@ -177,14 +177,18 @@ public function testAddSubscriber() ]; $this->dispatcher->addSubscriber($subscriber); - self::assertAttributeEquals([ + + $listenersReflection = new \ReflectionProperty(EventDispatcher::class, 'listeners'); + $listenersReflection->setAccessible(true); + + self::assertSame([ 'foo.bar_baz' => [ [[$subscriber, 'onfoobarbaz'], null, 'foo', null], ], 'bar' => [ [[$subscriber, 'bar'], 'foo', null, null], ], - ], 'listeners', $this->dispatcher); + ], $listenersReflection->getValue($this->dispatcher)); } protected function setUp(): void diff --git a/tests/Serializer/EventDispatcher/Subscriber/SymfonyValidatorValidatorSubscriberTest.php b/tests/Serializer/EventDispatcher/Subscriber/SymfonyValidatorValidatorSubscriberTest.php index 831131ace..2e03ebb02 100644 --- a/tests/Serializer/EventDispatcher/Subscriber/SymfonyValidatorValidatorSubscriberTest.php +++ b/tests/Serializer/EventDispatcher/Subscriber/SymfonyValidatorValidatorSubscriberTest.php @@ -9,6 +9,7 @@ use JMS\Serializer\EventDispatcher\ObjectEvent; use JMS\Serializer\EventDispatcher\Subscriber\SymfonyValidatorSubscriber; use JMS\Serializer\EventDispatcher\Subscriber\SymfonyValidatorValidatorSubscriber; +use JMS\Serializer\Exception\ValidationFailedException; use JMS\Serializer\SerializerBuilder; use PHPUnit\Framework\TestCase; use Symfony\Component\Validator\ConstraintViolation; @@ -35,10 +36,6 @@ public function testValidate() $this->subscriber->onPostDeserialize(new ObjectEvent($context, $obj, [])); } - /** - * @expectedException \JMS\Serializer\Exception\ValidationFailedException - * @expectedExceptionMessage Validation failed with 1 error(s). - */ public function testValidateThrowsExceptionWhenListIsNotEmpty() { $obj = new \stdClass(); @@ -50,6 +47,9 @@ public function testValidateThrowsExceptionWhenListIsNotEmpty() $context = DeserializationContext::create()->setAttribute('validation_groups', ['foo']); + $this->expectException(ValidationFailedException::class); + $this->expectExceptionMessage('Validation failed with 1 error(s).'); + $this->subscriber->onPostDeserialize(new ObjectEvent($context, $obj, [])); } diff --git a/tests/Serializer/GraphNavigatorTest.php b/tests/Serializer/GraphNavigatorTest.php index ff57b7a35..0787f8d80 100644 --- a/tests/Serializer/GraphNavigatorTest.php +++ b/tests/Serializer/GraphNavigatorTest.php @@ -9,6 +9,7 @@ use JMS\Serializer\Construction\UnserializeObjectConstructor; use JMS\Serializer\DeserializationContext; use JMS\Serializer\EventDispatcher\EventDispatcher; +use JMS\Serializer\Exception\RuntimeException; use JMS\Serializer\Exclusion\ExclusionStrategyInterface; use JMS\Serializer\GraphNavigator\DeserializationGraphNavigator; use JMS\Serializer\GraphNavigator\SerializationGraphNavigator; @@ -38,12 +39,11 @@ class GraphNavigatorTest extends TestCase private $serializationVisitor; private $deserializationVisitor; - /** - * @expectedException JMS\Serializer\Exception\RuntimeException - * @expectedExceptionMessage Resources are not supported in serialized data. - */ public function testResourceThrowsException() { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Resources are not supported in serialized data.'); + $this->serializationNavigator->accept(STDIN, null); } diff --git a/tests/Serializer/JsonSerializationTest.php b/tests/Serializer/JsonSerializationTest.php index d1f95f4e0..471d5e378 100644 --- a/tests/Serializer/JsonSerializationTest.php +++ b/tests/Serializer/JsonSerializationTest.php @@ -213,16 +213,14 @@ static function (SerializationVisitorInterface $visitor, AuthorList $data, array self::assertEquals('[{"full_name":"new name"},{"full_name":"new name"}]', $this->serialize($list)); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage Invalid data "baz" (string), expected "JMS\Serializer\Tests\Fixtures\Author". - */ public function testDeserializingObjectWithObjectPropertyWithNoArrayToObject() { $content = $this->getContent('object_with_object_property_no_array_to_author'); - $object = $this->deserialize($content, 'JMS\Serializer\Tests\Fixtures\ObjectWithObjectProperty'); - self::assertEquals('bar', $object->getFoo()); - self::assertInstanceOf('JMS\Serializer\Tests\Fixtures\Author', $object->getAuthor()); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Invalid data "baz" (string), expected "JMS\Serializer\Tests\Fixtures\Author".'); + + $this->deserialize($content, 'JMS\Serializer\Tests\Fixtures\ObjectWithObjectProperty'); } public function testDeserializingObjectWithObjectProperty() @@ -268,10 +266,7 @@ public function testPrimitiveTypes($primitiveType, $data) $visitor->setNavigator($navigator); $functionToCall = 'visit' . ucfirst($primitiveType); $result = $visitor->$functionToCall($data, [], $this->getMockBuilder(SerializationContext::class)->getMock()); - if ('double' === $primitiveType) { - $primitiveType = 'float'; - } - self::assertInternalType($primitiveType, $result); + self::{'assertIs' . (['boolean' => 'bool', 'integer' => 'int', 'double' => 'float'][$primitiveType] ?? $primitiveType)}($result); } /** @@ -284,23 +279,27 @@ public function testSerializeEmptyObject() /** * @group encoding - * @expectedException RuntimeException - * @expectedExceptionMessage Your data could not be encoded because it contains invalid UTF8 characters. */ public function testSerializeWithNonUtf8EncodingWhenDisplayErrorsOff() { ini_set('display_errors', '1'); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Your data could not be encoded because it contains invalid UTF8 characters.'); + $this->serialize(['foo' => 'bar', 'bar' => pack('H*', 'c32e')]); } /** * @group encoding - * @expectedException RuntimeException - * @expectedExceptionMessage Your data could not be encoded because it contains invalid UTF8 characters. */ public function testSerializeWithNonUtf8EncodingWhenDisplayErrorsOn() { ini_set('display_errors', '0'); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Your data could not be encoded because it contains invalid UTF8 characters.'); + $this->serialize(['foo' => 'bar', 'bar' => pack('H*', 'c32e')]); } diff --git a/tests/Serializer/XmlSerializationTest.php b/tests/Serializer/XmlSerializationTest.php index 6fb3b9659..488f39c9d 100644 --- a/tests/Serializer/XmlSerializationTest.php +++ b/tests/Serializer/XmlSerializationTest.php @@ -7,6 +7,8 @@ use JMS\Serializer\Context; use JMS\Serializer\DeserializationContext; use JMS\Serializer\Exception\InvalidArgumentException; +use JMS\Serializer\Exception\RuntimeException; +use JMS\Serializer\Exception\XmlErrorException; use JMS\Serializer\GraphNavigatorInterface; use JMS\Serializer\Handler\DateHandler; use JMS\Serializer\Handler\HandlerRegistryInterface; @@ -46,12 +48,12 @@ class XmlSerializationTest extends BaseSerializationTest { - /** - * @expectedException \JMS\Serializer\Exception\RuntimeException - */ public function testInvalidUsageOfXmlValue() { $obj = new InvalidUsageOfXmlValue(); + + $this->expectException(RuntimeException::class); + $this->serialize($obj); } @@ -115,12 +117,11 @@ public function testPropertyIsCollectionOfObjectsWithAttributeAndValue() self::assertEquals($this->getContent('person_collection'), $this->serialize($personCollection)); } - /** - * @expectedException JMS\Serializer\Exception\InvalidArgumentException - * @expectedExceptionMessage The document type "]>" is not allowed. If it is safe, you may add it to the whitelist configuration. - */ public function testExternalEntitiesAreDisabledByDefault() { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The document type "]>" is not allowed. If it is safe, you may add it to the whitelist configuration.'); + $this->deserialize(' @@ -130,12 +131,11 @@ public function testExternalEntitiesAreDisabledByDefault() ', 'stdClass'); } - /** - * @expectedException JMS\Serializer\Exception\InvalidArgumentException - * @expectedExceptionMessage The document type "" is not allowed. If it is safe, you may add it to the whitelist configuration. - */ public function testDocumentTypesAreNotAllowed() { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The document type "" is not allowed. If it is safe, you may add it to the whitelist configuration.'); + $this->deserialize('', 'stdClass'); } @@ -327,14 +327,13 @@ public function testDateTimeImmutableNoCData($key, $value, $type) self::assertEquals($this->getContent($key . '_no_cdata'), $serializer->serialize($value, $this->getFormat())); } - /** - * @expectedException JMS\Serializer\Exception\RuntimeException - * @expectedExceptionMessage Unsupported value type for XML attribute map. Expected array but got object - */ public function testXmlAttributeMapWithoutArray() { $attributes = new \ArrayObject(['type' => 'text']); + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Unsupported value type for XML attribute map. Expected array but got object'); + $this->serializer->serialize(new Input($attributes), $this->getFormat()); } @@ -389,10 +388,10 @@ public function testObjectWithXmlNamespaces() $deserialized = $this->deserialize($this->getContent('object_with_xml_namespacesalias'), get_class($object)); self::assertEquals('2011-07-30T00:00:00+00:00', $this->getField($deserialized, 'createdAt')->format(\DateTime::ATOM)); - self::assertAttributeEquals('This is a nice title.', 'title', $deserialized); - self::assertAttributeSame('e86ce85cdb1253e4fc6352f5cf297248bceec62b', 'etag', $deserialized); - self::assertAttributeSame('en', 'language', $deserialized); - self::assertAttributeEquals('Foo Bar', 'author', $deserialized); + self::assertSame('This is a nice title.', $this->getField($deserialized, 'title')); + self::assertSame('e86ce85cdb1253e4fc6352f5cf297248bceec62b', $this->getField($deserialized, 'etag')); + self::assertSame('en', $this->getField($deserialized, 'language')); + self::assertSame('Foo Bar', $this->getField($deserialized, 'author')); self::assertEquals('value for empty namespace property', $this->getField($deserialized, 'emptyNsElement')); } @@ -527,11 +526,10 @@ public function testDiscriminatorAsXmlAttributeWithNamespace() ); } - /** - * @expectedException \JMS\Serializer\Exception\XmlErrorException - */ public function testDeserializeEmptyString() { + $this->expectException(XmlErrorException::class); + $this->deserialize('', 'stdClass'); } diff --git a/tests/SerializerBuilderTest.php b/tests/SerializerBuilderTest.php index fbad6e152..e40d7a411 100644 --- a/tests/SerializerBuilderTest.php +++ b/tests/SerializerBuilderTest.php @@ -5,6 +5,7 @@ namespace JMS\Serializer\Tests; use JMS\Serializer\DeserializationContext; +use JMS\Serializer\Exception\UnsupportedFormatException; use JMS\Serializer\Expression\ExpressionEvaluator; use JMS\Serializer\Handler\HandlerRegistry; use JMS\Serializer\SerializationContext; @@ -50,8 +51,8 @@ public function testWithCache() self::assertFileExists($this->tmpDir . '/metadata'); $factory = $this->getField($serializer, 'factory'); - self::assertAttributeSame(false, 'debug', $factory); - self::assertAttributeNotSame(null, 'cache', $factory); + self::assertFalse($this->getField($factory, 'debug')); + self::assertNotNull($this->getField($factory, 'cache')); } public function testDoesAddDefaultHandlers() @@ -88,10 +89,6 @@ public function testDoesNotAddDefaultHandlersWhenExplicitlyConfigured() self::assertEquals('{}', $this->builder->build()->serialize(new \DateTime('2020-04-16'), 'json')); } - /** - * @expectedException JMS\Serializer\Exception\UnsupportedFormatException - * @expectedExceptionMessage The format "xml" is not supported for serialization. - */ public function testDoesNotAddOtherVisitorsWhenConfiguredExplicitly() { self::assertSame( @@ -99,6 +96,9 @@ public function testDoesNotAddOtherVisitorsWhenConfiguredExplicitly() $this->builder->setSerializationVisitor('json', new JsonSerializationVisitorFactory()) ); + $this->expectException(UnsupportedFormatException::class); + $this->expectExceptionMessage('The format "xml" is not supported for serialization.'); + $this->builder->build()->serialize('foo', 'xml'); }