PageRenderTime 55ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/PHPSpec/PHPSpec.php

http://github.com/phpspec/phpspec
PHP | 383 lines | 176 code | 33 blank | 174 comment | 10 complexity | c70bb5b83f1ea2da46b52521da1dee8d MD5 | raw file
  1. <?php
  2. /**
  3. * PHPSpec
  4. *
  5. * LICENSE
  6. *
  7. * This file is subject to the GNU Lesser General Public License Version 3
  8. * that is bundled with this package in the file LICENSE.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.gnu.org/licenses/lgpl-3.0.txt
  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 license@phpspec.net so we can send you a copy immediately.
  14. *
  15. * @category PHPSpec
  16. * @package PHPSpec
  17. * @copyright Copyright (c) 2007-2009 P??draic Brady, Travis Swicegood
  18. * @copyright Copyright (c) 2010-2012 P??draic Brady, Travis Swicegood,
  19. * Marcello Duarte
  20. * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public Licence Version 3
  21. */
  22. namespace PHPSpec;
  23. use \PHPSpec\Runner\Runner,
  24. \PHPSpec\Runner\Reporter,
  25. \PHPSpec\Runner\ReporterEvent,
  26. \PHPSpec\Runner\Parser,
  27. \PHPSpec\World,
  28. \PHPSpec\Runner\Cli\Parser as CliParser,
  29. \PHPSpec\Runner\Cli\Runner as CliRunner,
  30. \PHPSpec\Runner\Cli\Reporter as CliReporter,
  31. \PHPSpec\Runner\Cli\Configuration,
  32. \PHPSpec\Runner\Formatter\Factory as FormatterFactory,
  33. \PHPSpec\Runner\Formatter;
  34. /**
  35. * @category PHPSpec
  36. * @package PHPSpec
  37. * @copyright Copyright (c) 2007-2009 P??draic Brady, Travis Swicegood
  38. * @copyright Copyright (c) 2010-2012 P??draic Brady, Travis Swicegood,
  39. * Marcello Duarte
  40. * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public Licence Version 3
  41. */
  42. class PHPSpec
  43. {
  44. /**
  45. * Raw arguments from argv
  46. *
  47. * @var array
  48. */
  49. protected $_arguments;
  50. /**
  51. * The parser
  52. *
  53. * @var \PHPSpec\Runner\Parser
  54. */
  55. protected $_parser;
  56. /**
  57. * The reporter object
  58. *
  59. * @var \PHPSpec\Runner\Reporter
  60. */
  61. protected $_reporter;
  62. /**
  63. * The runner object
  64. *
  65. * @var \PHPSpec\Runner\Runner
  66. */
  67. protected $_runner;
  68. /**
  69. * Keeps the environment data, mainly options and reporter
  70. *
  71. * @var \PHPSpec\World
  72. */
  73. protected $_world;
  74. /**
  75. * The formatter factory
  76. *
  77. * @var \PHPSpec\Runner\Formatter\Factory
  78. */
  79. protected $_formatterFactory;
  80. /**
  81. * The configuration
  82. *
  83. * @var \PHPSpec\Runner\Cli\Configuration
  84. */
  85. protected $_configuration;
  86. /**
  87. * Whether we are testing PHPSpec itself
  88. *
  89. * @var bool
  90. */
  91. protected static $_testingPHPSpec = false;
  92. /**
  93. * PHPSpec is constructed with arguments
  94. *
  95. * @param array $argv
  96. */
  97. public function __construct(array $argv = array())
  98. {
  99. $this->_arguments = $argv;
  100. list (
  101. $this->_parser, $this->_runner, $this->_reporter, $this->_world
  102. ) = $this->inlineFactory(
  103. array(
  104. 'Parser', 'Runner', 'Reporter', 'World'
  105. )
  106. );
  107. }
  108. /**
  109. * Executes PHPSpec and outputs the result
  110. */
  111. public function execute()
  112. {
  113. try {
  114. $this->loadAndRun();
  115. } catch (\PHPSpec\Runner\Error $e) {
  116. $this->_reporter->setMessage($e->getMessage());
  117. }
  118. $this->output();
  119. }
  120. /**
  121. * Loads options with the parser into the world. If there are no options
  122. * then sends message to show usage
  123. */
  124. protected function loadAndRun()
  125. {
  126. $options = $this->parseOptionsAndSetWorld();
  127. $this->setFormatter($this->_world);
  128. if ($options !== null) {
  129. $this->setDefaultBootstrap($this->_world);
  130. $this->_runner->run($this->_world);
  131. } else {
  132. $this->showUsage();
  133. }
  134. }
  135. /**
  136. * Sends message to Formatter so it starts output
  137. */
  138. protected function output()
  139. {
  140. $this->makeSureWeHaveAFormatter();
  141. $this->_reporter->notify(new ReporterEvent('exit', '', ''));
  142. }
  143. /**
  144. * Parses options into World
  145. *
  146. * @return array|null
  147. */
  148. protected function parseOptionsAndSetWorld()
  149. {
  150. $this->_world->setReporter($this->_reporter);
  151. $configOptions = $this->getConfiguration()->load();
  152. $arguments = array_merge($this->_arguments, $configOptions);
  153. $options = $this->getParser()->parse($arguments);
  154. $this->_world->setOptions($options);
  155. return $options;
  156. }
  157. /**
  158. * Looks for a SpecHelper.php in case the bootstrap option is empty and
  159. * add that to world's options
  160. *
  161. * @param World $world
  162. */
  163. protected function setDefaultBootstrap(World $world)
  164. {
  165. $specHelper = $world->getOption('specFile') . DIRECTORY_SEPARATOR .
  166. 'SpecHelper.php';
  167. if (!$world->getOption('bootstrap') &&
  168. is_dir($world->getOption('specFile')) &&
  169. file_exists($specHelper)) {
  170. $world->setOption('bootstrap', $specHelper);
  171. }
  172. }
  173. /**
  174. * Asserts we have a formatter and create one if we don't
  175. */
  176. private function makeSureWeHaveAFormatter()
  177. {
  178. if (!count($this->_reporter->getFormatters())) {
  179. $this->_world->setOptions(array('formatter' => 'p'));
  180. $this->setFormatter();
  181. }
  182. }
  183. /**
  184. * Sends a message to the reporter to show message
  185. */
  186. protected function showUsage()
  187. {
  188. $this->_reporter->setMessage($this->_runner->getUsage());
  189. }
  190. /**
  191. * Gets the parser
  192. *
  193. * @return \PHPSpec\Runner\Parser
  194. */
  195. public function getParser()
  196. {
  197. if ($this->_parser === null) {
  198. $this->_parser = new CliParser;
  199. }
  200. return $this->_parser;
  201. }
  202. /**
  203. * Gets the reporter
  204. *
  205. * @return \PHPSpec\Runner\Reporter
  206. */
  207. public function getReporter()
  208. {
  209. if ($this->_reporter === null) {
  210. $this->_reporter = new CliReporter;
  211. }
  212. return $this->_reporter;
  213. }
  214. /**
  215. * Gets the runner
  216. *
  217. * @return \PHPSpec\Runner\Runner
  218. */
  219. public function getRunner()
  220. {
  221. if ($this->_runner === null) {
  222. $this->_runner = new CliRunner;
  223. }
  224. return $this->_runner;
  225. }
  226. /**
  227. * Gets the workd
  228. *
  229. * @return \PHPSpec\World
  230. */
  231. public function getWorld()
  232. {
  233. if ($this->_world === null) {
  234. $this->_world = new World;
  235. }
  236. return $this->_world;
  237. }
  238. /**
  239. * Gets the configuration
  240. *
  241. * @return \PHPSpec\Runner\Cli\Configuration
  242. */
  243. public function getConfiguration()
  244. {
  245. if ($this->_configuration === null) {
  246. $this->_configuration = new Configuration;
  247. }
  248. return $this->_configuration;
  249. }
  250. /**
  251. * Sets the parser
  252. *
  253. * @param \PHPSpec\Runner\Parser $parser
  254. */
  255. public function setParser(Parser $parser)
  256. {
  257. $this->_parser = $parser;
  258. }
  259. /**
  260. * Sets the reporter
  261. *
  262. * @param \PHPSpec\Runner\Reporter $reporter
  263. */
  264. public function setReporter(Reporter $reporter)
  265. {
  266. $this->_reporter = $reporter;
  267. }
  268. /**
  269. * Sets the runner
  270. *
  271. * @param \PHPSpec\Runner\Runner $runner
  272. */
  273. public function setRunner(Runner $runner)
  274. {
  275. $this->_runner = $runner;
  276. }
  277. /**
  278. * Sets the environment
  279. *
  280. * @param \PHPSpec\World $world
  281. */
  282. public function setWorld(World $world)
  283. {
  284. $this->_world = $world;
  285. }
  286. /**
  287. * Gets the formatter from world and register it into the reporter
  288. */
  289. protected function setFormatter()
  290. {
  291. $formatterOption = $this->_world->getOption('formatter');
  292. $formatter = $this->getFormatterFactory()->create(
  293. $formatterOption, $this->_world->getReporter()
  294. );
  295. $this->_world->getReporter()->addFormatter($formatter);
  296. }
  297. /**
  298. * Gets the formatter factory
  299. *
  300. * @return \PHPSpec\Runner\Formatter\Factory
  301. */
  302. public function getFormatterFactory()
  303. {
  304. if ($this->_formatterFactory === null) {
  305. $this->_formatterFactory = new FormatterFactory;
  306. }
  307. return $this->_formatterFactory;
  308. }
  309. /**
  310. * Sets the formatter factory
  311. *
  312. * @param \PHPSpec\Runner\Formatter\Factory $factory
  313. */
  314. public function setFormatterFactory(FormatterFactory $factory)
  315. {
  316. $this->_formatterFactory = $factory;
  317. }
  318. /**
  319. * Inline factory pattern
  320. *
  321. * @param array $classes
  322. * @return array
  323. */
  324. protected function inlineFactory(array $classes)
  325. {
  326. $objects = array();
  327. foreach ($classes as $class) {
  328. $objects[] = $this->{"get$class"}();
  329. }
  330. return $objects;
  331. }
  332. /**
  333. * Whether we are testing PHPSpec itself
  334. *
  335. * @return boolean
  336. */
  337. public static function testingPHPSpec()
  338. {
  339. return self::$_testingPHPSpec;
  340. }
  341. /**
  342. * Sets the testing PHPSpec flag
  343. *
  344. * @param boolean $flag
  345. */
  346. public static function setTestingPHPSpec($flag = true)
  347. {
  348. self::$_testingPHPSpec = $flag;
  349. }
  350. }