1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JMS\SerializerBundle\Tests\ContextFactory; |
4
|
|
|
|
5
|
|
|
use JMS\SerializerBundle\ContextFactory\ConfiguredContextFactory; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ConfiguredContextFactoryTest |
9
|
|
|
*/ |
10
|
|
|
class ConfiguredContextFactoryTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
View Code Duplication |
public function testCreateSerializationContext() |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
$config = $this->getContextConfig(); |
15
|
|
|
$object = new ConfiguredContextFactory($config); |
16
|
|
|
|
17
|
|
|
$this->assertInstanceOf('JMS\Serializer\ContextFactory\SerializationContextFactoryInterface', $object); |
18
|
|
|
|
19
|
|
|
$context = $object->createSerializationContext(); |
20
|
|
|
$this->assertInstanceOf('JMS\Serializer\SerializationContext', $context); |
21
|
|
|
$this->assertSame($config['serialize_null'], $context->shouldSerializeNull()); |
22
|
|
|
|
23
|
|
|
$this->assertSame($config['version'], $context->attributes->get('version')->get()); |
24
|
|
|
$this->assertSame($config['groups'], $context->attributes->get('groups')->get()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
public function testCreateDeserializationContext() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$config = $this->getContextConfig(); |
30
|
|
|
$object = new ConfiguredContextFactory($config); |
31
|
|
|
|
32
|
|
|
$this->assertInstanceOf('JMS\Serializer\ContextFactory\DeserializationContextFactoryInterface', $object); |
33
|
|
|
|
34
|
|
|
$context = $object->createDeserializationContext(); |
35
|
|
|
$this->assertInstanceOf('JMS\Serializer\DeserializationContext', $context); |
36
|
|
|
$this->assertSame($config['serialize_null'], $context->shouldSerializeNull()); |
37
|
|
|
|
38
|
|
|
$this->assertSame($config['version'], $context->attributes->get('version')->get()); |
39
|
|
|
$this->assertSame($config['groups'], $context->attributes->get('groups')->get()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function getContextConfig() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
'attributes' => [ |
46
|
|
|
'x' => mt_rand(0, PHP_INT_MAX), |
47
|
|
|
], |
48
|
|
|
'groups' => [ 'Default', 'Registration' ], |
49
|
|
|
'serialize_null' => true, |
50
|
|
|
'version' => sprintf('%d.%d.%d', mt_rand(1, 10), mt_rand(1, 10), mt_rand(1, 10)), |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.