No Description

FactoryTest.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * @package Comparator
  15. * @author Bernhard Schussek <bschussek@2bepublished.at>
  16. * @copyright Sebastian Bergmann <sebastian@phpunit.de>
  17. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  18. * @link http://www.github.com/sebastianbergmann/comparator
  19. */
  20. class FactoryTest extends \PHPUnit_Framework_TestCase
  21. {
  22. public function instanceProvider()
  23. {
  24. $tmpfile = tmpfile();
  25. return array(
  26. array(NULL, NULL, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  27. array(NULL, TRUE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  28. array(TRUE, NULL, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  29. array(TRUE, TRUE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  30. array(FALSE, FALSE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  31. array(TRUE, FALSE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  32. array(FALSE, TRUE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  33. array('', '', 'SebastianBergmann\\Comparator\\ScalarComparator'),
  34. array('0', '0', 'SebastianBergmann\\Comparator\\ScalarComparator'),
  35. array('0', 0, 'SebastianBergmann\\Comparator\\NumericComparator'),
  36. array(0, '0', 'SebastianBergmann\\Comparator\\NumericComparator'),
  37. array(0, 0, 'SebastianBergmann\\Comparator\\NumericComparator'),
  38. array(1.0, 0, 'SebastianBergmann\\Comparator\\DoubleComparator'),
  39. array(0, 1.0, 'SebastianBergmann\\Comparator\\DoubleComparator'),
  40. array(1.0, 1.0, 'SebastianBergmann\\Comparator\\DoubleComparator'),
  41. array(array(1), array(1), 'SebastianBergmann\\Comparator\\ArrayComparator'),
  42. array($tmpfile, $tmpfile, 'SebastianBergmann\\Comparator\\ResourceComparator'),
  43. array(new \stdClass, new \stdClass, 'SebastianBergmann\\Comparator\\ObjectComparator'),
  44. array(new \DateTime, new \DateTime, 'SebastianBergmann\\Comparator\\DateTimeComparator'),
  45. array(new \SplObjectStorage, new \SplObjectStorage, 'SebastianBergmann\\Comparator\\SplObjectStorageComparator'),
  46. array(new \Exception, new \Exception, 'SebastianBergmann\\Comparator\\ExceptionComparator'),
  47. array(new \DOMDocument, new \DOMDocument, 'SebastianBergmann\\Comparator\\DOMNodeComparator'),
  48. // mixed types
  49. array($tmpfile, array(1), 'SebastianBergmann\\Comparator\\TypeComparator'),
  50. array(array(1), $tmpfile, 'SebastianBergmann\\Comparator\\TypeComparator'),
  51. array($tmpfile, '1', 'SebastianBergmann\\Comparator\\TypeComparator'),
  52. array('1', $tmpfile, 'SebastianBergmann\\Comparator\\TypeComparator'),
  53. array($tmpfile, new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'),
  54. array(new \stdClass, $tmpfile, 'SebastianBergmann\\Comparator\\TypeComparator'),
  55. array(new \stdClass, array(1), 'SebastianBergmann\\Comparator\\TypeComparator'),
  56. array(array(1), new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'),
  57. array(new \stdClass, '1', 'SebastianBergmann\\Comparator\\TypeComparator'),
  58. array('1', new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'),
  59. array(new ClassWithToString, '1', 'SebastianBergmann\\Comparator\\ScalarComparator'),
  60. array('1', new ClassWithToString, 'SebastianBergmann\\Comparator\\ScalarComparator'),
  61. array(1.0, new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'),
  62. array(new \stdClass, 1.0, 'SebastianBergmann\\Comparator\\TypeComparator'),
  63. array(1.0, array(1), 'SebastianBergmann\\Comparator\\TypeComparator'),
  64. array(array(1), 1.0, 'SebastianBergmann\\Comparator\\TypeComparator'),
  65. );
  66. }
  67. /**
  68. * @dataProvider instanceProvider
  69. * @covers ::getComparatorFor
  70. * @covers ::__construct
  71. */
  72. public function testGetComparatorFor($a, $b, $expected)
  73. {
  74. $factory = new Factory;
  75. $actual = $factory->getComparatorFor($a, $b);
  76. $this->assertInstanceOf($expected, $actual);
  77. }
  78. /**
  79. * @covers ::register
  80. */
  81. public function testRegister()
  82. {
  83. $comparator = new TestClassComparator;
  84. $factory = new Factory;
  85. $factory->register($comparator);
  86. $a = new TestClass;
  87. $b = new TestClass;
  88. $expected = 'SebastianBergmann\\Comparator\\TestClassComparator';
  89. $actual = $factory->getComparatorFor($a, $b);
  90. $factory->unregister($comparator);
  91. $this->assertInstanceOf($expected, $actual);
  92. }
  93. /**
  94. * @covers ::unregister
  95. */
  96. public function testUnregister()
  97. {
  98. $comparator = new TestClassComparator;
  99. $factory = new Factory;
  100. $factory->register($comparator);
  101. $factory->unregister($comparator);
  102. $a = new TestClass;
  103. $b = new TestClass;
  104. $expected = 'SebastianBergmann\\Comparator\\ObjectComparator';
  105. $actual = $factory->getComparatorFor($a, $b);
  106. $this->assertInstanceOf($expected, $actual);
  107. }
  108. }