PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Mockery/MockTest.php

http://github.com/padraic/mockery
PHP | 231 lines | 174 code | 35 blank | 22 comment | 0 complexity | fc20eef7fc24bae1201e8a77a1d72c50 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Mockery
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://github.com/padraic/mockery/blob/master/LICENSE
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to padraic@php.net so we can send you a copy immediately.
  14. *
  15. * @category Mockery
  16. * @package Mockery
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
  19. * @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
  20. */
  21. use Mockery\Adapter\Phpunit\MockeryTestCase;
  22. use Mockery\Mock;
  23. class Mockery_MockTest extends MockeryTestCase
  24. {
  25. public function testAnonymousMockWorksWithNotAllowingMockingOfNonExistentMethods()
  26. {
  27. \Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
  28. $m = mock();
  29. $m->shouldReceive("test123")->andReturn(true);
  30. assertThat($m->test123(), equalTo(true));
  31. \Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
  32. }
  33. public function testMockWithNotAllowingMockingOfNonExistentMethodsCanBeGivenAdditionalMethodsToMockEvenIfTheyDontExistOnClass()
  34. {
  35. \Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
  36. $m = mock('ExampleClassForTestingNonExistentMethod');
  37. $m->shouldAllowMockingMethod('testSomeNonExistentMethod');
  38. $m->shouldReceive("testSomeNonExistentMethod")->andReturn(true);
  39. assertThat($m->testSomeNonExistentMethod(), equalTo(true));
  40. \Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
  41. }
  42. public function testProtectedMethodMockWithNotAllowingMockingOfNonExistentMethodsWhenShouldAllowMockingProtectedMethodsIsCalled()
  43. {
  44. \Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
  45. $m = mock('ClassWithProtectedMethod');
  46. $m->shouldAllowMockingProtectedMethods();
  47. $m->shouldReceive('foo')->andReturn(true);
  48. assertThat($m->foo(), equalTo(true));
  49. \Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
  50. }
  51. public function testShouldAllowMockingMethodReturnsMockInstance()
  52. {
  53. $m = Mockery::mock('someClass');
  54. $this->assertInstanceOf('Mockery\MockInterface', $m->shouldAllowMockingMethod('testFunction'));
  55. }
  56. public function testShouldAllowMockingProtectedMethodReturnsMockInstance()
  57. {
  58. $m = Mockery::mock('someClass');
  59. $this->assertInstanceOf('Mockery\MockInterface', $m->shouldAllowMockingProtectedMethods('testFunction'));
  60. }
  61. public function testMockAddsToString()
  62. {
  63. $mock = mock('ClassWithNoToString');
  64. $this->assertTrue(method_exists($mock, '__toString'));
  65. }
  66. public function testMockToStringMayBeDeferred()
  67. {
  68. $mock = mock('ClassWithToString')->makePartial();
  69. $this->assertEquals("foo", (string)$mock);
  70. }
  71. public function testMockToStringShouldIgnoreMissingAlwaysReturnsString()
  72. {
  73. $mock = mock('ClassWithNoToString')->shouldIgnoreMissing();
  74. $this->assertNotEquals('', (string)$mock);
  75. $mock->asUndefined();
  76. $this->assertNotEquals('', (string)$mock);
  77. }
  78. public function testShouldIgnoreMissing()
  79. {
  80. $mock = mock('ClassWithNoToString')->shouldIgnoreMissing();
  81. $this->assertNull($mock->nonExistingMethod());
  82. }
  83. public function testShouldIgnoreMissingDisallowMockingNonExistentMethodsUsingGlobalConfiguration()
  84. {
  85. Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
  86. $mock = mock('ClassWithMethods')->shouldIgnoreMissing();
  87. $this->expectException(\Mockery\Exception::class);
  88. $mock->shouldReceive('nonExistentMethod');
  89. }
  90. public function testShouldIgnoreMissingCallingNonExistentMethodsUsingGlobalConfiguration()
  91. {
  92. Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
  93. $mock = mock('ClassWithMethods')->shouldIgnoreMissing();
  94. $this->expectException(\BadMethodCallException::class);
  95. $mock->nonExistentMethod();
  96. }
  97. public function testShouldIgnoreMissingCallingExistentMethods()
  98. {
  99. Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
  100. $mock = mock('ClassWithMethods')->shouldIgnoreMissing();
  101. assertThat(nullValue($mock->foo()));
  102. $mock->shouldReceive('bar')->passthru();
  103. assertThat($mock->bar(), equalTo('bar'));
  104. }
  105. public function testShouldIgnoreMissingCallingNonExistentMethods()
  106. {
  107. Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
  108. $mock = mock('ClassWithMethods')->shouldIgnoreMissing();
  109. assertThat(nullValue($mock->foo()));
  110. assertThat(nullValue($mock->bar()));
  111. assertThat(nullValue($mock->nonExistentMethod()));
  112. $mock->shouldReceive(array('foo' => 'new_foo', 'nonExistentMethod' => 'result'));
  113. $mock->shouldReceive('bar')->passthru();
  114. assertThat($mock->foo(), equalTo('new_foo'));
  115. assertThat($mock->bar(), equalTo('bar'));
  116. assertThat($mock->nonExistentMethod(), equalTo('result'));
  117. }
  118. public function testCanMockException()
  119. {
  120. $exception = Mockery::mock('Exception');
  121. $this->assertInstanceOf('Exception', $exception);
  122. }
  123. public function testCanMockSubclassOfException()
  124. {
  125. $errorException = Mockery::mock('ErrorException');
  126. $this->assertInstanceOf('ErrorException', $errorException);
  127. $this->assertInstanceOf('Exception', $errorException);
  128. }
  129. public function testCallingShouldReceiveWithoutAValidMethodName()
  130. {
  131. $mock = Mockery::mock();
  132. $this->expectException("InvalidArgumentException", "Received empty method name");
  133. $mock->shouldReceive("");
  134. }
  135. public function testShouldThrowExceptionWithInvalidClassName()
  136. {
  137. $this->expectException(\Mockery\Exception::class);
  138. mock('ClassName.CannotContainDot');
  139. }
  140. /** @test */
  141. public function expectation_count_will_count_expectations()
  142. {
  143. $mock = new Mock();
  144. $mock->shouldReceive("doThis")->once();
  145. $mock->shouldReceive("doThat")->once();
  146. $this->assertEquals(2, $mock->mockery_getExpectationCount());
  147. }
  148. /** @test */
  149. public function expectation_count_will_ignore_defaults_if_overriden()
  150. {
  151. $mock = new Mock();
  152. $mock->shouldReceive("doThis")->once()->byDefault();
  153. $mock->shouldReceive("doThis")->twice();
  154. $mock->shouldReceive("andThis")->twice();
  155. $this->assertEquals(2, $mock->mockery_getExpectationCount());
  156. }
  157. /** @test */
  158. public function expectation_count_will_count_defaults_if_not_overriden()
  159. {
  160. $mock = new Mock();
  161. $mock->shouldReceive("doThis")->once()->byDefault();
  162. $mock->shouldReceive("doThat")->once()->byDefault();
  163. $this->assertEquals(2, $mock->mockery_getExpectationCount());
  164. }
  165. }
  166. class ExampleClassForTestingNonExistentMethod
  167. {
  168. }
  169. class ClassWithToString
  170. {
  171. public function __toString()
  172. {
  173. return 'foo';
  174. }
  175. }
  176. class ClassWithNoToString
  177. {
  178. }
  179. class ClassWithMethods
  180. {
  181. public function foo()
  182. {
  183. return 'foo';
  184. }
  185. public function bar()
  186. {
  187. return 'bar';
  188. }
  189. }
  190. class ClassWithProtectedMethod
  191. {
  192. protected function foo()
  193. {
  194. return 'foo';
  195. }
  196. }