DisjunctExclusionStrategyTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\StaticPropertyMetadata;
  21. use JMS\Serializer\SerializationContext;
  22. class DisjunctExclusionStrategyTest extends \PHPUnit_Framework_TestCase
  23. {
  24. public function testShouldSkipClassShortCircuiting()
  25. {
  26. $metadata = new ClassMetadata('stdClass');
  27. $context = SerializationContext::create();
  28. $strat = new DisjunctExclusionStrategy(array(
  29. $first = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  30. $last = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  31. ));
  32. $first->expects($this->once())
  33. ->method('shouldSkipClass')
  34. ->with($metadata, $context)
  35. ->will($this->returnValue(true));
  36. $last->expects($this->never())
  37. ->method('shouldSkipClass');
  38. $this->assertTrue($strat->shouldSkipClass($metadata, $context));
  39. }
  40. public function testShouldSkipClassDisjunctBehavior()
  41. {
  42. $metadata = new ClassMetadata('stdClass');
  43. $context = SerializationContext::create();
  44. $strat = new DisjunctExclusionStrategy(array(
  45. $first = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  46. $last = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  47. ));
  48. $first->expects($this->once())
  49. ->method('shouldSkipClass')
  50. ->with($metadata, $context)
  51. ->will($this->returnValue(false));
  52. $last->expects($this->once())
  53. ->method('shouldSkipClass')
  54. ->with($metadata, $context)
  55. ->will($this->returnValue(true));
  56. $this->assertTrue($strat->shouldSkipClass($metadata, $context));
  57. }
  58. public function testShouldSkipClassReturnsFalseIfNoPredicateMatched()
  59. {
  60. $metadata = new ClassMetadata('stdClass');
  61. $context = SerializationContext::create();
  62. $strat = new DisjunctExclusionStrategy(array(
  63. $first = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  64. $last = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  65. ));
  66. $first->expects($this->once())
  67. ->method('shouldSkipClass')
  68. ->with($metadata, $context)
  69. ->will($this->returnValue(false));
  70. $last->expects($this->once())
  71. ->method('shouldSkipClass')
  72. ->with($metadata, $context)
  73. ->will($this->returnValue(false));
  74. $this->assertFalse($strat->shouldSkipClass($metadata, $context));
  75. }
  76. public function testShouldSkipPropertyShortCircuiting()
  77. {
  78. $metadata = new StaticPropertyMetadata('stdClass', 'foo', 'bar');
  79. $context = SerializationContext::create();
  80. $strat = new DisjunctExclusionStrategy(array(
  81. $first = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  82. $last = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  83. ));
  84. $first->expects($this->once())
  85. ->method('shouldSkipProperty')
  86. ->with($metadata, $context)
  87. ->will($this->returnValue(true));
  88. $last->expects($this->never())
  89. ->method('shouldSkipProperty');
  90. $this->assertTrue($strat->shouldSkipProperty($metadata, $context));
  91. }
  92. public function testShouldSkipPropertyDisjunct()
  93. {
  94. $metadata = new StaticPropertyMetadata('stdClass', 'foo', 'bar');
  95. $context = SerializationContext::create();
  96. $strat = new DisjunctExclusionStrategy(array(
  97. $first = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  98. $last = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  99. ));
  100. $first->expects($this->once())
  101. ->method('shouldSkipProperty')
  102. ->with($metadata, $context)
  103. ->will($this->returnValue(false));
  104. $last->expects($this->once())
  105. ->method('shouldSkipProperty')
  106. ->with($metadata, $context)
  107. ->will($this->returnValue(true));
  108. $this->assertTrue($strat->shouldSkipProperty($metadata, $context));
  109. }
  110. public function testShouldSkipPropertyReturnsFalseIfNoPredicateMatches()
  111. {
  112. $metadata = new StaticPropertyMetadata('stdClass', 'foo', 'bar');
  113. $context = SerializationContext::create();
  114. $strat = new DisjunctExclusionStrategy(array(
  115. $first = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  116. $last = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface'),
  117. ));
  118. $first->expects($this->once())
  119. ->method('shouldSkipProperty')
  120. ->with($metadata, $context)
  121. ->will($this->returnValue(false));
  122. $last->expects($this->once())
  123. ->method('shouldSkipProperty')
  124. ->with($metadata, $context)
  125. ->will($this->returnValue(false));
  126. $this->assertFalse($strat->shouldSkipProperty($metadata, $context));
  127. }
  128. }