diff --git a/tests/Parser/RssParserTest.php b/tests/Parser/RssParserTest.php new file mode 100644 index 0000000..0925c84 --- /dev/null +++ b/tests/Parser/RssParserTest.php @@ -0,0 +1,83 @@ +rssParser = new RssParser(); + } + + /** + * @dataProvider validXmlDataProvider + */ + public function testParseWithValidXml(string $xmlContent, string $expectedRootElement): void + { + $stream = $this->createMock(StreamInterface::class); + $stream->method('getContents')->willReturn($xmlContent); + + $response = $this->createMock(ResponseInterface::class); + $response->method('getBody')->willReturn($stream); + + $result = $this->rssParser->parse($response); + + $this->assertInstanceOf(\SimpleXMLElement::class, $result); + $this->assertEquals($expectedRootElement, $result->getName()); + } + + public function testParseWithEmptyResponse(): void + { + $this->expectException(EmptyResponseException::class); + $this->expectExceptionMessage('Empty response received'); + + $stream = $this->createMock(StreamInterface::class); + $stream->method('getContents')->willReturn(''); + + $response = $this->createMock(ResponseInterface::class); + $response->method('getBody')->willReturn($stream); + + $this->rssParser->parse($response); + } + + public function testParseWithInvalidXml(): void + { + $stream = $this->createMock(StreamInterface::class); + $stream->method('getContents')->willReturn('invalid xml content'); + + $response = $this->createMock(ResponseInterface::class); + $response->method('getBody')->willReturn($stream); + + $result = $this->rssParser->parse($response); + + $this->assertFalse($result); + } + + public static function validXmlDataProvider(): array + { + return [ + 'basic RSS feed' => [ + 'xmlContent' => 'Test RSS', + 'expectedRootElement' => 'rss' + ], + 'simple XML' => [ + 'xmlContent' => 'test', + 'expectedRootElement' => 'root' + ], + 'XML with namespaces' => [ + 'xmlContent' => 'Test Feed', + 'expectedRootElement' => 'feed' + ] + ]; + } +} \ No newline at end of file