PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/event/php_exporter_test.php

http://github.com/phpbb/phpbb
PHP | 765 lines | 638 code | 46 blank | 81 comment | 0 complexity | 73084653a2503656a40371ae4d07d4a5 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. class phpbb_event_php_exporter_test extends phpbb_test_case
  14. {
  15. /** @var \phpbb\event\php_exporter */
  16. protected $exporter;
  17. public function setUp(): void
  18. {
  19. parent::setUp();
  20. $this->exporter = new \phpbb\event\php_exporter(dirname(__FILE__) . '/fixtures/');
  21. }
  22. static public function crawl_php_file_data()
  23. {
  24. return array(
  25. array(
  26. 'default.test',
  27. array(
  28. 'default.dispatch' => array(
  29. 'event' => 'default.dispatch',
  30. 'file' => 'default.test',
  31. 'arguments' => array(),
  32. 'since' => '3.1.0-b2',
  33. 'description' => 'Description',
  34. ),
  35. ),
  36. ),
  37. array(
  38. 'event_migration.test',
  39. array(
  40. 'core.ucp_pm_view_message' => array(
  41. 'event' => 'core.ucp_pm_view_message',
  42. 'file' => 'event_migration.test',
  43. 'arguments' => array('cp_row', 'folder', 'folder_id', 'id', 'message_row', 'mode', 'msg_data', 'msg_id', 'user_info'),
  44. 'since' => '3.1.0-a1',
  45. 'description' => 'Modify pm and sender data before it is assigned to the template',
  46. ),
  47. ),
  48. ),
  49. array(
  50. 'extra_description.test',
  51. array(
  52. 'extra_description.dispatch' => array(
  53. 'event' => 'extra_description.dispatch',
  54. 'file' => 'extra_description.test',
  55. 'arguments' => array(),
  56. 'since' => '3.1.0-b2',
  57. 'description' => 'Description<br/><br/>NOTE: This will also be exported',
  58. ),
  59. ),
  60. ),
  61. array(
  62. 'trigger.test',
  63. array(
  64. 'core.trigger' => array(
  65. 'event' => 'core.trigger',
  66. 'file' => 'trigger.test',
  67. 'arguments' => array('cp_row', 'current_row_number', 'end', 'row', 'start'),
  68. 'since' => '3.1.0-a3',
  69. 'description' => 'Event after the post data has been assigned to the template',
  70. ),
  71. ),
  72. ),
  73. array(
  74. 'trigger_wspace.test',
  75. array(
  76. 'core.trigger' => array(
  77. 'event' => 'core.trigger',
  78. 'file' => 'trigger_wspace.test',
  79. 'arguments' => array('cp_row', 'current_row_number', 'end', 'row', 'start'),
  80. 'since' => '3.1.0-a3',
  81. 'description' => 'Event after the post data has been assigned to the template',
  82. ),
  83. ),
  84. ),
  85. array(
  86. 'trigger_many_vars.test',
  87. array(
  88. 'core.posting_modify_template_vars' => array(
  89. 'event' => 'core.posting_modify_template_vars',
  90. 'file' => 'trigger_many_vars.test',
  91. 'arguments' => array(
  92. 'cancel', 'delete', 'error', 'form_enctype', 'forum_id',
  93. 'load', 'message_parser', 'mode', 'moderators', 'page_data',
  94. 'page_title', 'post_data', 'post_id', 'preview', 'refresh',
  95. 's_action', 's_hidden_fields', 's_topic_icons', 'save',
  96. 'submit', 'topic_id',
  97. ),
  98. 'since' => '3.1.0-a1',
  99. 'description' => 'This event allows you to modify template variables for the posting screen',
  100. ),
  101. ),
  102. ),
  103. array(
  104. 'none.test',
  105. array(),
  106. ),
  107. );
  108. }
  109. /**
  110. * @dataProvider crawl_php_file_data
  111. */
  112. public function test_crawl_php_file($file, $expected)
  113. {
  114. $this->exporter->crawl_php_file($file);
  115. $this->assertEquals($expected, $this->exporter->get_events());
  116. }
  117. static public function crawl_php_file_throws_data()
  118. {
  119. return array(
  120. array('missing_var.test', 2),
  121. array('duplicate_event.test', 10),
  122. );
  123. }
  124. /**
  125. * @dataProvider crawl_php_file_throws_data
  126. */
  127. public function test_crawl_php_file_throws($file, $exception_code)
  128. {
  129. $this->expectException('LogicException');
  130. $this->expectExceptionCode($exception_code);
  131. $this->exporter->crawl_php_file($file);
  132. }
  133. static public function validate_since_data()
  134. {
  135. return array(
  136. array('* @since 3.1.0-a1', '3.1.0-a1'),
  137. array('* @since 3.1.0-b3', '3.1.0-b3'),
  138. array(' * @since 3.1.0-b3', '3.1.0-b3'),
  139. array('* @since 3.1.0-RC2', '3.1.0-RC2'),
  140. array(' * @since 3.1.0-a1', '3.1.0-a1'),
  141. );
  142. }
  143. /**
  144. * @dataProvider validate_since_data
  145. */
  146. public function test_validate_since($since, $expected)
  147. {
  148. $this->assertEquals($expected, $this->exporter->validate_since($since));
  149. }
  150. static public function validate_since_throws_data()
  151. {
  152. return array(
  153. array('* @since 3.1.0-a1 '),
  154. array('* @since 3.1.0-a1 bertie is cool'),
  155. array('bertie* @since 3.1.0-a1'),
  156. array('* @since 3.1-A2'),
  157. array('* @since 3.1.0-rc1'),
  158. );
  159. }
  160. /**
  161. * @dataProvider validate_since_throws_data
  162. * @expectedException LogicException
  163. */
  164. public function test_validate_since_throws($since)
  165. {
  166. $this->exporter->validate_since($since);
  167. }
  168. static public function validate_event_data()
  169. {
  170. return array(
  171. array('test.event', '* @event test.event', 'test.event'),
  172. array('test.event2', ' * @event test.event2', 'test.event2'),
  173. array('test.event', ' * @event test.event', 'test.event'),
  174. );
  175. }
  176. /**
  177. * @dataProvider validate_event_data
  178. */
  179. public function test_validate_event($event_name, $event, $expected)
  180. {
  181. $this->assertEquals($expected, $this->exporter->validate_event($event_name, $event));
  182. }
  183. static public function validate_event_throws_data()
  184. {
  185. return array(
  186. array('test.event', '* @event test.event bertie is cool', 2),
  187. array('test.event', 'bertie* @event test.event', 2),
  188. );
  189. }
  190. /**
  191. * @dataProvider validate_event_throws_data
  192. * @expectedException LogicException
  193. */
  194. public function test_validate_event_throws($event_name, $event, $exception_code)
  195. {
  196. $this->expectException('LogicException');
  197. $this->expectExceptionCode($exception_code);
  198. $this->exporter->validate_event($event_name, $event);
  199. }
  200. static public function validate_vars_docblock_array_data()
  201. {
  202. return array(
  203. array(array('abc', 'def'), array('abc', 'def')),
  204. );
  205. }
  206. /**
  207. * @dataProvider validate_vars_docblock_array_data
  208. */
  209. public function test_validate_vars_docblock_array($vars_array, $vars_docblock)
  210. {
  211. $this->assertNull($this->exporter->validate_vars_docblock_array($vars_array, $vars_docblock));
  212. }
  213. static public function validate_vars_docblock_array_throws_data()
  214. {
  215. return array(
  216. array(array('abc', 'def'), array()),
  217. array(array('abc', 'def'), array('abc')),
  218. array(array('abc', 'defg'), array('abc', 'def')),
  219. array(array('abc'), array('abc', 'def')),
  220. array(array(), array('abc', 'def')),
  221. );
  222. }
  223. /**
  224. * @dataProvider validate_vars_docblock_array_throws_data
  225. * @expectedException LogicException
  226. */
  227. public function test_validate_vars_docblock_array_throws($vars_array, $vars_docblock)
  228. {
  229. $this->exporter->validate_vars_docblock_array($vars_array, $vars_docblock);
  230. }
  231. static public function get_dispatch_name_data()
  232. {
  233. return array(
  234. array("\$phpbb_dispatcher->dispatch('dispatch.one2');", 'dispatch.one2'),
  235. array("\t\$phpbb_dispatcher->dispatch('dispatch.one2.thr_ee4');", 'dispatch.one2.thr_ee4'),
  236. array("\$this->dispatcher->dispatch('dispatch.one2');", 'dispatch.one2'),
  237. array("\$phpbb_dispatcher->dispatch('dis_patch.one');", 'dis_patch.one'),
  238. array("\$phpbb_dispatcher->dispatch(['dis_patch.one', 'dis_patch.one2']);", 'dis_patch.one'),
  239. array("\$phpbb_dispatcher->dispatch(['dis_patch.one', 'dis_patch.one2', 'dis_patch.two3']);", 'dis_patch.one'),
  240. );
  241. }
  242. /**
  243. * @dataProvider get_dispatch_name_data
  244. */
  245. public function test_get_dispatch_name($event_line, $expected)
  246. {
  247. $this->exporter->set_content(array($event_line));
  248. $this->assertEquals($expected, $this->exporter->get_event_name(0, true));
  249. }
  250. static public function get_dispatch_name_throws_data()
  251. {
  252. return array(
  253. array("\$phpbb_dispatcher->dispatch();"),
  254. array("\$phpbb_dispatcher->dispatch('');"),
  255. array("\$phpbb_dispatcher->dispatch('dispatch.2one');"),
  256. array("\$phpbb_dispatcher->dispatch('dispatch');"),
  257. array("\$phpbb_dispatcher->dispatch(['dispatch.one']);"),
  258. array("\$phpbb_dispatcher->dispatch(array('dispatch.one', 'dispatch.one2'));"),
  259. );
  260. }
  261. /**
  262. * @dataProvider get_dispatch_name_throws_data
  263. * @expectedException LogicException
  264. */
  265. public function test_get_dispatch_name_throws($event_line)
  266. {
  267. $this->exporter->set_content(array($event_line));
  268. $this->exporter->get_event_name(0, true);
  269. }
  270. static public function get_trigger_event_name_data()
  271. {
  272. return array(
  273. array("extract(\$phpbb_dispatcher->trigger_event('dispatch.one2', compact(\$vars)));", 'dispatch.one2'),
  274. array("\textract(\$phpbb_dispatcher->trigger_event('dispatch.one2.thr_ee4', compact(\$vars)));", 'dispatch.one2.thr_ee4'),
  275. array("extract(\$this->dispatcher->trigger_event('dispatch.one2', compact(\$vars)));", 'dispatch.one2'),
  276. array("extract(\$phpbb_dispatcher->trigger_event('dis_patch.one', compact(\$vars)));", 'dis_patch.one'),
  277. array("extract(\$phpbb_dispatcher->trigger_event(['dis_patch.one', 'dis_patch.one2'], compact(\$vars)));", 'dis_patch.one'),
  278. array("extract(\$phpbb_dispatcher->trigger_event(['dis_patch.one', 'dis_patch.one2', 'dis_patch.two3'], compact(\$vars)));", 'dis_patch.one'),
  279. );
  280. }
  281. /**
  282. * @dataProvider get_trigger_event_name_data
  283. */
  284. public function test_get_trigger_event_name($event_line, $expected)
  285. {
  286. $this->exporter->set_content(array($event_line));
  287. $this->assertEquals($expected, $this->exporter->get_event_name(0, false));
  288. }
  289. static public function get_trigger_event_name_throws_data()
  290. {
  291. return array(
  292. array("extract(\$phpbb_dispatcher->trigger_event());"),
  293. array("extract(\$phpbb_dispatcher->trigger_event(''));"),
  294. array("extract(\$phpbb_dispatcher->trigger_event('dispatch.2one'));"),
  295. array("extract(\$phpbb_dispatcher->trigger_event('dispatch'));"),
  296. array("extract(\$phpbb_dispatcher->trigger_event('dispatch.one', \$vars));"),
  297. array("extract(\$phpbb_dispatcher->trigger_event('dispatch.one', compact(\$var)));"),
  298. array("extract(\$phpbb_dispatcher->trigger_event('dispatch.one', compact(\$array)));"),
  299. array("extract(\$phpbb_dispatcher->trigger_event(['dispatch.one'], compact(\$vars)));"),
  300. array("\$phpbb_dispatcher->trigger_event('dis_patch.one', compact(\$vars));", 'dis_patch.one'),
  301. );
  302. }
  303. /**
  304. * @dataProvider get_trigger_event_name_throws_data
  305. * @expectedException LogicException
  306. */
  307. public function test_get_trigger_event_name_throws($event_line)
  308. {
  309. $this->exporter->set_content(array($event_line));
  310. $this->exporter->get_event_name(0, false);
  311. }
  312. static public function get_vars_from_array_data()
  313. {
  314. return array(
  315. array(
  316. array(
  317. '/**',
  318. '*/',
  319. '$vars = array(\'bertie\');',
  320. '$phpbb_dispatcher->dispatch(\'test\');',
  321. ),
  322. 3,
  323. array('bertie'),
  324. ),
  325. array(
  326. array(
  327. "\t/**",
  328. "\t*/",
  329. "\t\$vars = array('_Strange123', 'phpBB3_Test');",
  330. "\t\$this->dispatcher->dispatch('test');",
  331. ),
  332. 3,
  333. array('_Strange123', 'phpBB3_Test'),
  334. ),
  335. );
  336. }
  337. /**
  338. * @dataProvider get_vars_from_array_data
  339. */
  340. public function test_get_vars_from_array($lines, $event_line, $expected)
  341. {
  342. $this->exporter->set_current_event('', $event_line);
  343. $this->exporter->set_content($lines);
  344. $this->assertEquals($expected, $this->exporter->get_vars_from_array());
  345. }
  346. static public function get_vars_from_array_throws_data()
  347. {
  348. return array(
  349. array(
  350. array(
  351. '/**',
  352. '*/',
  353. '$phpbb_dispatcher->trigger_event(\'test\', compact($vars));',
  354. ),
  355. 2,
  356. 1,
  357. ),
  358. array(
  359. array(
  360. '/**',
  361. '*/',
  362. '$vars = $bertie;',
  363. '$phpbb_dispatcher->trigger_event(\'test\', compact($vars));',
  364. ),
  365. 3,
  366. 1,
  367. ),
  368. array(
  369. array(
  370. '/**',
  371. '*/',
  372. '$vars = array(\'$bertie\');',
  373. '$phpbb_dispatcher->trigger_event(\'test\', compact($vars));',
  374. ),
  375. 3,
  376. 1,
  377. ),
  378. array(
  379. array(
  380. '/**',
  381. '*/',
  382. '$vars = array();',
  383. '$phpbb_dispatcher->trigger_event(\'test\', compact($vars));',
  384. ),
  385. 3,
  386. 1,
  387. ),
  388. array(
  389. array(
  390. '/**',
  391. '*/',
  392. '$vars = array(\'t1\', \'t2\', \'t3\', \'t4\', \'t5\', \'t6\', \'t7\');',
  393. '$phpbb_dispatcher->trigger_event(\'test\', compact($vars));',
  394. ),
  395. 3,
  396. 2,
  397. ),
  398. array(
  399. array(
  400. '/**',
  401. '*/',
  402. '$vars = array(\'test2\', \'\');',
  403. '$phpbb_dispatcher->trigger_event(\'test\', compact($vars));',
  404. ),
  405. 3,
  406. 3,
  407. ),
  408. array(
  409. array(
  410. '/**',
  411. '*/',
  412. '$vars = array(\'bertie\'\');',
  413. '$phpbb_dispatcher->trigger_event(\'test\', compact($vars));',
  414. ),
  415. 3,
  416. 3,
  417. ),
  418. array(
  419. array(
  420. '/**',
  421. '*/',
  422. '$vars = array(\'bertie\',\'basically_valid\');',
  423. '$phpbb_dispatcher->trigger_event(\'test\', compact($vars));',
  424. ),
  425. 3,
  426. 3,
  427. ),
  428. );
  429. }
  430. /**
  431. * @dataProvider get_vars_from_array_throws_data
  432. * @expectedException LogicException
  433. */
  434. public function test_get_vars_from_array_throws($lines, $event_line, $exception_code)
  435. {
  436. $this->expectException('LogicException');
  437. $this->expectExceptionCode($exception_code);
  438. $this->exporter->set_current_event('', $event_line);
  439. $this->exporter->set_content($lines);
  440. $this->exporter->get_vars_from_array();
  441. }
  442. static public function get_vars_from_docblock_data()
  443. {
  444. return array(
  445. array(
  446. array(
  447. '/**',
  448. '* @var int name1 Description',
  449. '* @var array name2 Description test',
  450. '*/',
  451. '$phpbb_dispatcher->dispatch(\'test\');',
  452. ),
  453. 4,
  454. array('name1', 'name2'),
  455. ),
  456. );
  457. }
  458. /**
  459. * @dataProvider get_vars_from_docblock_data
  460. */
  461. public function test_get_vars_from_docblock($lines, $event_line, $expected)
  462. {
  463. $this->exporter->set_current_event('', $event_line);
  464. $this->exporter->set_content($lines);
  465. $this->assertEquals($expected, $this->exporter->get_vars_from_docblock());
  466. }
  467. static public function get_vars_from_docblock_throws_data()
  468. {
  469. return array(
  470. array(
  471. array(
  472. '$vars = array();',
  473. '$phpbb_dispatcher->dispatch(\'test\');',
  474. ),
  475. 1,
  476. 2,
  477. ),
  478. array(
  479. array(
  480. '/**',
  481. '* @var int name1',
  482. '* @var array name2 Description test',
  483. '*/',
  484. '$phpbb_dispatcher->dispatch(\'test\');',
  485. ),
  486. 4,
  487. 1,
  488. ),
  489. array(
  490. array(
  491. '/**',
  492. '*/',
  493. '$phpbb_dispatcher->dispatch(\'test\');',
  494. ),
  495. 2,
  496. 3,
  497. ),
  498. array(
  499. array(
  500. '/**',
  501. '* @var int name1 Description',
  502. '* @var array $name2 Description',
  503. '*/',
  504. '$phpbb_dispatcher->dispatch(\'test\');',
  505. ),
  506. 4,
  507. 4,
  508. ),
  509. );
  510. }
  511. /**
  512. * @dataProvider get_vars_from_docblock_throws_data
  513. * @expectedException LogicException
  514. */
  515. public function test_get_vars_from_docblock_throws($lines, $event_line, $exception_code)
  516. {
  517. $this->expectException('LogicException');
  518. $this->expectExceptionCode($exception_code);
  519. $this->exporter->set_current_event('', $event_line);
  520. $this->exporter->set_content($lines);
  521. $this->exporter->get_vars_from_docblock();
  522. }
  523. static public function find_since_data()
  524. {
  525. return array(
  526. array(
  527. array(
  528. '/**',
  529. '* @since 3.1.0-a1',
  530. '*/',
  531. '$phpbb_dispatcher->dispatch(\'test\');',
  532. ),
  533. 3,
  534. 1,
  535. ),
  536. array(
  537. array(
  538. '* @since 3.1.0-a1',
  539. '/**',
  540. '* @since 3.1.0-a1',
  541. '* @changed 3.1.0-a2',
  542. '*/',
  543. '$phpbb_dispatcher->dispatch(\'test\');',
  544. ),
  545. 5,
  546. 2,
  547. ),
  548. );
  549. }
  550. /**
  551. * @dataProvider find_since_data
  552. */
  553. public function test_find_since($lines, $event_line, $expected)
  554. {
  555. $this->exporter->set_current_event('', $event_line);
  556. $this->exporter->set_content($lines);
  557. $this->assertEquals($expected, $this->exporter->find_since());
  558. }
  559. static public function find_since_throws_data()
  560. {
  561. return array(
  562. array(
  563. array(
  564. '/**',
  565. '* @since 3.1.0-a1',
  566. '*/',
  567. '/**',
  568. '*/',
  569. '$phpbb_dispatcher->dispatch(\'test\');',
  570. ),
  571. 5,
  572. 1,
  573. ),
  574. array(
  575. array(
  576. '/**',
  577. '* @changed 3.1.0-a1',
  578. '* @changed 3.1.0-a2',
  579. '* @changed 3.1.0-a3',
  580. '*/',
  581. '$phpbb_dispatcher->dispatch(\'test\');',
  582. ),
  583. 5,
  584. 2,
  585. ),
  586. array(
  587. array(
  588. '/**',
  589. '* @since 3.1.0-a2',
  590. '* @var',
  591. '*/',
  592. '$phpbb_dispatcher->dispatch(\'test\');',
  593. ),
  594. 4,
  595. 3,
  596. ),
  597. array(
  598. array(
  599. '/**',
  600. '* @since 3.1.0-a2',
  601. '* @event',
  602. '*/',
  603. '$phpbb_dispatcher->dispatch(\'test\');',
  604. ),
  605. 4,
  606. 3,
  607. ),
  608. );
  609. }
  610. /**
  611. * @dataProvider find_since_throws_data
  612. * @expectedException LogicException
  613. */
  614. public function test_find_since_throws($lines, $event_line, $exception_code)
  615. {
  616. $this->expectException('LogicException');
  617. $this->expectExceptionCode($exception_code);
  618. $this->exporter->set_current_event('', $event_line);
  619. $this->exporter->set_content($lines);
  620. $this->exporter->find_since();
  621. }
  622. static public function find_description_data()
  623. {
  624. return array(
  625. array(
  626. array(
  627. '/**',
  628. '* Hello Bertie!',
  629. '* @since 3.1.0-a1',
  630. '*/',
  631. '$phpbb_dispatcher->dispatch(\'test\');',
  632. ),
  633. 4,
  634. 1,
  635. ),
  636. array(
  637. array(
  638. ' /**',
  639. ' * Hello Bertie!',
  640. ' *',
  641. ' * @since 3.1.0-a1',
  642. ' * @changed 3.1.0-a2',
  643. ' */',
  644. ' $phpbb_dispatcher->dispatch(\'test\');',
  645. ),
  646. 6,
  647. 1,
  648. ),
  649. );
  650. }
  651. /**
  652. * @dataProvider find_description_data
  653. */
  654. public function test_find_description($lines, $event_line, $expected)
  655. {
  656. $this->exporter->set_current_event('', $event_line);
  657. $this->exporter->set_content($lines);
  658. $this->assertEquals($expected, $this->exporter->find_description());
  659. }
  660. static public function find_description_throws_data()
  661. {
  662. return array(
  663. array(
  664. array(
  665. '$vars = array();',
  666. '$phpbb_dispatcher->dispatch(\'test\');',
  667. ),
  668. 1,
  669. 1,
  670. ),
  671. array(
  672. array(
  673. '/**',
  674. '* @changed 3.1.0-a1',
  675. '* @changed 3.1.0-a2',
  676. '* @changed 3.1.0-a3',
  677. '*/',
  678. '$phpbb_dispatcher->dispatch(\'test\');',
  679. ),
  680. 5,
  681. 2,
  682. ),
  683. array(
  684. array(
  685. '/**',
  686. '*',
  687. '* @since 3.1.0-a2',
  688. '*/',
  689. '$phpbb_dispatcher->dispatch(\'test\');',
  690. ),
  691. 4,
  692. 2,
  693. ),
  694. array(
  695. array(
  696. '/**',
  697. '* ',
  698. '* @event',
  699. '*/',
  700. '$phpbb_dispatcher->dispatch(\'test\');',
  701. ),
  702. 4,
  703. 2,
  704. ),
  705. );
  706. }
  707. /**
  708. * @dataProvider find_description_throws_data
  709. * @expectedException LogicException
  710. */
  711. public function test_find_description_throws($lines, $event_line, $exception_code)
  712. {
  713. $this->expectException('LogicException');
  714. $this->expectExceptionCode($exception_code);
  715. $this->exporter->set_current_event('', $event_line);
  716. $this->exporter->set_content($lines);
  717. $this->exporter->find_description();
  718. }
  719. }