1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Tests\Happyr\MessageSerializer\Transformer; |
6
|
|
|
|
7
|
|
|
use Happyr\MessageSerializer\Transformer\Exception\TransformerNotFoundException; |
8
|
|
|
use Happyr\MessageSerializer\Transformer\Transformer; |
9
|
|
|
use Happyr\MessageSerializer\Transformer\TransformerInterface; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Symfony\Component\Messenger\Envelope; |
12
|
|
|
|
13
|
|
|
class TransformerTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function testOutputArray() |
16
|
|
|
{ |
17
|
|
|
$fooTransformer = $this->getMockBuilder(TransformerInterface::class) |
18
|
|
|
->setMethods(['getVersion', 'getIdentifier', 'getPayload', 'supportsTransform']) |
19
|
|
|
->getMock(); |
20
|
|
|
|
21
|
|
|
$version = 2; |
22
|
|
|
$identifier = 'foobar'; |
23
|
|
|
$payload = ['foo' => 'bar']; |
24
|
|
|
|
25
|
|
|
$fooTransformer->method('getVersion')->willReturn($version); |
26
|
|
|
$fooTransformer->method('getIdentifier')->willReturn($identifier); |
27
|
|
|
$fooTransformer->method('supportsTransform')->willReturn(true); |
28
|
|
|
$fooTransformer->method('getPayload')->willReturn($payload); |
29
|
|
|
|
30
|
|
|
$transformer = new Transformer([$fooTransformer]); |
31
|
|
|
$output = $transformer->toArray(new \stdClass()); |
32
|
|
|
|
33
|
|
|
$this->assertArrayHasKey('version', $output); |
34
|
|
|
$this->assertArrayHasKey('identifier', $output); |
35
|
|
|
$this->assertArrayHasKey('timestamp', $output); |
36
|
|
|
$this->assertArrayHasKey('payload', $output); |
37
|
|
|
|
38
|
|
|
$this->assertEquals($output['version'], $version); |
39
|
|
|
$this->assertEquals($output['identifier'], $identifier); |
40
|
|
|
$this->assertEquals($output['payload'], $payload); |
41
|
|
|
$this->assertEqualsWithDelta($output['timestamp'], time(), 3); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function testTransformerNotFoundExceptionClass() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$transformer = new Transformer([]); |
47
|
|
|
$this->expectException(TransformerNotFoundException::class); |
48
|
|
|
$this->expectExceptionMessage('No transformer found for "stdClass"'); |
49
|
|
|
$output = $transformer->toArray(new \stdClass()); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
public function testTransformerNotFoundExceptionInteger() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$transformer = new Transformer([]); |
55
|
|
|
$this->expectException(TransformerNotFoundException::class); |
56
|
|
|
$this->expectExceptionMessage('No transformer found for "integer"'); |
57
|
|
|
$output = $transformer->toArray(4711); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function testTransformerNotFoundExceptionEnvelope() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$transformer = new Transformer([]); |
63
|
|
|
$this->expectException(TransformerNotFoundException::class); |
64
|
|
|
$this->expectExceptionMessage('No transformer found for "Envelope<stdClass>"'); |
65
|
|
|
$output = $transformer->toArray(new Envelope(new \stdClass())); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
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.