PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/unit/DocBlox/TransformerTest.php

https://github.com/androa/Docblox
PHP | 348 lines | 217 code | 41 blank | 90 comment | 0 complexity | bba72ebbbf3b2d7339fbc934599179d6 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * DocBlox
  4. *
  5. * PHP Version 5
  6. *
  7. * @category DocBlox
  8. * @package Transformer
  9. * @subpackage Tests
  10. * @author Mike van Riel <mike.vanriel@naenius.com>
  11. * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT
  13. * @link http://docblox-project.org
  14. */
  15. /**
  16. * Test class for DocBlox_Transformer.
  17. *
  18. * @category DocBlox
  19. * @package Transformer
  20. * @subpackage Tests
  21. * @author Mike van Riel <mike.vanriel@naenius.com>
  22. * @license http://www.opensource.org/licenses/mit-license.php MIT
  23. * @link http://docblox-project.org
  24. */
  25. class DocBlox_TransformerTest extends PHPUnit_Framework_TestCase
  26. {
  27. /** @var DocBlox_Transformer */
  28. protected $fixture = null;
  29. /**
  30. * Instantiates a new DocBlox_Transformer for use as fixture.
  31. *
  32. * @return void
  33. */
  34. protected function setUp()
  35. {
  36. $this->fixture = new DocBlox_Transformer();
  37. }
  38. /**
  39. * Tests whether setting a target succeed.
  40. *
  41. * @return void
  42. */
  43. public function testTarget()
  44. {
  45. $this->assertEquals('', $this->fixture->getTarget());
  46. $this->fixture->setTarget(dirname(__FILE__));
  47. $this->assertEquals(dirname(__FILE__), $this->fixture->getTarget());
  48. // only directories are accepted, not files
  49. $this->setExpectedException('Exception');
  50. $this->fixture->setTarget(__FILE__);
  51. // only valid directories are accepted
  52. $this->setExpectedException('Exception');
  53. $this->fixture->setTarget(dirname(__FILE__) . 'a');
  54. }
  55. /**
  56. * Tests whether setting the source succeeds.
  57. *
  58. * @return void
  59. */
  60. public function testSource()
  61. {
  62. $this->assertEquals('', $this->fixture->getSource());
  63. file_put_contents('/tmp/test_structure.xml', '<structure></structure>');
  64. $this->fixture->setSource('/tmp/test_structure.xml');
  65. $this->assertInstanceOf('DOMDocument', $this->fixture->getSource());
  66. // directories are not allowed
  67. $this->setExpectedException('Exception');
  68. $this->fixture->setSource('/tmp');
  69. // unknown directories are not allowed
  70. $this->setExpectedException('Exception');
  71. $this->fixture->setSource('/tmpa');
  72. $this->markTestIncomplete(
  73. 'We still need to test the structure.xml changes that are induced '
  74. . 'by the addMetaDataToStructure method'
  75. );
  76. }
  77. /**
  78. * Tests whether getting and setting the template name works.
  79. *
  80. * @return void
  81. */
  82. public function testTemplate()
  83. {
  84. $this->assertSame(array('default'), $this->fixture->getTemplates());
  85. DocBlox_Core_Abstract::config()->templates->test = new Zend_Config(array());
  86. DocBlox_Core_Abstract::config()->templates->test2 = new Zend_Config(array());
  87. $this->fixture->setTemplates('test');
  88. $this->assertEquals(array('test'), $this->fixture->getTemplates());
  89. $this->fixture->setTemplates(array('test', 'test2'));
  90. $this->assertEquals(array('test', 'test2'), $this->fixture->getTemplates());
  91. }
  92. /**
  93. * Tests whether getting and setting transformations work.
  94. *
  95. * @return void
  96. */
  97. public function testTransformations()
  98. {
  99. $this->assertNotEmpty($this->fixture->getTransformations());
  100. $count = count($this->fixture->getTransformations());
  101. // test creation without parameters
  102. $this->fixture->addTransformation(
  103. array(
  104. 'query' => 'a',
  105. 'writer' => 'Xsl',
  106. 'source' => 'b',
  107. 'artifact' => 'c',
  108. )
  109. );
  110. $this->assertEquals(
  111. $count + 1,
  112. count($this->fixture->getTransformations())
  113. );
  114. $this->assertInstanceOf(
  115. 'DocBlox_Transformer_Transformation',
  116. current($this->fixture->getTransformations())
  117. );
  118. $this->assertEquals(
  119. 0,
  120. count(current($this->fixture->getTransformations())->getParameters())
  121. );
  122. // test creation with parameters
  123. $this->fixture->addTransformation(
  124. array(
  125. 'query' => 'a',
  126. 'writer' => 'Xsl',
  127. 'source' => 'b',
  128. 'artifact' => 'c',
  129. 'parameters' => array(
  130. '1' => '2'
  131. )
  132. )
  133. );
  134. $this->assertEquals($count + 2, count($this->fixture->getTransformations()));
  135. $transformations = $this->fixture->getTransformations();
  136. $this->assertEquals(1, count($transformations[$count + 1]->getParameters()));
  137. // test creation with pre-fab object
  138. $transformation = new DocBlox_Transformer_Transformation(
  139. $this->fixture, 'd', 'Xsl', 'b', 'c'
  140. );
  141. $this->fixture->addTransformation($transformation);
  142. $this->assertEquals($count + 3, count($this->fixture->getTransformations()));
  143. try
  144. {
  145. $this->fixture->addTransformation(array('a' => 'b'));
  146. $this->fail(
  147. 'Expected an exception to be thrown when '
  148. . 'supplying a bogus array'
  149. );
  150. }
  151. catch (Exception $e)
  152. {
  153. // this is good; exception is thrown
  154. }
  155. try
  156. {
  157. $this->fixture->addTransformation('a');
  158. $this->fail('Expected an exception when supplying a scalar');
  159. }
  160. catch (Exception $e)
  161. {
  162. // this is good; exception is thrown
  163. }
  164. try
  165. {
  166. $this->fixture->addTransformation(new StdClass());
  167. $this->fail('Expected an exception when supplying a false object');
  168. }
  169. catch (Exception $e)
  170. {
  171. // this is good; exception is thrown
  172. }
  173. }
  174. /**
  175. * Tests whether adding a template has the desired effect.
  176. *
  177. * @return void
  178. */
  179. public function testAddTemplate()
  180. {
  181. $this->assertNotEmpty(
  182. $this->fixture->getTransformations(),
  183. 'Initial state should not be an empty array'
  184. );
  185. $this->fixture->addTemplate('default');
  186. $this->assertGreaterThan(
  187. 0,
  188. count($this->fixture->getTransformations()),
  189. 'Transformations should be added'
  190. );
  191. try
  192. {
  193. $this->fixture->addTemplate('wargarbl');
  194. $this->fail(
  195. 'Expected an exception to be thrown when '
  196. . 'supplying a non-existent template'
  197. );
  198. }
  199. catch (InvalidArgumentException $e)
  200. {
  201. // this is good; exception is thrown
  202. }
  203. catch (Exception $e)
  204. {
  205. $this->fail(
  206. 'An unknown exception has occurred when supplying a '
  207. . 'non-existent template: ' . $e->getMessage()
  208. );
  209. }
  210. // nothing should happen when template does not contain transformations
  211. DocBlox_Core_Abstract::config()->templates->wargarbl = '';
  212. $count = count($this->fixture->getTransformations());
  213. $this->assertNull(
  214. $this->fixture->addTemplate('wargarbl'),
  215. 'No erroneous actions should happen when adding an empty template'
  216. );
  217. $this->assertEquals(
  218. $count,
  219. count($this->fixture->getTransformations()),
  220. 'Transformation count should be unchanged'
  221. );
  222. }
  223. /**
  224. * Tests whether the generateFilename method returns a file according to
  225. * the right format.
  226. *
  227. * @return void
  228. */
  229. public function testGenerateFilename()
  230. {
  231. // separate the directories with the DIRECTORY_SEPARATOR constant to
  232. // prevent failing tests on windows
  233. $filename = 'directory' . DIRECTORY_SEPARATOR . 'directory2'
  234. . DIRECTORY_SEPARATOR . 'file.php';
  235. $this->assertEquals(
  236. '_directory_directory2_file.html',
  237. $this->fixture->generateFilename($filename)
  238. );
  239. }
  240. /**
  241. * Tests whether the loading of transformation is working as expected.
  242. *
  243. * @return void
  244. */
  245. public function testLoadTransformations()
  246. {
  247. DocBlox_Core_Abstract::config()->transformations = array();
  248. $this->fixture = new DocBlox_Transformer();
  249. $this->assertEquals(
  250. 0,
  251. count($this->fixture->getTransformations()),
  252. 'A transformer should not have transformations when initialized '
  253. . 'with an empty configuration'
  254. );
  255. DocBlox_Core_Abstract::config()->transformations = array(
  256. new Zend_Config_Xml(
  257. <<<XML
  258. <?xml version="1.0"?>
  259. <transformation>
  260. <query>test1</query>
  261. <writer>Xsl</writer>
  262. <source>test3</source>
  263. <artifact>test4</artifact>
  264. </transformation>
  265. XML
  266. ));
  267. $this->fixture->loadTransformations();
  268. $this->assertEquals(
  269. 1,
  270. count($this->fixture->getTransformations()),
  271. 'When invoking the loadTransformations method with only a single '
  272. . 'transformation in the configuration there should be only 1'
  273. );
  274. // TODO: add test to check if a transformation which is passed as
  275. // argument is added correctly
  276. // TODO: add test to check if a template's transformations which is
  277. // passed as argument is added correctly
  278. $this->markTestIncomplete();
  279. }
  280. /**
  281. * Tests whether an execute call will yield the desired results.
  282. *
  283. * @return void
  284. */
  285. public function testExecute()
  286. {
  287. DocBlox_Core_Abstract::config()->transformations = array();
  288. $this->fixture = new DocBlox_Transformer();
  289. // when nothing is added; this mock should not be invoked
  290. $transformation = $this->getMock(
  291. 'DocBlox_Transformer_Transformation',
  292. array('execute'),
  293. array($this->fixture, '', 'Xsl', '', '')
  294. );
  295. $transformation->expects($this->never())->method('execute');
  296. $this->fixture->execute();
  297. // when we add this mock; we expect it to be invoked
  298. $transformation = $this->getMock(
  299. 'DocBlox_Transformer_Transformation',
  300. array('execute'),
  301. array($this->fixture, '', 'Xsl', '', '')
  302. );
  303. $transformation->expects($this->once())->method('execute');
  304. $this->fixture->addTransformation($transformation);
  305. $this->fixture->execute();
  306. }
  307. }