PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/simpletest/test/interfaces_test.php

https://bitbucket.org/mercucci/mercucci
PHP | 137 lines | 114 code | 22 blank | 1 comment | 4 complexity | ba5d414cc46ef0932bf1acaefb551af4 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. // $Id: interfaces_test.php 1699 2008-03-24 16:01:29Z lastcraft $
  3. require_once(dirname(__FILE__) . '/../autorun.php');
  4. if (function_exists('spl_classes')) {
  5. include(dirname(__FILE__) . '/support/spl_examples.php');
  6. }
  7. interface DummyInterface {
  8. function aMethod();
  9. function anotherMethod($a);
  10. function &referenceMethod(&$a);
  11. }
  12. Mock::generate('DummyInterface');
  13. Mock::generatePartial('DummyInterface', 'PartialDummyInterface', array());
  14. class TestOfMockInterfaces extends UnitTestCase {
  15. function testCanMockAnInterface() {
  16. $mock = new MockDummyInterface();
  17. $this->assertIsA($mock, 'SimpleMock');
  18. $this->assertIsA($mock, 'MockDummyInterface');
  19. $this->assertTrue(method_exists($mock, 'aMethod'));
  20. $this->assertTrue(method_exists($mock, 'anotherMethod'));
  21. $this->assertNull($mock->aMethod());
  22. }
  23. function testMockedInterfaceExpectsParameters() {
  24. $mock = new MockDummyInterface();
  25. $mock->anotherMethod();
  26. $this->assertError();
  27. }
  28. function testCannotPartiallyMockAnInterface() {
  29. $this->assertFalse(class_exists('PartialDummyInterface'));
  30. }
  31. }
  32. class TestOfSpl extends UnitTestCase {
  33. function skip() {
  34. $this->skipUnless(function_exists('spl_classes'), 'No SPL module loaded');
  35. }
  36. function testCanMockAllSplClasses() {
  37. if (! function_exists('spl_classes')) {
  38. return;
  39. }
  40. foreach(spl_classes() as $class) {
  41. if ($class == 'SplHeap') {
  42. continue;
  43. }
  44. $mock_class = "Mock$class";
  45. Mock::generate($class);
  46. $this->assertIsA(new $mock_class(), $mock_class);
  47. }
  48. }
  49. function testExtensionOfCommonSplClasses() {
  50. Mock::generate('IteratorImplementation');
  51. $this->assertIsA(
  52. new IteratorImplementation(),
  53. 'IteratorImplementation');
  54. Mock::generate('IteratorAggregateImplementation');
  55. $this->assertIsA(
  56. new IteratorAggregateImplementation(),
  57. 'IteratorAggregateImplementation');
  58. }
  59. }
  60. class WithHint {
  61. function hinted(DummyInterface $object) { }
  62. }
  63. class ImplementsDummy implements DummyInterface {
  64. function aMethod() { }
  65. function anotherMethod($a) { }
  66. function &referenceMethod(&$a) { }
  67. function extraMethod($a = false) { }
  68. }
  69. Mock::generate('ImplementsDummy');
  70. class TestOfImplementations extends UnitTestCase {
  71. function testMockedInterfaceCanPassThroughTypeHint() {
  72. $mock = new MockDummyInterface();
  73. $hinter = new WithHint();
  74. $hinter->hinted($mock);
  75. }
  76. function testImplementedInterfacesAreCarried() {
  77. $mock = new MockImplementsDummy();
  78. $hinter = new WithHint();
  79. $hinter->hinted($mock);
  80. }
  81. function testNoSpuriousWarningsWhenSkippingDefaultedParameter() {
  82. $mock = new MockImplementsDummy();
  83. $mock->extraMethod();
  84. }
  85. }
  86. interface SampleClassWithConstruct {
  87. function __construct($something);
  88. }
  89. class TestOfInterfaceMocksWithConstruct extends UnitTestCase {
  90. function testBasicConstructOfAnInterface() {
  91. Mock::generate('SampleClassWithConstruct');
  92. $this->assertNoErrors();
  93. }
  94. }
  95. interface SampleInterfaceWithHintInSignature {
  96. function method(array $hinted);
  97. }
  98. class TestOfInterfaceMocksWithHintInSignature extends UnitTestCase {
  99. function testBasicConstructOfAnInterfaceWithHintInSignature() {
  100. Mock::generate('SampleInterfaceWithHintInSignature');
  101. $this->assertNoErrors();
  102. $mock = new MockSampleInterfaceWithHintInSignature();
  103. $this->assertIsA($mock, 'SampleInterfaceWithHintInSignature');
  104. }
  105. }
  106. interface SampleInterfaceWithClone {
  107. function __clone();
  108. }
  109. class TestOfSampleInterfaceWithClone extends UnitTestCase {
  110. function testCanMockWithoutErrors() {
  111. Mock::generate('SampleInterfaceWithClone');
  112. $this->assertNoErrors();
  113. }
  114. }
  115. ?>