Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PHP parser based on PEG.js grammar #1152

Merged
merged 18 commits into from
Jun 30, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Run tests against PHP parser during Travis CI builds
Or using the following commands (TODO - add a npm script for this?):

```
bin/create-php-parser.js
RUN_PARSER_TESTS=1 phpunit
```
  • Loading branch information
nylen committed Jun 30, 2017
commit b6337f702948a6247feb2b11a3d49d01163263fa
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ script:
node bin/create-php-parser.js || exit 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be added to the NPM build and dev scripts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not yet - instead, we need to make sure we run our existing parsing tests against the PHP parser as well.

php lib/parser.php || exit 1
# Run PHPUnit tests
phpunit || exit 1
RUN_PARSER_TESTS=1 phpunit || exit 1
WP_MULTISITE=1 phpunit || exit 1
fi
- |
6 changes: 6 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -40,4 +40,10 @@
<rule ref="Generic.Commenting.DocComment.MissingShort">
<exclude-pattern>phpunit/*</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting.VariableComment.Missing">
<exclude-pattern>phpunit/*</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting.FunctionCommentThrowTag.Missing">
<exclude-pattern>phpunit/*</exclude-pattern>
</rule>
</ruleset>
68 changes: 68 additions & 0 deletions phpunit/class-parsing-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* PEG.js parser (PHP code target) tests
*
* @package Gutenberg
*/

class Parsing_Test extends WP_UnitTestCase {
protected static $fixtures_dir;

function parsing_test_filenames() {
if ( ! getenv( 'RUN_PARSER_TESTS' ) ) {
// Use the RUN_PARSER_TESTS environment variable to run these tests.
return array();
}
self::$fixtures_dir = dirname( dirname( __FILE__ ) ) . '/blocks/test/fixtures';

require_once dirname( dirname( __FILE__ ) ) . '/lib/parser.php';

$fixture_filenames = glob( self::$fixtures_dir . '/*.{json,html}', GLOB_BRACE );
$fixture_filenames = array_values( array_unique( array_map(
array( $this, 'clean_fixture_filename' ),
$fixture_filenames
) ) );

return array_map(
array( $this, 'pass_parser_fixture_filenames' ),
$fixture_filenames
);
}

function clean_fixture_filename( $filename ) {
$filename = basename( $filename );
$filename = preg_replace( '/\..+$/', '', $filename );
return $filename;
}

function pass_parser_fixture_filenames( $filename ) {
return array(
"$filename.html",
"$filename.parsed.json",
);
}

/**
* @dataProvider parsing_test_filenames
*/
function test_parser_output( $html_filename, $parsed_json_filename ) {
$html_filename = self::$fixtures_dir . '/' . $html_filename;
$parsed_json_filename = self::$fixtures_dir . '/' . $parsed_json_filename;

foreach ( array( $html_filename, $parsed_json_filename ) as $filename ) {
if ( ! file_exists( $filename ) ) {
throw new Exception( "Missing fixture file: '$filename'" );
}
}

$html = file_get_contents( $html_filename );
$parsed = json_decode( file_get_contents( $parsed_json_filename ), true );

$parser = new PhpPegJs\Parser;
$result = $parser->parse( $html );

error_log( json_encode( compact( 'html', 'result' ) ) );

$this->assertEquals( $parsed, $result );
}
}