1<?php
2
3declare(strict_types=1);
4
5namespace JMS\Serializer\Tests\Exclusion;
6
7use JMS\Serializer\Exclusion\DisjunctExclusionStrategy;
8use JMS\Serializer\Metadata\ClassMetadata;
9use JMS\Serializer\Metadata\StaticPropertyMetadata;
10use JMS\Serializer\SerializationContext;
11use PHPUnit\Framework\TestCase;
12
13class DisjunctExclusionStrategyTest extends TestCase
14{
15    public function testShouldSkipClassShortCircuiting()
16    {
17        $metadata = new ClassMetadata('stdClass');
18        $context = SerializationContext::create();
19
20        $strat = new DisjunctExclusionStrategy([
21            $first = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(),
22            $last = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(),
23        ]);
24
25        $first->expects($this->once())
26            ->method('shouldSkipClass')
27            ->with($metadata, $context)
28            ->will($this->returnValue(true));
29
30        $last->expects($this->never())
31            ->method('shouldSkipClass');
32
33        self::assertTrue($strat->shouldSkipClass($metadata, $context));
34    }
35
36    public function testShouldSkipClassDisjunctBehavior()
37    {
38        $metadata = new ClassMetadata('stdClass');
39        $context = SerializationContext::create();
40
41        $strat = new DisjunctExclusionStrategy([
42            $first = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(),
43            $last = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(),
44        ]);
45
46        $first->expects($this->once())
47            ->method('shouldSkipClass')
48            ->with($metadata, $context)
49            ->will($this->returnValue(false));
50
51        $last->expects($this->once())
52            ->method('shouldSkipClass')
53            ->with($metadata, $context)
54            ->will($this->returnValue(true));
55
56        self::assertTrue($strat->shouldSkipClass($metadata, $context));
57    }
58
59    public function testShouldSkipClassReturnsFalseIfNoPredicateMatched()
60    {
61        $metadata = new ClassMetadata('stdClass');
62        $context = SerializationContext::create();
63
64        $strat = new DisjunctExclusionStrategy([
65            $first = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(),
66            $last = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(),
67        ]);
68
69        $first->expects($this->once())
70            ->method('shouldSkipClass')
71            ->with($metadata, $context)
72            ->will($this->returnValue(false));
73
74        $last->expects($this->once())
75            ->method('shouldSkipClass')
76            ->with($metadata, $context)
77            ->will($this->returnValue(false));
78
79        self::assertFalse($strat->shouldSkipClass($metadata, $context));
80    }
81
82    public function testShouldSkipPropertyShortCircuiting()
83    {
84        $metadata = new StaticPropertyMetadata('stdClass', 'foo', 'bar');
85        $context = SerializationContext::create();
86
87        $strat = new DisjunctExclusionStrategy([
88            $first = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(),
89            $last = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(),
90        ]);
91
92        $first->expects($this->once())
93            ->method('shouldSkipProperty')
94            ->with($metadata, $context)
95            ->will($this->returnValue(true));
96
97        $last->expects($this->never())
98            ->method('shouldSkipProperty');
99
100        self::assertTrue($strat->shouldSkipProperty($metadata, $context));
101    }
102
103    public function testShouldSkipPropertyDisjunct()
104    {
105        $metadata = new StaticPropertyMetadata('stdClass', 'foo', 'bar');
106        $context = SerializationContext::create();
107
108        $strat = new DisjunctExclusionStrategy([
109            $first = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(),
110            $last = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(),
111        ]);
112
113        $first->expects($this->once())
114            ->method('shouldSkipProperty')
115            ->with($metadata, $context)
116            ->will($this->returnValue(false));
117
118        $last->expects($this->once())
119            ->method('shouldSkipProperty')
120            ->with($metadata, $context)
121            ->will($this->returnValue(true));
122
123        self::assertTrue($strat->shouldSkipProperty($metadata, $context));
124    }
125
126    public function testShouldSkipPropertyReturnsFalseIfNoPredicateMatches()
127    {
128        $metadata = new StaticPropertyMetadata('stdClass', 'foo', 'bar');
129        $context = SerializationContext::create();
130
131        $strat = new DisjunctExclusionStrategy([
132            $first = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(),
133            $last = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(),
134        ]);
135
136        $first->expects($this->once())
137            ->method('shouldSkipProperty')
138            ->with($metadata, $context)
139            ->will($this->returnValue(false));
140
141        $last->expects($this->once())
142            ->method('shouldSkipProperty')
143            ->with($metadata, $context)
144            ->will($this->returnValue(false));
145
146        self::assertFalse($strat->shouldSkipProperty($metadata, $context));
147    }
148}
149