testReverseTransformForwardsToProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Tests\Form\DataTransformer;
15
16
use PHPUnit\Framework\TestCase;
17
use Prophecy\Argument;
18
use Psr\Log\LoggerInterface;
19
use Sonata\MediaBundle\Form\DataTransformer\ServiceProviderDataTransformer;
20
use Sonata\MediaBundle\Model\MediaInterface;
21
use Sonata\MediaBundle\Provider\MediaProviderInterface;
22
23
class ServiceProviderDataTransformerTest extends TestCase
24
{
25
    public function testTransformNoop(): void
26
    {
27
        $provider = $this->prophesize(MediaProviderInterface::class);
28
29
        $transformer = new ServiceProviderDataTransformer($provider->reveal());
30
31
        $value = new \stdClass();
32
        $this->assertSame($value, $transformer->transform($value));
33
    }
34
35
    public function testReverseTransformSkipsProviderIfNotMedia(): void
36
    {
37
        $provider = $this->prophesize(MediaProviderInterface::class);
38
        $provider->transform()->shouldNotBeCalled();
39
40
        $transformer = new ServiceProviderDataTransformer($provider->reveal());
41
42
        $media = new \stdClass();
43
        $this->assertSame($media, $transformer->reverseTransform($media));
44
    }
45
46
    public function testReverseTransformForwardsToProvider(): void
47
    {
48
        $media = $this->prophesize(MediaInterface::class)->reveal();
49
50
        $provider = $this->prophesize(MediaProviderInterface::class);
51
        $provider->transform(Argument::is($media))->shouldBeCalledTimes(1);
52
53
        $transformer = new ServiceProviderDataTransformer($provider->reveal());
54
        $this->assertSame($media, $transformer->reverseTransform($media));
55
    }
56
57
    public function testReverseTransformWithThrowingProviderNoThrow(): void
58
    {
59
        $media = $this->prophesize(MediaInterface::class)->reveal();
60
61
        $provider = $this->prophesize(MediaProviderInterface::class);
62
        $provider->transform(Argument::is($media))->shouldBeCalled()->willThrow(new \Exception());
63
64
        $transformer = new ServiceProviderDataTransformer($provider->reveal());
65
        $transformer->reverseTransform($media);
66
    }
67
68
    public function testReverseTransformWithThrowingProviderLogsException(): void
69
    {
70
        $media = $this->prophesize(MediaInterface::class)->reveal();
71
72
        $exception = new \Exception('foo');
73
        $provider = $this->prophesize(MediaProviderInterface::class);
74
        $provider->transform(Argument::is($media))->shouldBeCalled()->willThrow($exception);
75
76
        $logger = $this->prophesize(LoggerInterface::class);
77
        $logger->error(
78
            Argument::containingString('Caught Exception Exception: "foo" at'),
79
            Argument::is(['exception' => $exception])
80
        )->shouldBeCalled();
81
82
        $transformer = new ServiceProviderDataTransformer($provider->reveal());
83
        $transformer->setLogger($logger->reveal());
84
        $transformer->reverseTransform($media);
85
    }
86
}
87