PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpspec/phpspec/src/PhpSpec/Wrapper/Subject.php

https://gitlab.com/judielsm/Handora
PHP | 267 lines | 119 code | 30 blank | 118 comment | 5 complexity | 7dc76852293992118e4bf82d1141f998 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of PhpSpec, A php toolset to drive emergent
  4. * design by specification.
  5. *
  6. * (c) Marcello Duarte <marcello.duarte@gmail.com>
  7. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  8. *
  9. * For the full copyright and license information, please view the LICENSE
  10. * file that was distributed with this source code.
  11. */
  12. namespace PhpSpec\Wrapper;
  13. use PhpSpec\Wrapper\Subject\WrappedObject;
  14. use PhpSpec\Wrapper\Subject\Caller;
  15. use PhpSpec\Wrapper\Subject\SubjectWithArrayAccess;
  16. use PhpSpec\Wrapper\Subject\ExpectationFactory;
  17. use PhpSpec\Util\Instantiator;
  18. use ArrayAccess;
  19. class Subject implements ArrayAccess, WrapperInterface
  20. {
  21. /**
  22. * @var mixed
  23. */
  24. private $subject;
  25. /**
  26. * @var Subject\WrappedObject
  27. */
  28. private $wrappedObject;
  29. /**
  30. * @var Subject\Caller
  31. */
  32. private $caller;
  33. /**
  34. * @var Subject\SubjectWithArrayAccess
  35. */
  36. private $arrayAccess;
  37. /**
  38. * @var Wrapper
  39. */
  40. private $wrapper;
  41. /**
  42. * @var Subject\ExpectationFactory
  43. */
  44. private $expectationFactory;
  45. /**
  46. * @param mixed $subject
  47. * @param Wrapper $wrapper
  48. * @param WrappedObject $wrappedObject
  49. * @param Caller $caller
  50. * @param SubjectWithArrayAccess $arrayAccess
  51. * @param ExpectationFactory $expectationFactory
  52. */
  53. public function __construct(
  54. $subject,
  55. Wrapper $wrapper,
  56. WrappedObject $wrappedObject,
  57. Caller $caller,
  58. SubjectWithArrayAccess $arrayAccess,
  59. ExpectationFactory $expectationFactory
  60. ) {
  61. $this->subject = $subject;
  62. $this->wrapper = $wrapper;
  63. $this->wrappedObject = $wrappedObject;
  64. $this->caller = $caller;
  65. $this->arrayAccess = $arrayAccess;
  66. $this->expectationFactory = $expectationFactory;
  67. }
  68. /**
  69. * @param string $className
  70. * @param array $arguments
  71. */
  72. public function beAnInstanceOf($className, array $arguments = array())
  73. {
  74. $this->wrappedObject->beAnInstanceOf($className, $arguments);
  75. }
  76. /**
  77. *
  78. */
  79. public function beConstructedWith()
  80. {
  81. $this->wrappedObject->beConstructedWith(func_get_args());
  82. }
  83. /**
  84. * @param array|string $factoryMethod
  85. * @param array $arguments
  86. */
  87. public function beConstructedThrough($factoryMethod, array $arguments = array())
  88. {
  89. $this->wrappedObject->beConstructedThrough($factoryMethod, $arguments);
  90. }
  91. /**
  92. * @return mixed
  93. */
  94. public function getWrappedObject()
  95. {
  96. if ($this->subject) {
  97. return $this->subject;
  98. }
  99. return $this->subject = $this->caller->getWrappedObject();
  100. }
  101. /**
  102. * @param string $method
  103. * @param array $arguments
  104. *
  105. * @return Subject
  106. */
  107. public function callOnWrappedObject($method, array $arguments = array())
  108. {
  109. return $this->caller->call($method, $arguments);
  110. }
  111. /**
  112. * @param string $property
  113. * @param mixed $value
  114. *
  115. * @return mixed
  116. */
  117. public function setToWrappedObject($property, $value = null)
  118. {
  119. return $this->caller->set($property, $value);
  120. }
  121. /**
  122. * @param string $property
  123. *
  124. * @return string|Subject
  125. */
  126. public function getFromWrappedObject($property)
  127. {
  128. return $this->caller->get($property);
  129. }
  130. /**
  131. * @param string|integer $key
  132. *
  133. * @return Subject
  134. */
  135. public function offsetExists($key)
  136. {
  137. return $this->wrap($this->arrayAccess->offSetExists($key));
  138. }
  139. /**
  140. * @param string|integer $key
  141. *
  142. * @return Subject
  143. */
  144. public function offsetGet($key)
  145. {
  146. return $this->wrap($this->arrayAccess->offsetGet($key));
  147. }
  148. /**
  149. * @param string|integer $key
  150. * @param mixed $value
  151. */
  152. public function offsetSet($key, $value)
  153. {
  154. $this->arrayAccess->offsetSet($key, $value);
  155. }
  156. /**
  157. * @param string|integer $key
  158. */
  159. public function offsetUnset($key)
  160. {
  161. $this->arrayAccess->offsetUnset($key);
  162. }
  163. /**
  164. * @param string $method
  165. * @param array $arguments
  166. *
  167. * @return mixed|Subject
  168. */
  169. public function __call($method, array $arguments = array())
  170. {
  171. if (0 === strpos($method, 'should')) {
  172. return $this->callExpectation($method, $arguments);
  173. }
  174. return $this->caller->call($method, $arguments);
  175. }
  176. /**
  177. * @return Subject
  178. */
  179. public function __invoke()
  180. {
  181. return $this->caller->call('__invoke', func_get_args());
  182. }
  183. /**
  184. * @param string $property
  185. * @param mixed $value
  186. *
  187. * @return mixed
  188. */
  189. public function __set($property, $value = null)
  190. {
  191. return $this->caller->set($property, $value);
  192. }
  193. /**
  194. * @param string $property
  195. *
  196. * @return string|Subject
  197. */
  198. public function __get($property)
  199. {
  200. return $this->caller->get($property);
  201. }
  202. /**
  203. * @param string $value
  204. *
  205. * @return Subject
  206. */
  207. private function wrap($value)
  208. {
  209. return $this->wrapper->wrap($value);
  210. }
  211. /**
  212. * @param string $method
  213. * @param array $arguments
  214. *
  215. * @return mixed
  216. */
  217. private function callExpectation($method, array $arguments)
  218. {
  219. $subject = $this->makeSureWeHaveASubject();
  220. $expectation = $this->expectationFactory->create($method, $subject, $arguments);
  221. if (0 === strpos($method, 'shouldNot')) {
  222. return $expectation->match(lcfirst(substr($method, 9)), $this, $arguments, $this->wrappedObject);
  223. }
  224. return $expectation->match(lcfirst(substr($method, 6)), $this, $arguments, $this->wrappedObject);
  225. }
  226. /**
  227. * @return object
  228. */
  229. private function makeSureWeHaveASubject()
  230. {
  231. if (null === $this->subject && $this->wrappedObject->getClassname()) {
  232. $instantiator = new Instantiator();
  233. return $instantiator->instantiate($this->wrappedObject->getClassname());
  234. }
  235. return $this->subject;
  236. }
  237. }