8
8
use DateTimeImmutable ;
9
9
use Generator ;
10
10
use PHPUnit \Framework \TestCase ;
11
- use Prophecy \Argument ;
12
- use Prophecy \PhpUnit \ProphecyTrait ;
13
- use Prophecy \Prophecy \ObjectProphecy ;
14
- use Symfony \Component \Serializer \Exception \ExceptionInterface ;
15
- use Symfony \Component \Serializer \Normalizer \DenormalizerInterface ;
11
+ use Symfony \Component \Serializer \Exception \UnsupportedException ;
16
12
use Yokai \Batch \Bridge \Symfony \Serializer \DenormalizeItemProcessor ;
17
13
use Yokai \Batch \Job \Item \Exception \SkipItemException ;
14
+ use Yokai \Batch \Job \Item \Exception \SkipItemOnError ;
15
+ use Yokai \Batch \Tests \Bridge \Symfony \Serializer \Dummy \DummyNormalizer ;
16
+ use Yokai \Batch \Tests \Bridge \Symfony \Serializer \Dummy \FailingNormalizer ;
18
17
19
18
final class DenormalizeItemProcessorTest extends TestCase
20
19
{
21
- use ProphecyTrait;
22
-
23
- /**
24
- * @var ObjectProphecy|DenormalizerInterface
25
- */
26
- private $ denormalizer ;
27
-
28
- protected function setUp (): void
29
- {
30
- $ this ->denormalizer = $ this ->prophesize (DenormalizerInterface::class);
31
- }
32
-
33
20
/**
34
21
* @dataProvider sets
35
22
*/
36
23
public function testProcess (string $ type , ?string $ format , array $ context , $ item , $ expected ): void
37
24
{
38
- $ this ->denormalizer ->supportsDenormalization ($ item , $ type , $ format )
39
- ->shouldBeCalled ()
40
- ->willReturn (true );
41
- $ this ->denormalizer ->denormalize ($ item , $ type , $ format , $ context )
42
- ->shouldBeCalled ()
43
- ->willReturn ($ expected );
44
-
45
- $ processor = new DenormalizeItemProcessor ($ this ->denormalizer ->reveal (), $ type , $ format , $ context );
25
+ $ denormalizer = new DummyNormalizer (true , $ expected );
26
+ $ processor = new DenormalizeItemProcessor ($ denormalizer , $ type , $ format , $ context );
46
27
47
28
self ::assertSame ($ expected , $ processor ->process ($ item ));
48
29
}
@@ -52,39 +33,43 @@ public function testProcess(string $type, ?string $format, array $context, $item
52
33
*/
53
34
public function testUnsupported (string $ type , ?string $ format , array $ context , $ item ): void
54
35
{
55
- $ this ->expectException (SkipItemException::class);
56
-
57
- $ this ->denormalizer ->supportsDenormalization ($ item , $ type , $ format )
58
- ->shouldBeCalled ()
59
- ->willReturn (false );
60
- $ this ->denormalizer ->denormalize (Argument::cetera ())
61
- ->shouldNotBeCalled ();
62
-
63
- $ processor = new DenormalizeItemProcessor ($ this ->denormalizer ->reveal (), $ type , $ format , $ context );
64
-
65
- $ processor ->process ($ item );
36
+ $ denormalizer = new DummyNormalizer (false , null );
37
+ $ processor = new DenormalizeItemProcessor ($ denormalizer , $ type , $ format , $ context );
38
+
39
+ $ exception = null ;
40
+ try {
41
+ $ processor ->process ($ item );
42
+ } catch (SkipItemException $ exception ) {
43
+ // just capture the exception
44
+ }
45
+
46
+ self ::assertNotNull ($ exception , 'Processor has thrown an exception ' );
47
+ $ cause = $ exception ->getCause ();
48
+ self ::assertInstanceOf (SkipItemOnError::class, $ cause );
49
+ /** @var SkipItemOnError $cause */
50
+ self ::assertSame ('Unable to denormalize item. Not supported. ' , $ cause ->getError ()->getMessage ());
66
51
}
67
52
68
53
/**
69
54
* @dataProvider sets
70
55
*/
71
56
public function testException (string $ type , ?string $ format , array $ context , $ item ): void
72
57
{
73
- $ this -> expectException (SkipItemException::class );
74
-
75
- $ this -> denormalizer -> supportsDenormalization ( $ item , $ type , $ format )
76
- -> shouldBeCalled ()
77
- -> willReturn ( true );
78
- $ this -> denormalizer -> denormalize ($ item, $ type , $ format , $ context )
79
- -> shouldBeCalled ()
80
- -> willThrow (
81
- new class extends \Exception implements ExceptionInterface {
82
- }
83
- );
84
-
85
- $ processor = new DenormalizeItemProcessor ( $ this -> denormalizer -> reveal () , $ type , $ format , $ context );
86
-
87
- $ processor -> process ( $ item );
58
+ $ denormalizer = new FailingNormalizer ( $ exceptionThrown = new UnsupportedException () );
59
+ $ processor = new DenormalizeItemProcessor ( $ denormalizer , $ type , $ format , $ context );
60
+
61
+ $ exception = null ;
62
+ try {
63
+ $ processor -> process ($ item);
64
+ } catch ( SkipItemException $ exception ) {
65
+ // just capture the exception
66
+ }
67
+
68
+ self :: assertNotNull ( $ exception , ' Processor has thrown an exception ' );
69
+ $ cause = $ exception -> getCause ();
70
+ self :: assertInstanceOf (SkipItemOnError::class , $ cause );
71
+ /** @var SkipItemOnError $cause */
72
+ self :: assertSame ( $ exceptionThrown , $ cause -> getError () );
88
73
}
89
74
90
75
public function sets (): Generator
0 commit comments