ParserFactoryTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. /* This test is very weak, because PHPUnit's assertEquals assertion is way too slow dealing with the
  4. * large objects involved here. So we just do some basic instanceof tests instead. */
  5. use PhpParser\Node\Stmt\Echo_;
  6. class ParserFactoryTest extends \PHPUnit\Framework\TestCase
  7. {
  8. /** @dataProvider provideTestCreate */
  9. public function testCreate($kind, $lexer, $expected) {
  10. $this->assertInstanceOf($expected, (new ParserFactory)->create($kind, $lexer));
  11. }
  12. public function provideTestCreate() {
  13. $lexer = new Lexer();
  14. return [
  15. [
  16. ParserFactory::PREFER_PHP7, $lexer,
  17. Parser\Multiple::class
  18. ],
  19. [
  20. ParserFactory::PREFER_PHP5, null,
  21. Parser\Multiple::class
  22. ],
  23. [
  24. ParserFactory::ONLY_PHP7, null,
  25. Parser\Php7::class
  26. ],
  27. [
  28. ParserFactory::ONLY_PHP5, $lexer,
  29. Parser\Php5::class
  30. ]
  31. ];
  32. }
  33. /** @dataProvider provideTestLexerAttributes */
  34. public function testLexerAttributes(Parser $parser) {
  35. $stmts = $parser->parse("<?php /* Bar */ echo 'Foo';");
  36. $stmt = $stmts[0];
  37. $this->assertInstanceOf(Echo_::class, $stmt);
  38. $this->assertCount(1, $stmt->getComments());
  39. $this->assertSame(1, $stmt->getStartLine());
  40. $this->assertSame(1, $stmt->getEndLine());
  41. $this->assertSame(3, $stmt->getStartTokenPos());
  42. $this->assertSame(6, $stmt->getEndTokenPos());
  43. $this->assertSame(16, $stmt->getStartFilePos());
  44. $this->assertSame(26, $stmt->getEndFilePos());
  45. }
  46. public function provideTestLexerAttributes() {
  47. $factory = new ParserFactory();
  48. return [
  49. [$factory->createForHostVersion()],
  50. [$factory->createForNewestSupportedVersion()],
  51. ];
  52. }
  53. }