From 2ba37a15c8bb8d4b205eb6b795c8a41b454dda5b Mon Sep 17 00:00:00 2001 From: stefanorosanelli Date: Fri, 10 May 2024 11:43:21 +0200 Subject: [PATCH] test: import sitemap tests --- .../Command/ImportSitemapCommandTest.php | 91 +++++++++++++++++++ tests/files/sitemap.xml | 14 +++ 2 files changed, 105 insertions(+) create mode 100644 tests/TestCase/Command/ImportSitemapCommandTest.php create mode 100644 tests/files/sitemap.xml diff --git a/tests/TestCase/Command/ImportSitemapCommandTest.php b/tests/TestCase/Command/ImportSitemapCommandTest.php new file mode 100644 index 0000000..e503aaf --- /dev/null +++ b/tests/TestCase/Command/ImportSitemapCommandTest.php @@ -0,0 +1,91 @@ +useCommandRunner(); + Router::reload(); + } + + /** + * Test buildOptionParser method + * + * @return void + * @covers ::buildOptionParser() + */ + public function testBuildOptionParser(): void + { + $this->exec('import_sitemap --help'); + $this->assertOutputContains('File path or URL of sitemap to import'); + $this->assertOutputContains('Optional path prefix of URLs to import'); + $this->assertOutputContains('Collection used to index'); + } + + /** + * Test options failure + * + * @return void + * @covers ::initialize() + * @covers ::execute() + */ + public function testOptionFailure(): void + { + $this->exec('import_sitemap --sitemap /not/existing/path --collection gustavo'); + $this->assertExitError('File not found: /not/existing/path'); + + $this->mockClientResponse(json_encode([])); + $xmlPath = sprintf('%s/tests/files/sitemap.xml', getcwd()); + $this->exec(sprintf('import_sitemap --sitemap %s --collection gustavo', $xmlPath)); + $this->assertExitError('Collection not found: gustavo'); + + $this->mockClientResponse('[{"cmetadata": {"id":"1"}}]'); + $this->mockTable('Collections', new ObjectEntity()); + $xmlPath = sprintf('%s/tests/files/empty.csv', getcwd()); + $this->exec(sprintf('import_sitemap --sitemap %s --collection gustavo', $xmlPath)); + $this->assertExitError('No URLs found in sitemap'); + + } + + /** + * Test command success + * + * @return void + * @covers ::execute() + */ + public function testCommand(): void + { + $this->mockTable('Collections', new ObjectEntity()); + $this->mockTable('Links', new ObjectEntity()); + $this->mockClientResponse('[{"cmetadata": {"id":"1"}}]', 200, 3); + $xmlPath = sprintf('%s/tests/files/sitemap.xml', getcwd()); + $this->exec(sprintf('import_sitemap --sitemap %s --collection gustavo', $xmlPath)); + $this->assertExitSuccess('Done'); + } +} diff --git a/tests/files/sitemap.xml b/tests/files/sitemap.xml new file mode 100644 index 0000000..b678db2 --- /dev/null +++ b/tests/files/sitemap.xml @@ -0,0 +1,14 @@ + + + https://example.com/home + 2024-04-28T08:02:38+00:00 + always + 1.0 + + + https://example.com/page2 + 2024-04-28T08:02:38+00:00 + always + 1.0 + +