UseStatementPassTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\UseStatementPass;
  12. class UseStatementPassTest extends CodeCleanerTestCase
  13. {
  14. public function setUp()
  15. {
  16. $this->setPass(new UseStatementPass());
  17. }
  18. /**
  19. * @dataProvider useStatements
  20. */
  21. public function testProcess($from, $to)
  22. {
  23. $this->assertProcessesAs($from, $to);
  24. }
  25. public function useStatements()
  26. {
  27. return [
  28. [
  29. "use StdClass as NotSoStd;\n\$std = new NotSoStd();",
  30. '$std = new \\StdClass();',
  31. ],
  32. [
  33. "namespace Foo;\n\nuse StdClass as S;\n\$std = new S();",
  34. "namespace Foo;\n\n\$std = new \\StdClass();",
  35. ],
  36. [
  37. "namespace Foo;\n\nuse \\StdClass as S;\n\$std = new S();",
  38. "namespace Foo;\n\n\$std = new \\StdClass();",
  39. ],
  40. [
  41. "use Foo\\Bar as fb;\n\$baz = new fb\\Baz();",
  42. '$baz = new \\Foo\\Bar\\Baz();',
  43. ],
  44. [
  45. "use Foo\\Bar;\n\$baz = new Bar\\Baz();",
  46. '$baz = new \\Foo\\Bar\\Baz();',
  47. ],
  48. [
  49. "namespace Foo;\nuse Bar;\n\$baz = new Bar\\Baz();",
  50. "namespace Foo;\n\n\$baz = new \\Bar\\Baz();",
  51. ],
  52. [
  53. "namespace Foo;\n\nuse \\StdClass as S;\n\$std = new S();\nnamespace Foo;\n\n\$std = new S();",
  54. "namespace Foo;\n\n\$std = new \\StdClass();\nnamespace Foo;\n\n\$std = new \\StdClass();",
  55. ],
  56. [
  57. "namespace Foo;\n\nuse \\StdClass as S;\n\$std = new S();\nnamespace Bar;\n\n\$std = new S();",
  58. "namespace Foo;\n\n\$std = new \\StdClass();\nnamespace Bar;\n\n\$std = new S();",
  59. ],
  60. [
  61. "use Foo\\Bar as fb, Qux as Q;\n\$baz = new fb\\Baz();\n\$qux = new Q();",
  62. "\$baz = new \\Foo\\Bar\\Baz();\n\$qux = new \\Qux();",
  63. ],
  64. ];
  65. }
  66. /**
  67. * @dataProvider groupUseStatements
  68. */
  69. public function testGroupUseProcess($from, $to)
  70. {
  71. $this->assertProcessesAs($from, $to);
  72. }
  73. public function groupUseStatements()
  74. {
  75. if (\version_compare(PHP_VERSION, '7.0', '<')) {
  76. $this->markTestSkipped();
  77. }
  78. return [
  79. [
  80. "use Foo\\{Bar, Baz, Qux as Q};\n\$bar = new Bar();\n\$baz = new Baz();\n\$qux = new Q();",
  81. "\$bar = new \\Foo\\Bar();\n\$baz = new \\Foo\\Baz();\n\$qux = new \\Foo\\Qux();",
  82. ],
  83. [
  84. "use X\\{Foo, Bar as B};\n\$foo = new Foo();\n\$baz = new B\\Baz();",
  85. "\$foo = new \\X\\Foo();\n\$baz = new \\X\\Bar\\Baz();",
  86. ],
  87. [
  88. "use X\\{Foo, Bar as B};\n\$foo = new Foo();\n\$bar = new Bar();\n\$baz = new B\\Baz();",
  89. "\$foo = new \\X\\Foo();\n\$bar = new Bar();\n\$baz = new \\X\\Bar\\Baz();",
  90. ],
  91. ];
  92. }
  93. }