Skip to content

Commit

Permalink
Merge pull request #1123 from Majkl578/phpunit-deprecations
Browse files Browse the repository at this point in the history
Fix PHPUnit deprecations
  • Loading branch information
goetas committed Sep 14, 2019
2 parents 89fc2a7 + 1b1b3f5 commit ff89778
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 102 deletions.
15 changes: 9 additions & 6 deletions tests/Metadata/Driver/XmlDriverTest.php
Expand Up @@ -4,23 +4,24 @@

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;
use Metadata\Driver\FileLocator;

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');
}

Expand Down Expand Up @@ -76,15 +77,17 @@ 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()
{
$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()
Expand Down
6 changes: 3 additions & 3 deletions tests/Metadata/Driver/YamlDriverTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

Expand Down
73 changes: 39 additions & 34 deletions tests/Serializer/BaseSerializationTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -223,7 +226,6 @@ public function testDeserializeNullObject()
}

/**
* @expectedException \JMS\Serializer\Exception\NotAcceptableException
* @dataProvider getTypes
*/
public function testNull($type)
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand Down Expand Up @@ -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'));
}
}

Expand All @@ -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'));
}
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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'));
}
}

Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 8 additions & 6 deletions tests/Serializer/Doctrine/ObjectConstructorTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}

Expand Down
8 changes: 6 additions & 2 deletions tests/Serializer/EventDispatcher/EventDispatcherTest.php
Expand Up @@ -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
Expand Down
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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, []));
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Serializer/GraphNavigatorTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit ff89778

Please sign in to comment.