SerializerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Test\Unit\Model\Cache;
  7. use Vertex\Tax\Model\Cache\Serializer;
  8. use Vertex\Tax\Test\Unit\TestCase;
  9. /**
  10. * Test cache storage serializer functionality.
  11. */
  12. class SerializerTest extends TestCase
  13. {
  14. /** @var Serializer */
  15. private $serializer;
  16. /**
  17. * Perform test setup.
  18. */
  19. protected function setUp()
  20. {
  21. parent::setUp();
  22. $this->serializer = $this->getObject(Serializer::class);
  23. }
  24. /**
  25. * Test that string data can be handled by the serializer with integrity.
  26. *
  27. * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
  28. * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
  29. */
  30. public function testStringType()
  31. {
  32. $expectedValue = 'test';
  33. $output = $this->serializer->serialize($expectedValue);
  34. $actualResult = $this->serializer->unserialize($output);
  35. $this->assertEquals($expectedValue, $actualResult);
  36. }
  37. /**
  38. * Test that double type data can be handled by the serializer with integrity.
  39. *
  40. * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
  41. * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
  42. */
  43. public function testDoubleType()
  44. {
  45. $expectedValue = 65.18;
  46. $output = $this->serializer->serialize($expectedValue);
  47. $actualResult = $this->serializer->unserialize($output);
  48. $this->assertEquals($expectedValue, $actualResult);
  49. }
  50. /**
  51. * Test that integer data can be handled by the serializer with integrity.
  52. *
  53. * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
  54. * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
  55. */
  56. public function testIntegerType()
  57. {
  58. $expectedValue = 100;
  59. $output = $this->serializer->serialize($expectedValue);
  60. $actualResult = $this->serializer->unserialize($output);
  61. $this->assertEquals($expectedValue, $actualResult);
  62. }
  63. /**
  64. * Test that boolean data can be handled by the serializer with integrity.
  65. *
  66. * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
  67. * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
  68. */
  69. public function testBooleanType()
  70. {
  71. $expectedValue = false;
  72. $output = $this->serializer->serialize($expectedValue);
  73. $actualResult = $this->serializer->unserialize($output);
  74. $this->assertEquals($expectedValue, $actualResult);
  75. }
  76. /**
  77. * Test that array data can be handled by the serializer with integrity.
  78. *
  79. * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
  80. * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
  81. */
  82. public function testArrayType()
  83. {
  84. $expectedValue = ['test'];
  85. $output = $this->serializer->serialize($expectedValue);
  86. $actualResult = $this->serializer->unserialize($output);
  87. $this->assertEquals('array', gettype($actualResult));
  88. $this->assertTrue(count($actualResult) === 1);
  89. $this->assertEquals($expectedValue[0], $actualResult[0]);
  90. }
  91. /**
  92. * Test that boolean data can be handled by the serializer with integrity.
  93. *
  94. * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
  95. * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
  96. */
  97. public function testNullType()
  98. {
  99. $expectedValue = null;
  100. $output = $this->serializer->serialize($expectedValue);
  101. $actualResult = $this->serializer->unserialize($output);
  102. $this->assertEquals($expectedValue, $actualResult);
  103. }
  104. /**
  105. * Test that unsupported types will throw an exception.
  106. *
  107. * @dataProvider provideUnsupportedInputs
  108. * @param mixed $input
  109. * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
  110. * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
  111. */
  112. public function testUnsupportedInputType($input)
  113. {
  114. $this->expectException(\InvalidArgumentException::class);
  115. $this->serializer->serialize($input);
  116. }
  117. /**
  118. * Test that circular references within an array cannot be accepted.
  119. *
  120. * @covers \Vertex\Tax\Model\Cache\Serializer::serialize()
  121. * @covers \Vertex\Tax\Model\Cache\Serializer::unserialize()
  122. */
  123. public function testArrayRecursion()
  124. {
  125. // For local environment tests in which XDebug is enabled.
  126. ini_set('xdebug.max_nesting_level', Serializer::MAX_ARRAY_DEPTH + 1);
  127. $this->expectException(\InvalidArgumentException::class);
  128. $this->expectExceptionMessage(
  129. sprintf('Serializable array depth cannot exceed %d', Serializer::MAX_ARRAY_DEPTH)
  130. );
  131. $input = ['test'];
  132. $input['reference'] = &$input;
  133. try {
  134. $this->serializer->serialize($input);
  135. } catch (\Error $error) {
  136. $this->markTestSkipped('Could not set high enough nesting level.');
  137. }
  138. }
  139. /**
  140. * Provide various inputs that should not be supported by serialization
  141. *
  142. * @return array
  143. */
  144. public function provideUnsupportedInputs()
  145. {
  146. return [
  147. [$this->getMockBuilder('NonStandardObject')],
  148. [['key' => $this->getMockBuilder('NonStandardObject')],],
  149. [['key' => ['otherKey' => $this->getMockBuilder('NonStandardObject')]]],
  150. ];
  151. }
  152. }