MockBuilder.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /*
  3. * This file is part of the phpunit-mock-objects 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. use PHPUnit\Framework\TestCase;
  11. /**
  12. * Implementation of the Builder pattern for Mock objects.
  13. */
  14. class PHPUnit_Framework_MockObject_MockBuilder
  15. {
  16. /**
  17. * @var TestCase
  18. */
  19. private $testCase;
  20. /**
  21. * @var string
  22. */
  23. private $type;
  24. /**
  25. * @var array
  26. */
  27. private $methods = [];
  28. /**
  29. * @var array
  30. */
  31. private $methodsExcept = [];
  32. /**
  33. * @var string
  34. */
  35. private $mockClassName = '';
  36. /**
  37. * @var array
  38. */
  39. private $constructorArgs = [];
  40. /**
  41. * @var bool
  42. */
  43. private $originalConstructor = true;
  44. /**
  45. * @var bool
  46. */
  47. private $originalClone = true;
  48. /**
  49. * @var bool
  50. */
  51. private $autoload = true;
  52. /**
  53. * @var bool
  54. */
  55. private $cloneArguments = false;
  56. /**
  57. * @var bool
  58. */
  59. private $callOriginalMethods = false;
  60. /**
  61. * @var object
  62. */
  63. private $proxyTarget = null;
  64. /**
  65. * @var bool
  66. */
  67. private $allowMockingUnknownTypes = true;
  68. /**
  69. * @var PHPUnit_Framework_MockObject_Generator
  70. */
  71. private $generator;
  72. /**
  73. * @param TestCase $testCase
  74. * @param array|string $type
  75. */
  76. public function __construct(TestCase $testCase, $type)
  77. {
  78. $this->testCase = $testCase;
  79. $this->type = $type;
  80. $this->generator = new PHPUnit_Framework_MockObject_Generator;
  81. }
  82. /**
  83. * Creates a mock object using a fluent interface.
  84. *
  85. * @return PHPUnit_Framework_MockObject_MockObject
  86. */
  87. public function getMock()
  88. {
  89. $object = $this->generator->getMock(
  90. $this->type,
  91. $this->methods,
  92. $this->constructorArgs,
  93. $this->mockClassName,
  94. $this->originalConstructor,
  95. $this->originalClone,
  96. $this->autoload,
  97. $this->cloneArguments,
  98. $this->callOriginalMethods,
  99. $this->proxyTarget,
  100. $this->allowMockingUnknownTypes
  101. );
  102. $this->testCase->registerMockObject($object);
  103. return $object;
  104. }
  105. /**
  106. * Creates a mock object for an abstract class using a fluent interface.
  107. *
  108. * @return PHPUnit_Framework_MockObject_MockObject
  109. */
  110. public function getMockForAbstractClass()
  111. {
  112. $object = $this->generator->getMockForAbstractClass(
  113. $this->type,
  114. $this->constructorArgs,
  115. $this->mockClassName,
  116. $this->originalConstructor,
  117. $this->originalClone,
  118. $this->autoload,
  119. $this->methods,
  120. $this->cloneArguments
  121. );
  122. $this->testCase->registerMockObject($object);
  123. return $object;
  124. }
  125. /**
  126. * Creates a mock object for a trait using a fluent interface.
  127. *
  128. * @return PHPUnit_Framework_MockObject_MockObject
  129. */
  130. public function getMockForTrait()
  131. {
  132. $object = $this->generator->getMockForTrait(
  133. $this->type,
  134. $this->constructorArgs,
  135. $this->mockClassName,
  136. $this->originalConstructor,
  137. $this->originalClone,
  138. $this->autoload,
  139. $this->methods,
  140. $this->cloneArguments
  141. );
  142. $this->testCase->registerMockObject($object);
  143. return $object;
  144. }
  145. /**
  146. * Specifies the subset of methods to mock. Default is to mock all of them.
  147. *
  148. * @param array|null $methods
  149. *
  150. * @return PHPUnit_Framework_MockObject_MockBuilder
  151. */
  152. public function setMethods(array $methods = null)
  153. {
  154. $this->methods = $methods;
  155. return $this;
  156. }
  157. /**
  158. * Specifies the subset of methods to not mock. Default is to mock all of them.
  159. *
  160. * @param array $methods
  161. *
  162. * @return PHPUnit_Framework_MockObject_MockBuilder
  163. */
  164. public function setMethodsExcept(array $methods = [])
  165. {
  166. $this->methodsExcept = $methods;
  167. $this->setMethods(
  168. array_diff(
  169. $this->generator->getClassMethods($this->type),
  170. $this->methodsExcept
  171. )
  172. );
  173. return $this;
  174. }
  175. /**
  176. * Specifies the arguments for the constructor.
  177. *
  178. * @param array $args
  179. *
  180. * @return PHPUnit_Framework_MockObject_MockBuilder
  181. */
  182. public function setConstructorArgs(array $args)
  183. {
  184. $this->constructorArgs = $args;
  185. return $this;
  186. }
  187. /**
  188. * Specifies the name for the mock class.
  189. *
  190. * @param string $name
  191. *
  192. * @return PHPUnit_Framework_MockObject_MockBuilder
  193. */
  194. public function setMockClassName($name)
  195. {
  196. $this->mockClassName = $name;
  197. return $this;
  198. }
  199. /**
  200. * Disables the invocation of the original constructor.
  201. *
  202. * @return PHPUnit_Framework_MockObject_MockBuilder
  203. */
  204. public function disableOriginalConstructor()
  205. {
  206. $this->originalConstructor = false;
  207. return $this;
  208. }
  209. /**
  210. * Enables the invocation of the original constructor.
  211. *
  212. * @return PHPUnit_Framework_MockObject_MockBuilder
  213. */
  214. public function enableOriginalConstructor()
  215. {
  216. $this->originalConstructor = true;
  217. return $this;
  218. }
  219. /**
  220. * Disables the invocation of the original clone constructor.
  221. *
  222. * @return PHPUnit_Framework_MockObject_MockBuilder
  223. */
  224. public function disableOriginalClone()
  225. {
  226. $this->originalClone = false;
  227. return $this;
  228. }
  229. /**
  230. * Enables the invocation of the original clone constructor.
  231. *
  232. * @return PHPUnit_Framework_MockObject_MockBuilder
  233. */
  234. public function enableOriginalClone()
  235. {
  236. $this->originalClone = true;
  237. return $this;
  238. }
  239. /**
  240. * Disables the use of class autoloading while creating the mock object.
  241. *
  242. * @return PHPUnit_Framework_MockObject_MockBuilder
  243. */
  244. public function disableAutoload()
  245. {
  246. $this->autoload = false;
  247. return $this;
  248. }
  249. /**
  250. * Enables the use of class autoloading while creating the mock object.
  251. *
  252. * @return PHPUnit_Framework_MockObject_MockBuilder
  253. */
  254. public function enableAutoload()
  255. {
  256. $this->autoload = true;
  257. return $this;
  258. }
  259. /**
  260. * Disables the cloning of arguments passed to mocked methods.
  261. *
  262. * @return PHPUnit_Framework_MockObject_MockBuilder
  263. */
  264. public function disableArgumentCloning()
  265. {
  266. $this->cloneArguments = false;
  267. return $this;
  268. }
  269. /**
  270. * Enables the cloning of arguments passed to mocked methods.
  271. *
  272. * @return PHPUnit_Framework_MockObject_MockBuilder
  273. */
  274. public function enableArgumentCloning()
  275. {
  276. $this->cloneArguments = true;
  277. return $this;
  278. }
  279. /**
  280. * Enables the invocation of the original methods.
  281. *
  282. * @return PHPUnit_Framework_MockObject_MockBuilder
  283. */
  284. public function enableProxyingToOriginalMethods()
  285. {
  286. $this->callOriginalMethods = true;
  287. return $this;
  288. }
  289. /**
  290. * Disables the invocation of the original methods.
  291. *
  292. * @return PHPUnit_Framework_MockObject_MockBuilder
  293. */
  294. public function disableProxyingToOriginalMethods()
  295. {
  296. $this->callOriginalMethods = false;
  297. $this->proxyTarget = null;
  298. return $this;
  299. }
  300. /**
  301. * Sets the proxy target.
  302. *
  303. * @param object $object
  304. *
  305. * @return PHPUnit_Framework_MockObject_MockBuilder
  306. */
  307. public function setProxyTarget($object)
  308. {
  309. $this->proxyTarget = $object;
  310. return $this;
  311. }
  312. /**
  313. * @return PHPUnit_Framework_MockObject_MockBuilder
  314. */
  315. public function allowMockingUnknownTypes()
  316. {
  317. $this->allowMockingUnknownTypes = true;
  318. return $this;
  319. }
  320. /**
  321. * @return PHPUnit_Framework_MockObject_MockBuilder
  322. */
  323. public function disallowMockingUnknownTypes()
  324. {
  325. $this->allowMockingUnknownTypes = false;
  326. return $this;
  327. }
  328. }