DisjunctExclusionStrategyTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /*
  3. * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\Serializer\Tests\Exclusion;
  18. use JMS\Serializer\Exclusion\DisjunctExclusionStrategy;
  19. use JMS\Serializer\Metadata\ClassMetadata;
  20. use JMS\Serializer\Metadata\PropertyMetadata;
  21. use JMS\Serializer\Metadata\StaticPropertyMetadata;
  22. use JMS\Serializer\SerializationContext;
  23. class DisjunctExclusionStrategyTest extends \PHPUnit_Framework_TestCase
  24. {
  25. public function testShouldSkipClassShortCircuiting()
  26. {
  27. $metadata = new ClassMetadata('stdClass');
  28. $context = SerializationContext::create();
  29. $strat = new DisjunctExclusionStrategy(array(
  30. $first = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  31. $last = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  32. ));
  33. $first->expects($this->once())
  34. ->method('shouldSkipClass')
  35. ->with($metadata, $context)
  36. ->will($this->returnValue(true));
  37. $last->expects($this->never())
  38. ->method('shouldSkipClass');
  39. $this->assertTrue($strat->shouldSkipClass($metadata, $context));
  40. }
  41. public function testShouldSkipClassDisjunctBehavior()
  42. {
  43. $metadata = new ClassMetadata('stdClass');
  44. $context = SerializationContext::create();
  45. $strat = new DisjunctExclusionStrategy(array(
  46. $first = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  47. $last = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  48. ));
  49. $first->expects($this->once())
  50. ->method('shouldSkipClass')
  51. ->with($metadata, $context)
  52. ->will($this->returnValue(false));
  53. $last->expects($this->once())
  54. ->method('shouldSkipClass')
  55. ->with($metadata, $context)
  56. ->will($this->returnValue(true));
  57. $this->assertTrue($strat->shouldSkipClass($metadata, $context));
  58. }
  59. public function testShouldSkipClassReturnsFalseIfNoPredicateMatched()
  60. {
  61. $metadata = new ClassMetadata('stdClass');
  62. $context = SerializationContext::create();
  63. $strat = new DisjunctExclusionStrategy(array(
  64. $first = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  65. $last = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  66. ));
  67. $first->expects($this->once())
  68. ->method('shouldSkipClass')
  69. ->with($metadata, $context)
  70. ->will($this->returnValue(false));
  71. $last->expects($this->once())
  72. ->method('shouldSkipClass')
  73. ->with($metadata, $context)
  74. ->will($this->returnValue(false));
  75. $this->assertFalse($strat->shouldSkipClass($metadata, $context));
  76. }
  77. public function testShouldSkipPropertyShortCircuiting()
  78. {
  79. $metadata = new StaticPropertyMetadata('stdClass', 'foo', 'bar');
  80. $context = SerializationContext::create();
  81. $strat = new DisjunctExclusionStrategy(array(
  82. $first = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  83. $last = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  84. ));
  85. $first->expects($this->once())
  86. ->method('shouldSkipProperty')
  87. ->with($metadata, $context)
  88. ->will($this->returnValue(true));
  89. $last->expects($this->never())
  90. ->method('shouldSkipProperty');
  91. $this->assertTrue($strat->shouldSkipProperty($metadata, $context));
  92. }
  93. public function testShouldSkipPropertyDisjunct()
  94. {
  95. $metadata = new StaticPropertyMetadata('stdClass', 'foo', 'bar');
  96. $context = SerializationContext::create();
  97. $strat = new DisjunctExclusionStrategy(array(
  98. $first = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  99. $last = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  100. ));
  101. $first->expects($this->once())
  102. ->method('shouldSkipProperty')
  103. ->with($metadata, $context)
  104. ->will($this->returnValue(false));
  105. $last->expects($this->once())
  106. ->method('shouldSkipProperty')
  107. ->with($metadata, $context)
  108. ->will($this->returnValue(true));
  109. $this->assertTrue($strat->shouldSkipProperty($metadata, $context));
  110. }
  111. public function testShouldSkipPropertyReturnsFalseIfNoPredicateMatches()
  112. {
  113. $metadata = new StaticPropertyMetadata('stdClass', 'foo', 'bar');
  114. $context = SerializationContext::create();
  115. $strat = new DisjunctExclusionStrategy(array(
  116. $first = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  117. $last = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  118. ));
  119. $first->expects($this->once())
  120. ->method('shouldSkipProperty')
  121. ->with($metadata, $context)
  122. ->will($this->returnValue(false));
  123. $last->expects($this->once())
  124. ->method('shouldSkipProperty')
  125. ->with($metadata, $context)
  126. ->will($this->returnValue(false));
  127. $this->assertFalse($strat->shouldSkipProperty($metadata, $context));
  128. }
  129. }