FactoryTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * @covers \SebastianBergmann\Comparator\Factory
  14. *
  15. * @uses \SebastianBergmann\Comparator\Comparator
  16. * @uses \SebastianBergmann\Comparator\Factory
  17. * @uses \SebastianBergmann\Comparator\ComparisonFailure
  18. */
  19. final 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. */
  69. public function testGetComparatorFor($a, $b, $expected): void
  70. {
  71. $factory = new Factory;
  72. $actual = $factory->getComparatorFor($a, $b);
  73. $this->assertInstanceOf($expected, $actual);
  74. }
  75. public function testRegister(): void
  76. {
  77. $comparator = new TestClassComparator;
  78. $factory = new Factory;
  79. $factory->register($comparator);
  80. $a = new TestClass;
  81. $b = new TestClass;
  82. $expected = 'SebastianBergmann\\Comparator\\TestClassComparator';
  83. $actual = $factory->getComparatorFor($a, $b);
  84. $factory->unregister($comparator);
  85. $this->assertInstanceOf($expected, $actual);
  86. }
  87. public function testUnregister(): void
  88. {
  89. $comparator = new TestClassComparator;
  90. $factory = new Factory;
  91. $factory->register($comparator);
  92. $factory->unregister($comparator);
  93. $a = new TestClass;
  94. $b = new TestClass;
  95. $expected = 'SebastianBergmann\\Comparator\\ObjectComparator';
  96. $actual = $factory->getComparatorFor($a, $b);
  97. $this->assertInstanceOf($expected, $actual);
  98. }
  99. public function testIsSingleton(): void
  100. {
  101. $f = Factory::getInstance();
  102. $this->assertSame($f, Factory::getInstance());
  103. }
  104. }