From 26cae08119dac3de002957ecf4a27cc585ba896e Mon Sep 17 00:00:00 2001 From: Samantha Nguyen Date: Thu, 20 Feb 2025 23:04:53 -0600 Subject: [PATCH] tests: more coverage --- tests/ErrorCodeTest.php | 4 ++ tests/XmlNativeParserTest.php | 95 +++++++++++++++++++++++++++++++++++ tests/XmlParseResultTest.php | 5 ++ tests/assets/example.xml | 5 ++ 4 files changed, 109 insertions(+) create mode 100644 tests/assets/example.xml diff --git a/tests/ErrorCodeTest.php b/tests/ErrorCodeTest.php index 0f0560d..565d0b8 100644 --- a/tests/ErrorCodeTest.php +++ b/tests/ErrorCodeTest.php @@ -9,6 +9,10 @@ #[CoversClass( ErrorCode::class )] class ErrorCodeTest extends TestCase { + public function testAsErrorString(): void { + $this->assertSame( 'No error', ErrorCode::None->asErrorString() ); + } + #[DataProvider( "provideVariantEquals" )] public function testVariantEquals( ErrorCode $errorCode, int $errorCodeInt ): void { $this->assertSame( $errorCode->value, $errorCodeInt ); diff --git a/tests/XmlNativeParserTest.php b/tests/XmlNativeParserTest.php index ba5f872..751a8ab 100644 --- a/tests/XmlNativeParserTest.php +++ b/tests/XmlNativeParserTest.php @@ -68,6 +68,101 @@ public function testGetErrorCode(): void { $this->assertSame( ErrorCode::None, $parser->getErrorCode() ); } + #[DataProvider( "provideParse" )] + public function testParse( string $path ): void { + $file = fopen( $path, 'r' ); + $parser = XmlNativeParser::new( Encoding::Utf8 ); + + // read file + $result = null; + // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition + while ( ( $data = \fread( $file, 16384 ) ) ) { + $result = $parser->parse( $data ); + } + $parser->parseFinalize(); + + // assert + $this->assertSame( 1, $result ); + } + + public static function provideParse(): array { + return [ + [ + __DIR__ . '/assets/example.xml', + ] + ]; + } + + #[DataProvider( "provideParseIntoStructFails" )] + public function testParseIntoStructFails( + string $xml, + ErrorCode $errorCode, + ): void { + $parser = XmlNativeParser::new( Encoding::Utf8 ); + $result = $parser->parseIntoStruct( $xml ); + + // assert + $this->assertSame( $errorCode, $parser->getErrorCode() ); + $this->assertFalse( $result ); + } + + public static function provideParseIntoStructFails(): array { + return [ + [ + << + XML, + ErrorCode::UnclosedToken, + ] + ]; + } + + #[DataProvider( "provideParseIntoStructWorks" )] + public function testParseIntoStructWorks( + string $xml, + ErrorCode $errorCode, + array $expectedResultValues, + ): void { + $parser = XmlNativeParser::new( Encoding::Utf8 ); + $result = $parser->parseIntoStruct( $xml ); + + // assert + $this->assertSame( $errorCode, $parser->getErrorCode() ); + + if ( $result !== false ) { + $this->assertSame( $expectedResultValues, $result->values ); + } + } + + public static function provideParseIntoStructWorks(): array { + return [ + [ + <<simple note + XML, + ErrorCode::None, + [ + [ + 'tag' => 'PARA', + 'type' => 'open', + 'level' => 1, + ], + [ + 'tag' => 'NOTE', + 'type' => 'complete', + 'level' => 2, + 'value' => 'simple note', + ], + [ + 'tag' => 'PARA', + 'type' => 'close', + 'level' => 1, + ], + ] + ] + ]; + } + #[DataProvider( "provideGetOption" )] public function testGetOption( Option $option, string|int|bool $expectedValue ): void { $parser = XmlNativeParser::new( Encoding::Utf8 ); diff --git a/tests/XmlParseResultTest.php b/tests/XmlParseResultTest.php index e7ba6aa..cfb474d 100644 --- a/tests/XmlParseResultTest.php +++ b/tests/XmlParseResultTest.php @@ -9,6 +9,11 @@ #[CoversClass( XmlParseResult::class )] class XmlParseResultTest extends TestCase { + public function testConstructor(): void { + $result = new XmlParseResult( [], [] ); + $this->assertInstanceOf( XmlParseResult::class, $result ); + } + #[DataProvider( "provideParseResult" )] public function testIndex( XmlParseResult $result ): void { $this->assertSame( diff --git a/tests/assets/example.xml b/tests/assets/example.xml new file mode 100644 index 0000000..7d9e623 --- /dev/null +++ b/tests/assets/example.xml @@ -0,0 +1,5 @@ + + + Alice + Bob +