FactoryTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /*
  3. * This file is part of sebastian/comparator.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Comparator;
  11. use PHPUnit\Framework\TestCase;
  12. /**
  13. * @coversDefaultClass SebastianBergmann\Comparator\Factory
  14. *
  15. * @uses SebastianBergmann\Comparator\Comparator
  16. * @uses SebastianBergmann\Comparator\Factory
  17. * @uses SebastianBergmann\Comparator\ComparisonFailure
  18. */
  19. class FactoryTest extends TestCase
  20. {
  21. public function instanceProvider()
  22. {
  23. $tmpfile = \tmpfile();
  24. return [
  25. [null, null, 'SebastianBergmann\\Comparator\\ScalarComparator'],
  26. [null, true, 'SebastianBergmann\\Comparator\\ScalarComparator'],
  27. [true, null, 'SebastianBergmann\\Comparator\\ScalarComparator'],
  28. [true, true, 'SebastianBergmann\\Comparator\\ScalarComparator'],
  29. [false, false, 'SebastianBergmann\\Comparator\\ScalarComparator'],
  30. [true, false, 'SebastianBergmann\\Comparator\\ScalarComparator'],
  31. [false, true, 'SebastianBergmann\\Comparator\\ScalarComparator'],
  32. ['', '', 'SebastianBergmann\\Comparator\\ScalarComparator'],
  33. ['0', '0', 'SebastianBergmann\\Comparator\\ScalarComparator'],
  34. ['0', 0, 'SebastianBergmann\\Comparator\\NumericComparator'],
  35. [0, '0', 'SebastianBergmann\\Comparator\\NumericComparator'],
  36. [0, 0, 'SebastianBergmann\\Comparator\\NumericComparator'],
  37. [1.0, 0, 'SebastianBergmann\\Comparator\\DoubleComparator'],
  38. [0, 1.0, 'SebastianBergmann\\Comparator\\DoubleComparator'],
  39. [1.0, 1.0, 'SebastianBergmann\\Comparator\\DoubleComparator'],
  40. [[1], [1], 'SebastianBergmann\\Comparator\\ArrayComparator'],
  41. [$tmpfile, $tmpfile, 'SebastianBergmann\\Comparator\\ResourceComparator'],
  42. [new \stdClass, new \stdClass, 'SebastianBergmann\\Comparator\\ObjectComparator'],
  43. [new \DateTime, new \DateTime, 'SebastianBergmann\\Comparator\\DateTimeComparator'],
  44. [new \SplObjectStorage, new \SplObjectStorage, 'SebastianBergmann\\Comparator\\SplObjectStorageComparator'],
  45. [new \Exception, new \Exception, 'SebastianBergmann\\Comparator\\ExceptionComparator'],
  46. [new \DOMDocument, new \DOMDocument, 'SebastianBergmann\\Comparator\\DOMNodeComparator'],
  47. // mixed types
  48. [$tmpfile, [1], 'SebastianBergmann\\Comparator\\TypeComparator'],
  49. [[1], $tmpfile, 'SebastianBergmann\\Comparator\\TypeComparator'],
  50. [$tmpfile, '1', 'SebastianBergmann\\Comparator\\TypeComparator'],
  51. ['1', $tmpfile, 'SebastianBergmann\\Comparator\\TypeComparator'],
  52. [$tmpfile, new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'],
  53. [new \stdClass, $tmpfile, 'SebastianBergmann\\Comparator\\TypeComparator'],
  54. [new \stdClass, [1], 'SebastianBergmann\\Comparator\\TypeComparator'],
  55. [[1], new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'],
  56. [new \stdClass, '1', 'SebastianBergmann\\Comparator\\TypeComparator'],
  57. ['1', new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'],
  58. [new ClassWithToString, '1', 'SebastianBergmann\\Comparator\\ScalarComparator'],
  59. ['1', new ClassWithToString, 'SebastianBergmann\\Comparator\\ScalarComparator'],
  60. [1.0, new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'],
  61. [new \stdClass, 1.0, 'SebastianBergmann\\Comparator\\TypeComparator'],
  62. [1.0, [1], 'SebastianBergmann\\Comparator\\TypeComparator'],
  63. [[1], 1.0, 'SebastianBergmann\\Comparator\\TypeComparator'],
  64. ];
  65. }
  66. /**
  67. * @dataProvider instanceProvider
  68. * @covers ::getComparatorFor
  69. * @covers ::__construct
  70. */
  71. public function testGetComparatorFor($a, $b, $expected)
  72. {
  73. $factory = new Factory;
  74. $actual = $factory->getComparatorFor($a, $b);
  75. $this->assertInstanceOf($expected, $actual);
  76. }
  77. /**
  78. * @covers ::register
  79. */
  80. public function testRegister()
  81. {
  82. $comparator = new TestClassComparator;
  83. $factory = new Factory;
  84. $factory->register($comparator);
  85. $a = new TestClass;
  86. $b = new TestClass;
  87. $expected = 'SebastianBergmann\\Comparator\\TestClassComparator';
  88. $actual = $factory->getComparatorFor($a, $b);
  89. $factory->unregister($comparator);
  90. $this->assertInstanceOf($expected, $actual);
  91. }
  92. /**
  93. * @covers ::unregister
  94. */
  95. public function testUnregister()
  96. {
  97. $comparator = new TestClassComparator;
  98. $factory = new Factory;
  99. $factory->register($comparator);
  100. $factory->unregister($comparator);
  101. $a = new TestClass;
  102. $b = new TestClass;
  103. $expected = 'SebastianBergmann\\Comparator\\ObjectComparator';
  104. $actual = $factory->getComparatorFor($a, $b);
  105. $this->assertInstanceOf($expected, $actual);
  106. }
  107. public function testIsSingleton()
  108. {
  109. $f = Factory::getInstance();
  110. $this->assertSame($f, Factory::getInstance());
  111. }
  112. }