RequirePassTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2018 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Test\CodeCleaner;
  11. use Psy\CodeCleaner\RequirePass;
  12. class RequirePassTest extends CodeCleanerTestCase
  13. {
  14. public function setUp()
  15. {
  16. $this->setPass(new RequirePass());
  17. }
  18. /**
  19. * @dataProvider exitStatements
  20. */
  21. public function testExitStatement($from, $to)
  22. {
  23. $this->assertProcessesAs($from, $to);
  24. }
  25. public function exitStatements()
  26. {
  27. $resolve = '\\Psy\\CodeCleaner\\RequirePass::resolve';
  28. return [
  29. // The basics
  30. ['require "a"', "require $resolve(\"a\", 1);"],
  31. ['require "b.php"', "require $resolve(\"b.php\", 1);"],
  32. ['require_once "c"', "require_once $resolve(\"c\", 1);"],
  33. ['require_once "d.php"', "require_once $resolve(\"d.php\", 1);"],
  34. // Ensure that line numbers work correctly
  35. ["null;\nrequire \"e.php\"", "null;\nrequire $resolve(\"e.php\", 2);"],
  36. ["null;\nrequire_once \"f.php\"", "null;\nrequire_once $resolve(\"f.php\", 2);"],
  37. // Things with expressions
  38. ['require $foo', "require $resolve(\$foo, 1);"],
  39. ['require_once $foo', "require_once $resolve(\$foo, 1);"],
  40. ['require ($bar = "g.php")', "require $resolve(\$bar = \"g.php\", 1);"],
  41. ['require_once ($bar = "h.php")', "require_once $resolve(\$bar = \"h.php\", 1);"],
  42. ['$bar = require ($baz = "i.php")', "\$bar = (require $resolve(\$baz = \"i.php\", 1));"],
  43. ['$bar = require_once ($baz = "j.php")', "\$bar = (require_once $resolve(\$baz = \"j.php\", 1));"],
  44. ];
  45. }
  46. /**
  47. * @expectedException \Psy\Exception\FatalErrorException
  48. * @expectedExceptionMessage Failed opening required 'not a file name' in eval()'d code on line 2
  49. */
  50. public function testResolve()
  51. {
  52. RequirePass::resolve('not a file name', 2);
  53. }
  54. /**
  55. * @dataProvider emptyWarnings
  56. *
  57. * @expectedException \Psy\Exception\ErrorException
  58. * @expectedExceptionMessage Filename cannot be empty on line 1
  59. */
  60. public function testResolveEmptyWarnings($file)
  61. {
  62. if (!E_WARNING & \error_reporting()) {
  63. $this->markTestSkipped();
  64. }
  65. RequirePass::resolve($file, 1);
  66. }
  67. public function emptyWarnings()
  68. {
  69. return [
  70. [null],
  71. [false],
  72. [''],
  73. ];
  74. }
  75. public function testResolveWorks()
  76. {
  77. $this->assertEquals(__FILE__, RequirePass::resolve(__FILE__, 3));
  78. }
  79. }