FactoryTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /*
  3. * This file is part of the Comparator package.
  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. /**
  12. * @coversDefaultClass SebastianBergmann\Comparator\Factory
  13. *
  14. */
  15. class FactoryTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function instanceProvider()
  18. {
  19. $tmpfile = tmpfile();
  20. return array(
  21. array(NULL, NULL, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  22. array(NULL, TRUE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  23. array(TRUE, NULL, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  24. array(TRUE, TRUE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  25. array(FALSE, FALSE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  26. array(TRUE, FALSE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  27. array(FALSE, TRUE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  28. array('', '', 'SebastianBergmann\\Comparator\\ScalarComparator'),
  29. array('0', '0', 'SebastianBergmann\\Comparator\\ScalarComparator'),
  30. array('0', 0, 'SebastianBergmann\\Comparator\\NumericComparator'),
  31. array(0, '0', 'SebastianBergmann\\Comparator\\NumericComparator'),
  32. array(0, 0, 'SebastianBergmann\\Comparator\\NumericComparator'),
  33. array(1.0, 0, 'SebastianBergmann\\Comparator\\DoubleComparator'),
  34. array(0, 1.0, 'SebastianBergmann\\Comparator\\DoubleComparator'),
  35. array(1.0, 1.0, 'SebastianBergmann\\Comparator\\DoubleComparator'),
  36. array(array(1), array(1), 'SebastianBergmann\\Comparator\\ArrayComparator'),
  37. array($tmpfile, $tmpfile, 'SebastianBergmann\\Comparator\\ResourceComparator'),
  38. array(new \stdClass, new \stdClass, 'SebastianBergmann\\Comparator\\ObjectComparator'),
  39. array(new \DateTime, new \DateTime, 'SebastianBergmann\\Comparator\\DateTimeComparator'),
  40. array(new \SplObjectStorage, new \SplObjectStorage, 'SebastianBergmann\\Comparator\\SplObjectStorageComparator'),
  41. array(new \Exception, new \Exception, 'SebastianBergmann\\Comparator\\ExceptionComparator'),
  42. array(new \DOMDocument, new \DOMDocument, 'SebastianBergmann\\Comparator\\DOMNodeComparator'),
  43. // mixed types
  44. array($tmpfile, array(1), 'SebastianBergmann\\Comparator\\TypeComparator'),
  45. array(array(1), $tmpfile, 'SebastianBergmann\\Comparator\\TypeComparator'),
  46. array($tmpfile, '1', 'SebastianBergmann\\Comparator\\TypeComparator'),
  47. array('1', $tmpfile, 'SebastianBergmann\\Comparator\\TypeComparator'),
  48. array($tmpfile, new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'),
  49. array(new \stdClass, $tmpfile, 'SebastianBergmann\\Comparator\\TypeComparator'),
  50. array(new \stdClass, array(1), 'SebastianBergmann\\Comparator\\TypeComparator'),
  51. array(array(1), new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'),
  52. array(new \stdClass, '1', 'SebastianBergmann\\Comparator\\TypeComparator'),
  53. array('1', new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'),
  54. array(new ClassWithToString, '1', 'SebastianBergmann\\Comparator\\ScalarComparator'),
  55. array('1', new ClassWithToString, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  56. array(1.0, new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'),
  57. array(new \stdClass, 1.0, 'SebastianBergmann\\Comparator\\TypeComparator'),
  58. array(1.0, array(1), 'SebastianBergmann\\Comparator\\TypeComparator'),
  59. array(array(1), 1.0, 'SebastianBergmann\\Comparator\\TypeComparator'),
  60. );
  61. }
  62. /**
  63. * @dataProvider instanceProvider
  64. * @covers ::getComparatorFor
  65. * @covers ::__construct
  66. */
  67. public function testGetComparatorFor($a, $b, $expected)
  68. {
  69. $factory = new Factory;
  70. $actual = $factory->getComparatorFor($a, $b);
  71. $this->assertInstanceOf($expected, $actual);
  72. }
  73. /**
  74. * @covers ::register
  75. */
  76. public function testRegister()
  77. {
  78. $comparator = new TestClassComparator;
  79. $factory = new Factory;
  80. $factory->register($comparator);
  81. $a = new TestClass;
  82. $b = new TestClass;
  83. $expected = 'SebastianBergmann\\Comparator\\TestClassComparator';
  84. $actual = $factory->getComparatorFor($a, $b);
  85. $factory->unregister($comparator);
  86. $this->assertInstanceOf($expected, $actual);
  87. }
  88. /**
  89. * @covers ::unregister
  90. */
  91. public function testUnregister()
  92. {
  93. $comparator = new TestClassComparator;
  94. $factory = new Factory;
  95. $factory->register($comparator);
  96. $factory->unregister($comparator);
  97. $a = new TestClass;
  98. $b = new TestClass;
  99. $expected = 'SebastianBergmann\\Comparator\\ObjectComparator';
  100. $actual = $factory->getComparatorFor($a, $b);
  101. $this->assertInstanceOf($expected, $actual);
  102. }
  103. }