interfaces_test.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. // $Id: interfaces_test.php 1781 2008-04-22 10:43:01Z lastcraft $
  3. require_once(dirname(__FILE__) . '/../autorun.php');
  4. if (function_exists('spl_classes')) {
  5. include(dirname(__FILE__) . '/support/spl_examples.php');
  6. }
  7. if (version_compare(PHP_VERSION, '5.1', '>=')) {
  8. include(dirname(__FILE__) . '/interfaces_test_php5_1.php');
  9. }
  10. interface DummyInterface {
  11. function aMethod();
  12. function anotherMethod($a);
  13. function &referenceMethod(&$a);
  14. }
  15. Mock::generate('DummyInterface');
  16. Mock::generatePartial('DummyInterface', 'PartialDummyInterface', array());
  17. class TestOfMockInterfaces extends UnitTestCase {
  18. function testCanMockAnInterface() {
  19. $mock = new MockDummyInterface();
  20. $this->assertIsA($mock, 'SimpleMock');
  21. $this->assertIsA($mock, 'MockDummyInterface');
  22. $this->assertTrue(method_exists($mock, 'aMethod'));
  23. $this->assertTrue(method_exists($mock, 'anotherMethod'));
  24. $this->assertNull($mock->aMethod());
  25. }
  26. function testMockedInterfaceExpectsParameters() {
  27. $mock = new MockDummyInterface();
  28. $this->expectError();
  29. $mock->anotherMethod();
  30. }
  31. function testCannotPartiallyMockAnInterface() {
  32. $this->assertFalse(class_exists('PartialDummyInterface'));
  33. }
  34. }
  35. class TestOfSpl extends UnitTestCase {
  36. function skip() {
  37. $this->skipUnless(function_exists('spl_classes'), 'No SPL module loaded');
  38. }
  39. function testCanMockAllSplClasses() {
  40. if (! function_exists('spl_classes')) {
  41. return;
  42. }
  43. foreach(spl_classes() as $class) {
  44. if ($class == 'SplHeap') {
  45. continue;
  46. }
  47. if (version_compare(PHP_VERSION, '5.1', '<') &&
  48. $class == 'CachingIterator' ||
  49. $class == 'CachingRecursiveIterator' ||
  50. $class == 'FilterIterator' ||
  51. $class == 'LimitIterator' ||
  52. $class == 'ParentIterator') {
  53. // These iterators require an iterator be passed to them during
  54. // construction in PHP 5.0; there is no way for SimpleTest
  55. // to supply such an iterator, however, so support for it is
  56. // disabled.
  57. continue;
  58. }
  59. $mock_class = "Mock$class";
  60. Mock::generate($class);
  61. $this->assertIsA(new $mock_class(), $mock_class);
  62. }
  63. }
  64. function testExtensionOfCommonSplClasses() {
  65. Mock::generate('IteratorImplementation');
  66. $this->assertIsA(
  67. new IteratorImplementation(),
  68. 'IteratorImplementation');
  69. Mock::generate('IteratorAggregateImplementation');
  70. $this->assertIsA(
  71. new IteratorAggregateImplementation(),
  72. 'IteratorAggregateImplementation');
  73. }
  74. }
  75. class WithHint {
  76. function hinted(DummyInterface $object) { }
  77. }
  78. class ImplementsDummy implements DummyInterface {
  79. function aMethod() { }
  80. function anotherMethod($a) { }
  81. function &referenceMethod(&$a) { }
  82. function extraMethod($a = false) { }
  83. }
  84. Mock::generate('ImplementsDummy');
  85. class TestOfImplementations extends UnitTestCase {
  86. function testMockedInterfaceCanPassThroughTypeHint() {
  87. $mock = new MockDummyInterface();
  88. $hinter = new WithHint();
  89. $hinter->hinted($mock);
  90. }
  91. function testImplementedInterfacesAreCarried() {
  92. $mock = new MockImplementsDummy();
  93. $hinter = new WithHint();
  94. $hinter->hinted($mock);
  95. }
  96. function testNoSpuriousWarningsWhenSkippingDefaultedParameter() {
  97. $mock = new MockImplementsDummy();
  98. $mock->extraMethod();
  99. }
  100. }
  101. interface SampleInterfaceWithConstruct {
  102. function __construct($something);
  103. }
  104. class TestOfInterfaceMocksWithConstruct extends UnitTestCase {
  105. function TODO_testBasicConstructOfAnInterface() { // Fails in PHP 5.3dev
  106. Mock::generate('SampleInterfaceWithConstruct');
  107. }
  108. }
  109. interface SampleInterfaceWithClone {
  110. function __clone();
  111. }
  112. class TestOfSampleInterfaceWithClone extends UnitTestCase {
  113. function testCanMockWithoutErrors() {
  114. Mock::generate('SampleInterfaceWithClone');
  115. }
  116. }
  117. ?>