/tests/units/classes/template/parser.php

https://github.com/Yacodo/atoum · PHP · 699 lines · 582 code · 117 blank · 0 comment · 0 complexity · 93e73fc85b30ae8b57151252e10bb3d0 MD5 · raw file

  1. <?php
  2. namespace mageekguy\atoum\tests\units\template;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\test,
  6. mageekguy\atoum\template
  7. ;
  8. require_once __DIR__ . '/../../runner.php';
  9. class parser extends atoum\test
  10. {
  11. public function testClass()
  12. {
  13. $this->assert
  14. ->testedClass->hasInterface('mageekguy\atoum\adapter\aggregator')
  15. ;
  16. }
  17. public function test__construct()
  18. {
  19. $parser = new template\parser();
  20. $this->assert
  21. ->string($parser->getNamespace())->isEqualTo(template\parser::defaultNamespace)
  22. ->object($parser->getAdapter())->isInstanceOf('mageekguy\atoum\adapter')
  23. ;
  24. $parser = new template\parser($namespace = uniqid());
  25. $this->assert
  26. ->string($namespace)->isEqualTo($parser->getNamespace())
  27. ->object($parser->getAdapter())->isInstanceOf('mageekguy\atoum\adapter')
  28. ;
  29. $parser = new template\parser($namespace = rand(1, PHP_INT_MAX));
  30. $this->assert
  31. ->string($parser->getNamespace())->isEqualTo((string) $namespace)
  32. ->object($parser->getAdapter())->isInstanceOf('mageekguy\atoum\adapter')
  33. ;
  34. $parser = new template\parser($namespace = uniqid(), $adapter = new atoum\test\adapter());
  35. $this->assert
  36. ->string($parser->getNamespace())->isEqualTo((string) $namespace)
  37. ->object($parser->getAdapter())->isIdenticalTo($adapter)
  38. ;
  39. }
  40. public function testSetNamespace()
  41. {
  42. $parser = new template\parser();
  43. $this->assert
  44. ->object($parser->setNamespace($namespace = uniqid()))->isIdenticalTo($parser)
  45. ->string($parser->getNamespace())->isEqualTo($namespace)
  46. ;
  47. $this->assert
  48. ->object($parser->setNamespace($namespace = rand(1, PHP_INT_MAX)))->isIdenticalTo($parser)
  49. ->string($parser->getNamespace())->isEqualTo((string) $namespace)
  50. ;
  51. }
  52. public function testSetAdapter()
  53. {
  54. $parser = new template\parser();
  55. $this->assert
  56. ->object($parser->setAdapter($adapter = new atoum\test\adapter()))->isIdenticalTo($parser)
  57. ->object($parser->getAdapter())->isIdenticalTo($adapter)
  58. ;
  59. }
  60. public function testCheckString()
  61. {
  62. $this->define->parserException = '\mageekguy\atoum\tests\units\asserters\template\parser\exception';
  63. $parser = new template\parser();
  64. $this->assert
  65. ->object($parser->checkString(uniqid()))->isIdenticalTo($parser)
  66. ->object($parser->checkString('<' . uniqid() . ':' . uniqid() . ' />'))->isIdenticalTo($parser)
  67. ;
  68. $tag = uniqid();
  69. $this->assert
  70. ->object($parser->checkString('<'))->isIdenticalTo($parser)
  71. ->object($parser->checkString('<' . template\parser::defaultNamespace))->isIdenticalTo($parser)
  72. ->object($parser->checkString('<' . template\parser::defaultNamespace . ':'))->isIdenticalTo($parser)
  73. ->object($parser->checkString('<' . template\parser::defaultNamespace . ':' . $tag))->isIdenticalTo($parser)
  74. ->object($parser->checkString('<' . template\parser::defaultNamespace . ':' . $tag . '/>'))->isIdenticalTo($parser)
  75. ->parserException(function() use ($parser, $tag) {
  76. $parser->checkString('<' . template\parser::defaultNamespace . ':' . $tag . ' id="" />');
  77. }
  78. )
  79. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  80. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  81. ->hasMessage('Id must not be empty')
  82. ->hasErrorLine(1)
  83. ->hasErrorOffset(1)
  84. ->object($parser->checkString('<' . template\parser::defaultNamespace . ':' . $tag . ' id="' . uniqid() . '" />'))->isIdenticalTo($parser)
  85. ->object($parser->checkString('<' . template\parser::defaultNamespace . ':' . $tag . '></' . template\parser::defaultNamespace . ':' . $tag . '>'))->isIdenticalTo($parser)
  86. ->object($parser->checkString('<' . template\parser::defaultNamespace . ':' . $tag . ' ' . "\t" . ' ></' . template\parser::defaultNamespace . ':' . $tag . '>'))->isIdenticalTo($parser)
  87. ->object($parser->checkString('<' . template\parser::defaultNamespace . ':' . $tag . '></' . template\parser::defaultNamespace . ':' . $tag . ' ' . "\t" . ' >'))->isIdenticalTo($parser)
  88. ;
  89. $tagWithId = '<' . template\parser::defaultNamespace . ':' . $tag . ' id="' . ($id = uniqid()) . '" />';
  90. $this->assert
  91. ->parserException(function() use ($parser, $tagWithId) {
  92. $parser->checkString($tagWithId . $tagWithId);
  93. }
  94. )
  95. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  96. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  97. ->hasMessage('Id \'' . $id . '\' is already defined in line 1 at offset 1')
  98. ->hasErrorLine(1)
  99. ->hasErrorOffset(41)
  100. ;
  101. $tagWithInvalidAttribute = '<' . template\parser::defaultNamespace . ':' . $tag . ' foo="bar" />';
  102. $this->assert
  103. ->parserException(function() use ($parser, $tagWithInvalidAttribute) {
  104. $parser->checkString($tagWithInvalidAttribute);
  105. }
  106. )
  107. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  108. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  109. ->hasMessage('Attribute \'foo\' is unknown')
  110. ->hasErrorLine(1)
  111. ->hasErrorOffset(1)
  112. ;
  113. $firstTag = '<' . template\parser::defaultNamespace . ':' . $tag . '>';
  114. $this->assert
  115. ->parserException(function() use ($parser, $firstTag) {
  116. $parser->checkString($firstTag);
  117. }
  118. )
  119. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  120. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  121. ->hasMessage('Tag \'' . $tag . '\' must be closed')
  122. ->hasErrorLine(1)
  123. ->hasErrorOffset(strlen($firstTag) + 1)
  124. ;
  125. $firstTag = '<' . template\parser::defaultNamespace . ':' . $tag . '>';
  126. $eols = str_repeat("\n", rand(1, 10));
  127. $this->assert
  128. ->parserException(function() use ($parser, $firstTag, $eols) {
  129. $parser->checkString($eols . $firstTag);
  130. }
  131. )
  132. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  133. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  134. ->hasMessage('Tag \'' . $tag . '\' must be closed')
  135. ->hasErrorLine(strlen($eols) + 1)
  136. ->hasErrorOffset(strlen($firstTag) + 1)
  137. ;
  138. $spaces = str_repeat(' ', rand(1, 10));
  139. $this->assert
  140. ->parserException(function() use ($parser, $firstTag, $eols, $spaces) {
  141. $parser->checkString($eols . $spaces . $firstTag);
  142. }
  143. )
  144. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  145. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  146. ->hasMessage('Tag \'' . $tag . '\' must be closed')
  147. ->hasErrorLine(strlen($eols) + 1)
  148. ->hasErrorOffset(strlen($firstTag) + strlen($spaces) + 1)
  149. ;
  150. $firstTag = '<' . template\parser::defaultNamespace . ':' . $tag . '>';
  151. $secondTag = '</' . template\parser::defaultNamespace . ':' . ($notOpenTag = uniqid()) . ' ' . "\t" . ' >';
  152. $this->assert
  153. ->parserException(function() use ($parser, $firstTag, $secondTag) {
  154. $parser->checkString($firstTag . $secondTag);
  155. }
  156. )
  157. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  158. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  159. ->hasMessage('Tag \'' . $notOpenTag . '\' is not open')
  160. ->hasErrorLine(1)
  161. ->hasErrorOffset(strlen($firstTag) + 1)
  162. ->parserException(function() use ($parser, $firstTag, $secondTag, $eols) {
  163. $parser->checkString($eols . $firstTag . $secondTag);
  164. }
  165. )
  166. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  167. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  168. ->hasMessage('Tag \'' . $notOpenTag . '\' is not open')
  169. ->hasErrorLine(strlen($eols) + 1)
  170. ->hasErrorOffset(strlen($firstTag) + 1)
  171. ->parserException(function() use ($parser, $firstTag, $secondTag, $eols, $spaces) {
  172. $parser->checkString($eols . $spaces . $firstTag . $secondTag);
  173. }
  174. )
  175. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  176. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  177. ->hasMessage('Tag \'' . $notOpenTag . '\' is not open')
  178. ->hasErrorLine(strlen($eols) + 1)
  179. ->hasErrorOffset(strlen($firstTag) + strlen($spaces) + 1)
  180. ;
  181. }
  182. public function testCheckFile()
  183. {
  184. $this->define->parserException = '\mageekguy\atoum\tests\units\asserters\template\parser\exception';
  185. $parser = new template\parser(null, $adapter = new test\adapter());
  186. $adapter->file_get_contents = false;
  187. $this->assert
  188. ->exception(function() use ($parser, & $path) {
  189. $parser->checkFile($path = uniqid());
  190. }
  191. )
  192. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  193. ->hasMessage('Unable to get contents from file \'' . $path . '\'')
  194. ;
  195. $adapter->file_get_contents = '<' . uniqid() . ':' . uniqid() . ' />';
  196. $this->assert
  197. ->object($parser->checkFile(uniqid()))->isIdenticalTo($parser)
  198. ;
  199. $adapter->file_get_contents = '<';
  200. $this->assert
  201. ->object($parser->checkFile(uniqid()))->isIdenticalTo($parser)
  202. ;
  203. $adapter->file_get_contents = '<' . template\parser::defaultNamespace;
  204. $this->assert
  205. ->object($parser->checkFile(uniqid()))->isIdenticalTo($parser)
  206. ;
  207. $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':';
  208. $this->assert
  209. ->object($parser->checkFile(uniqid()))->isIdenticalTo($parser)
  210. ;
  211. $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid());
  212. $this->assert
  213. ->object($parser->checkFile(uniqid()))->isIdenticalTo($parser)
  214. ;
  215. $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . '/>';
  216. $this->assert
  217. ->object($parser->checkFile(uniqid()))->isIdenticalTo($parser)
  218. ;
  219. $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . ' id="" />';
  220. $this->assert
  221. ->parserException(function() use ($parser, $tag) {
  222. $parser->checkFile(uniqid());
  223. }
  224. )
  225. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  226. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  227. ->hasMessage('Id must not be empty')
  228. ->hasErrorLine(1)
  229. ->hasErrorOffset(1)
  230. ;
  231. $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . $tag . ' id="' . uniqid() . '" />';
  232. $this->assert
  233. ->object($parser->checkFile(uniqid()))->isIdenticalTo($parser)
  234. ;
  235. $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . $tag . '></' . template\parser::defaultNamespace . ':' . $tag . '>';
  236. $this->assert
  237. ->object($parser->checkFile(uniqid()))->isIdenticalTo($parser)
  238. ;
  239. $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . $tag . ' ' . "\t" . ' ></' . template\parser::defaultNamespace . ':' . $tag . '>';
  240. $this->assert
  241. ->object($parser->checkFile(uniqid()))->isIdenticalTo($parser)
  242. ;
  243. $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . $tag . '></' . template\parser::defaultNamespace . ':' . $tag . ' ' . "\t" . ' >';
  244. $this->assert
  245. ->object($parser->checkFile(uniqid()))->isIdenticalTo($parser)
  246. ;
  247. $tagWithId = '<' . template\parser::defaultNamespace . ':' . $tag . ' id="' . ($id = uniqid()) . '" />';
  248. $adapter->file_get_contents = $tagWithId . $tagWithId;
  249. $this->assert
  250. ->parserException(function() use ($parser, $tagWithId) {
  251. $parser->checkFile(uniqid());
  252. }
  253. )
  254. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  255. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  256. ->hasMessage('Id \'' . $id . '\' is already defined in line 1 at offset 1')
  257. ->hasErrorLine(1)
  258. ->hasErrorOffset(41)
  259. ;
  260. $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . $tag . ' foo="bar" />';
  261. $this->assert
  262. ->parserException(function() use ($parser) {
  263. $parser->checkFile(uniqid());
  264. }
  265. )
  266. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  267. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  268. ->hasMessage('Attribute \'foo\' is unknown')
  269. ->hasErrorLine(1)
  270. ->hasErrorOffset(1)
  271. ;
  272. $adapter->file_get_contents = $firstTag = '<' . template\parser::defaultNamespace . ':' . $tag . '>';
  273. $this->assert
  274. ->parserException(function() use ($parser) {
  275. $parser->checkFile(uniqid());
  276. }
  277. )
  278. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  279. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  280. ->hasMessage('Tag \'' . $tag . '\' must be closed')
  281. ->hasErrorLine(1)
  282. ->hasErrorOffset(strlen($firstTag) + 1)
  283. ;
  284. $adapter->file_get_contents = ($eols = str_repeat("\n", rand(1, 10))) . ($firstTag = '<' . template\parser::defaultNamespace . ':' . $tag . '>');
  285. $this->assert
  286. ->parserException(function() use ($parser) {
  287. $parser->checkFile(uniqid());
  288. }
  289. )
  290. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  291. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  292. ->hasMessage('Tag \'' . $tag . '\' must be closed')
  293. ->hasErrorLine(strlen($eols) + 1)
  294. ->hasErrorOffset(strlen($firstTag) + 1)
  295. ;
  296. $adapter->file_get_contents = ($eols = str_repeat("\n", rand(1, 10))) . ($spaces = str_repeat(' ', rand(1, 10))) . ($firstTag = '<' . template\parser::defaultNamespace . ':' . $tag . '>');
  297. $this->assert
  298. ->parserException(function() use ($parser) {
  299. $parser->checkFile(uniqid());
  300. }
  301. )
  302. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  303. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  304. ->hasMessage('Tag \'' . $tag . '\' must be closed')
  305. ->hasErrorLine(strlen($eols) + 1)
  306. ->hasErrorOffset(strlen($firstTag) + strlen($spaces) + 1)
  307. ;
  308. $firstTag = '<' . template\parser::defaultNamespace . ':' . $tag . '>';
  309. $secondTag = '</' . template\parser::defaultNamespace . ':' . ($notOpenTag = uniqid()) . ' ' . "\t" . ' >';
  310. $adapter->file_get_contents = $firstTag . $secondTag;
  311. $this->assert
  312. ->parserException(function() use ($parser) {
  313. $parser->checkFile(uniqid());
  314. }
  315. )
  316. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  317. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  318. ->hasMessage('Tag \'' . $notOpenTag . '\' is not open')
  319. ->hasErrorLine(1)
  320. ->hasErrorOffset(strlen($firstTag) + 1)
  321. ;
  322. $adapter->file_get_contents = $eols . $firstTag . $secondTag;
  323. $this->assert
  324. ->parserException(function() use ($parser) {
  325. $parser->checkFile(uniqid());
  326. }
  327. )
  328. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  329. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  330. ->hasMessage('Tag \'' . $notOpenTag . '\' is not open')
  331. ->hasErrorLine(strlen($eols) + 1)
  332. ->hasErrorOffset(strlen($firstTag) + 1)
  333. ;
  334. $adapter->file_get_contents = $eols . $spaces . $firstTag . $secondTag;
  335. $this->assert
  336. ->parserException(function() use ($parser, $firstTag, $secondTag, $eols, $spaces) {
  337. $parser->checkFile($eols . $spaces . $firstTag . $secondTag);
  338. }
  339. )
  340. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  341. ->isInstanceOf('mageekguy\atoum\template\parser\exception')
  342. ->hasMessage('Tag \'' . $notOpenTag . '\' is not open')
  343. ->hasErrorLine(strlen($eols) + 1)
  344. ->hasErrorOffset(strlen($firstTag) + strlen($spaces) + 1)
  345. ;
  346. }
  347. public function testParseString()
  348. {
  349. $parser = new template\parser();
  350. $this->assert
  351. ->object($root = $parser->parseString(''))->isInstanceOf('mageekguy\atoum\template')
  352. ->array($root->getChildren())->isEmpty()
  353. ;
  354. $this->assert
  355. ->object($root = $parser->parseString($string = uniqid()))->isInstanceOf('mageekguy\atoum\template')
  356. ->array($root->getChildren())->hasSize(1)
  357. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\data')
  358. ->string($root->getChild(0)->getData())->isEqualTo($string)
  359. ;
  360. $this->assert
  361. ->object($root = $parser->parseString($string = uniqid() . "\n" . uniqid() . "\n"))->isInstanceOf('mageekguy\atoum\template')
  362. ->array($root->getChildren())->hasSize(1)
  363. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\data')
  364. ->string($root->getChild(0)->getData())->isEqualTo($string)
  365. ;
  366. $this->assert
  367. ->object($root = $parser->parseString('<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . ' />'))->isInstanceOf('mageekguy\atoum\template')
  368. ->array($root->getChildren())->hasSize(1)
  369. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\tag')
  370. ->string($root->getChild(0)->getTag())->isEqualTo($tag)
  371. ->variable($root->getChild(0)->getId())->isNull()
  372. ->string($root->getChild(0)->getData())->isEmpty()
  373. ->integer($root->getChild(0)->getLine())->isEqualTo(1)
  374. ->integer($root->getChild(0)->getOffset())->isEqualTo(1)
  375. ;
  376. $this->assert
  377. ->object($root = $parser->parseString('<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . '/>'))->isInstanceOf('mageekguy\atoum\template')
  378. ->array($root->getChildren())->hasSize(1)
  379. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\tag')
  380. ->string($root->getChild(0)->getTag())->isEqualTo($tag)
  381. ->variable($root->getChild(0)->getId())->isNull()
  382. ->string($root->getChild(0)->getData())->isEmpty()
  383. ->integer($root->getChild(0)->getLine())->isEqualTo(1)
  384. ->integer($root->getChild(0)->getOffset())->isEqualTo(1)
  385. ;
  386. $this->assert
  387. ->object($root = $parser->parseString('<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . ' id="' . ($id = uniqid()) . '" />'))->isInstanceOf('mageekguy\atoum\template')
  388. ->array($root->getChildren())->hasSize(1)
  389. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\tag')
  390. ->string($root->getChild(0)->getTag())->isEqualTo($tag)
  391. ->string($root->getChild(0)->getId())->isEqualTo($id)
  392. ->string($root->getChild(0)->getData())->isEmpty()
  393. ->integer($root->getChild(0)->getLine())->isEqualTo(1)
  394. ->integer($root->getChild(0)->getOffset())->isEqualTo(1)
  395. ;
  396. $this->assert
  397. ->object($root = $parser->parseString('<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . ' id="' . ($id = uniqid()) . '"/>'))->isInstanceOf('mageekguy\atoum\template')
  398. ->array($root->getChildren())->hasSize(1)
  399. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\tag')
  400. ->string($root->getChild(0)->getTag())->isEqualTo($tag)
  401. ->string($root->getChild(0)->getId())->isEqualTo($id)
  402. ->string($root->getChild(0)->getData())->isEmpty()
  403. ->integer($root->getChild(0)->getLine())->isEqualTo(1)
  404. ->integer($root->getChild(0)->getOffset())->isEqualTo(1)
  405. ;
  406. $this->assert
  407. ->object($root = $parser->parseString('<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . '>' . ($data = uniqid()) . '</' . template\parser::defaultNamespace . ':' . $tag . '>'))->isInstanceOf('mageekguy\atoum\template')
  408. ->array($root->getChildren())->hasSize(1)
  409. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\tag')
  410. ->string($root->getChild(0)->getTag())->isEqualTo($tag)
  411. ->variable($root->getChild(0)->getId())->isNull()
  412. ->string($root->getChild(0)->getData())->isEmpty()
  413. ->integer($root->getChild(0)->getLine())->isEqualTo(1)
  414. ->integer($root->getChild(0)->getOffset())->isEqualTo(1)
  415. ;
  416. $this->assert
  417. ->object($root = $parser->parseString(($string1 = uniqid()) . '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . '>' . ($data = uniqid()) . '</' . template\parser::defaultNamespace . ':' . $tag . '>' . ($string2 = uniqid())))->isInstanceOf('mageekguy\atoum\template')
  418. ->array($root->getChildren())->hasSize(3)
  419. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\data')
  420. ->string($root->getChild(0)->getData())->isEqualTo($string1)
  421. ->object($root->getChild(1))->isInstanceOf('mageekguy\atoum\template\tag')
  422. ->variable($root->getChild(1)->getId())->isNull()
  423. ->string($root->getChild(1)->getData())->isEmpty()
  424. ->integer($root->getChild(1)->getLine())->isEqualTo(1)
  425. ->integer($root->getChild(1)->getOffset())->isEqualTo(strlen($string1) + 1)
  426. ->object($root->getChild(2))->isInstanceOf('mageekguy\atoum\template\data')
  427. ->string($root->getChild(2)->getData())->isEqualTo($string2)
  428. ;
  429. $this->assert
  430. ->object($root = $parser->parseString(($string1 = uniqid()) . '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . ' id="' . ($id = uniqid()) . '">' . ($data = uniqid()) . '</' . template\parser::defaultNamespace . ':' . $tag . '>' . ($string2 = uniqid())))->isInstanceOf('mageekguy\atoum\template')
  431. ->array($root->getChildren())->hasSize(3)
  432. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\data')
  433. ->string($root->getChild(0)->getData())->isEqualTo($string1)
  434. ->object($root->getChild(1))->isInstanceOf('mageekguy\atoum\template\tag')
  435. ->string($root->getChild(1)->getId())->isEqualTo($id)
  436. ->string($root->getChild(1)->getData())->isEmpty()
  437. ->integer($root->getChild(1)->getLine())->isEqualTo(1)
  438. ->integer($root->getChild(1)->getOffset())->isEqualTo(strlen($string1) + 1)
  439. ->object($root->getChild(2))->isInstanceOf('mageekguy\atoum\template\data')
  440. ->string($root->getChild(2)->getData())->isEqualTo($string2)
  441. ;
  442. $this->assert
  443. ->object($root = $parser->parseString(($string = str_repeat("\n", 6)) . '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . '/>'))
  444. ->array($root->getChildren())->hasSize(2)
  445. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\data')
  446. ->string($root->getChild(0)->getData())->isEqualTo($string)
  447. ->object($root->getChild(1))->isInstanceOf('mageekguy\atoum\template\tag')
  448. ->variable($root->getChild(1)->getId())->isNull()
  449. ->string($root->getChild(1)->getData())->isEmpty()
  450. ->integer($root->getChild(1)->getLine())->isEqualTo(7)
  451. ->integer($root->getChild(1)->getOffset())->isEqualTo(1)
  452. ;
  453. }
  454. public function testParseFile()
  455. {
  456. $parser = new template\parser(null, $adapter = new test\adapter());
  457. $adapter->file_get_contents = false;
  458. $this->assert
  459. ->exception(function() use ($parser, & $path) {
  460. $parser->parseFile($path = uniqid());
  461. }
  462. )
  463. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  464. ->hasMessage('Unable to get contents from file \'' . $path . '\'')
  465. ;
  466. $adapter->file_get_contents = '';
  467. $this->assert
  468. ->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\atoum\template')
  469. ->array($root->getChildren())->isEmpty()
  470. ;
  471. $adapter->file_get_contents = $string = uniqid();
  472. $this->assert
  473. ->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\atoum\template')
  474. ->array($root->getChildren())->hasSize(1)
  475. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\data')
  476. ->string($root->getChild(0)->getData())->isEqualTo($string)
  477. ;
  478. $adapter->file_get_contents = $string = uniqid() . "\n" . uniqid() . "\n";
  479. $this->assert
  480. ->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\atoum\template')
  481. ->array($root->getChildren())->hasSize(1)
  482. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\data')
  483. ->string($root->getChild(0)->getData())->isEqualTo($string)
  484. ;
  485. $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . ' />';
  486. $this->assert
  487. ->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\atoum\template')
  488. ->array($root->getChildren())->hasSize(1)
  489. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\tag')
  490. ->string($root->getChild(0)->getTag())->isEqualTo($tag)
  491. ->variable($root->getChild(0)->getId())->isNull()
  492. ->string($root->getChild(0)->getData())->isEmpty()
  493. ->integer($root->getChild(0)->getLine())->isEqualTo(1)
  494. ->integer($root->getChild(0)->getOffset())->isEqualTo(1)
  495. ;
  496. $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . '/>';
  497. $this->assert
  498. ->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\atoum\template')
  499. ->array($root->getChildren())->hasSize(1)
  500. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\tag')
  501. ->string($root->getChild(0)->getTag())->isEqualTo($tag)
  502. ->variable($root->getChild(0)->getId())->isNull()
  503. ->string($root->getChild(0)->getData())->isEmpty()
  504. ->integer($root->getChild(0)->getLine())->isEqualTo(1)
  505. ->integer($root->getChild(0)->getOffset())->isEqualTo(1)
  506. ;
  507. $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . ' id="' . ($id = uniqid()) . '" />';
  508. $this->assert
  509. ->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\atoum\template')
  510. ->array($root->getChildren())->hasSize(1)
  511. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\tag')
  512. ->string($root->getChild(0)->getTag())->isEqualTo($tag)
  513. ->string($root->getChild(0)->getId())->isEqualTo($id)
  514. ->string($root->getChild(0)->getData())->isEmpty()
  515. ->integer($root->getChild(0)->getLine())->isEqualTo(1)
  516. ->integer($root->getChild(0)->getOffset())->isEqualTo(1)
  517. ;
  518. $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . ' id="' . ($id = uniqid()) . '"/>';
  519. $this->assert
  520. ->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\atoum\template')
  521. ->array($root->getChildren())->hasSize(1)
  522. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\tag')
  523. ->string($root->getChild(0)->getTag())->isEqualTo($tag)
  524. ->string($root->getChild(0)->getId())->isEqualTo($id)
  525. ->string($root->getChild(0)->getData())->isEmpty()
  526. ->integer($root->getChild(0)->getLine())->isEqualTo(1)
  527. ->integer($root->getChild(0)->getOffset())->isEqualTo(1)
  528. ;
  529. $adapter->file_get_contents = '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . '>' . ($data = uniqid()) . '</' . template\parser::defaultNamespace . ':' . $tag . '>';
  530. $this->assert
  531. ->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\atoum\template')
  532. ->array($root->getChildren())->hasSize(1)
  533. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\tag')
  534. ->string($root->getChild(0)->getTag())->isEqualTo($tag)
  535. ->variable($root->getChild(0)->getId())->isNull()
  536. ->string($root->getChild(0)->getData())->isEmpty()
  537. ->integer($root->getChild(0)->getLine())->isEqualTo(1)
  538. ->integer($root->getChild(0)->getOffset())->isEqualTo(1)
  539. ;
  540. $adapter->file_get_contents = ($string1 = uniqid()) . '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . '>' . ($data = uniqid()) . '</' . template\parser::defaultNamespace . ':' . $tag . '>' . ($string2 = uniqid());
  541. $this->assert
  542. ->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\atoum\template')
  543. ->array($root->getChildren())->hasSize(3)
  544. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\data')
  545. ->string($root->getChild(0)->getData())->isEqualTo($string1)
  546. ->object($root->getChild(1))->isInstanceOf('mageekguy\atoum\template\tag')
  547. ->variable($root->getChild(1)->getId())->isNull()
  548. ->string($root->getChild(1)->getData())->isEmpty()
  549. ->integer($root->getChild(1)->getLine())->isEqualTo(1)
  550. ->integer($root->getChild(1)->getOffset())->isEqualTo(strlen($string1) + 1)
  551. ->object($root->getChild(2))->isInstanceOf('mageekguy\atoum\template\data')
  552. ->string($root->getChild(2)->getData())->isEqualTo($string2)
  553. ;
  554. $adapter->file_get_contents = ($string1 = uniqid()) . '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . ' id="' . ($id = uniqid()) . '">' . ($data = uniqid()) . '</' . template\parser::defaultNamespace . ':' . $tag . '>' . ($string2 = uniqid());
  555. $this->assert
  556. ->object($root = $parser->parseFile(uniqid()))->isInstanceOf('mageekguy\atoum\template')
  557. ->array($root->getChildren())->hasSize(3)
  558. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\data')
  559. ->string($root->getChild(0)->getData())->isEqualTo($string1)
  560. ->object($root->getChild(1))->isInstanceOf('mageekguy\atoum\template\tag')
  561. ->string($root->getChild(1)->getId())->isEqualTo($id)
  562. ->string($root->getChild(1)->getData())->isEmpty()
  563. ->integer($root->getChild(1)->getLine())->isEqualTo(1)
  564. ->integer($root->getChild(1)->getOffset())->isEqualTo(strlen($string1) + 1)
  565. ->object($root->getChild(2))->isInstanceOf('mageekguy\atoum\template\data')
  566. ->string($root->getChild(2)->getData())->isEqualTo($string2)
  567. ;
  568. $adapter->file_get_contents = ($string = str_repeat("\n", 6)) . '<' . template\parser::defaultNamespace . ':' . ($tag = uniqid()) . '/>';
  569. $this->assert
  570. ->object($root = $parser->parseFile(uniqid()))
  571. ->array($root->getChildren())->hasSize(2)
  572. ->object($root->getChild(0))->isInstanceOf('mageekguy\atoum\template\data')
  573. ->string($root->getChild(0)->getData())->isEqualTo($string)
  574. ->object($root->getChild(1))->isInstanceOf('mageekguy\atoum\template\tag')
  575. ->variable($root->getChild(1)->getId())->isNull()
  576. ->string($root->getChild(1)->getData())->isEmpty()
  577. ->integer($root->getChild(1)->getLine())->isEqualTo(7)
  578. ->integer($root->getChild(1)->getOffset())->isEqualTo(1)
  579. ;
  580. }
  581. }
  582. ?>