. /** * PHP lang parser test. * * @package tool_customlang * @copyright 2015 Damyon Wiese * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace tool_customlang\local\mlang; use advanced_testcase; use moodle_exception; /** * PHP lang parser test class. * * @package tool_customlang * @copyright 2015 Damyon Wiese * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class phpparser_testcase extends advanced_testcase { /** * Test get instance static method. * */ public function test_get_instance(): void { $instance = phpparser::get_instance(); $this->assertInstanceOf('tool_customlang\local\mlang\phpparser', $instance); $this->assertEquals($instance, phpparser::get_instance()); } /** * Test get instance parse method. * * @dataProvider parse_provider * @param string $phpcode PHP code to test * @param array $expected Expected result * @param bool $exception if an exception is expected */ public function test_parse(string $phpcode, array $expected, bool $exception): void { $instance = phpparser::get_instance(); if ($exception) { $this->expectException(moodle_exception::class); } $strings = $instance->parse($phpcode); $this->assertEquals(count($expected), count($strings)); foreach ($strings as $key => $langstring) { $this->assertEquals($expected[$key][0], $langstring->id); $this->assertEquals($expected[$key][1], $langstring->text); } } /** * Data provider for the test_parse. * * @return array */ public function parse_provider() : array { return [ 'Invalid PHP code' => [ 'No PHP code', [], false ], 'No PHP open tag' => [ "\$string['example'] = 'text';\n", [], false ], 'One string code' => [ " [ " [ " [ " [ " [ " [ " [ " [ " [ " [ " [ " [ " [ " [ " [ " [ " [ "