From ae886c6e5bbd48d212b590c6d37abdaca428bea0 Mon Sep 17 00:00:00 2001 From: Bugo Date: Thu, 30 Jan 2025 02:24:06 +0500 Subject: [PATCH] Update tests --- phpunit.xml | 3 ++- tests/Enums/FrequencyTest.php | 35 +++++++++++++++++++++++++ tests/Enums/PriorityTest.php | 29 ++++++++++++++++++++ tests/Services/SitemapGeneratorTest.php | 23 ---------------- tests/Services/XmlGeneratorTest.php | 8 +++--- 5 files changed, 71 insertions(+), 27 deletions(-) create mode 100644 tests/Enums/FrequencyTest.php create mode 100644 tests/Enums/PriorityTest.php diff --git a/phpunit.xml b/phpunit.xml index 665b933..6d28b08 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -19,9 +19,10 @@ src/Sources/Optimus/Addons - src/Sources/Optimus/Events src/Sources/Optimus/Libs src/Sources/Optimus/Handlers/index.php + src/Sources/Optimus/Enums/index.php + src/Sources/Optimus/Events/index.php src/Sources/Optimus/Services/index.php src/Sources/Optimus/Tasks/index.php src/Sources/Optimus/Utils/index.php diff --git a/tests/Enums/FrequencyTest.php b/tests/Enums/FrequencyTest.php new file mode 100644 index 0000000..b7f7f4e --- /dev/null +++ b/tests/Enums/FrequencyTest.php @@ -0,0 +1,35 @@ +toBe(Frequency::Hourly); +}); + +it('returns Daily for timestamps within the last 7 days', function () { + $timestamp = time() - (6 * 86400); // 6 days ago + expect(Frequency::fromTimestamp($timestamp))->toBe(Frequency::Daily); +}); + +it('returns Weekly for timestamps within the last month', function () { + $timestamp = time() - (14 * 86400); // 2 weeks ago + expect(Frequency::fromTimestamp($timestamp))->toBe(Frequency::Weekly); +}); + +it('returns Monthly for timestamps within the last year', function () { + $timestamp = time() - (30 * 86400 * 6); // 6 months ago + expect(Frequency::fromTimestamp($timestamp))->toBe(Frequency::Monthly); +}); + +it('returns Yearly for timestamps older than a year', function () { + $timestamp = time() - (400 * 86400); // More than a year ago + expect(Frequency::fromTimestamp($timestamp))->toBe(Frequency::Yearly); +}); + +it('handles exact boundary cases correctly', function () { + expect(Frequency::fromTimestamp(time() - 86400))->toBe(Frequency::Daily); // Exactly 24 hours + expect(Frequency::fromTimestamp(time() - 604800))->toBe(Frequency::Weekly); // Exactly 7 days + expect(Frequency::fromTimestamp(time() - 2628000))->toBe(Frequency::Monthly); // Exactly 1 month + expect(Frequency::fromTimestamp(time() - 31536000))->toBe(Frequency::Yearly); // Exactly 1 year +}); diff --git a/tests/Enums/PriorityTest.php b/tests/Enums/PriorityTest.php new file mode 100644 index 0000000..aeb0f71 --- /dev/null +++ b/tests/Enums/PriorityTest.php @@ -0,0 +1,29 @@ +toBe(Priority::Prime); +}); + +it('returns Elevated for timestamps within 31-60 days', function () { + $timestamp = time() - (45 * 86400); + expect(Priority::fromTimestamp($timestamp))->toBe(Priority::Elevated); +}); + +it('returns Base for timestamps within 61-90 days', function () { + $timestamp = time() - (75 * 86400); + expect(Priority::fromTimestamp($timestamp))->toBe(Priority::Base); +}); + +it('returns Minimal for timestamps older than 90 days', function () { + $timestamp = time() - (120 * 86400); + expect(Priority::fromTimestamp($timestamp))->toBe(Priority::Minimal); +}); + +it('handles exact boundary cases correctly', function () { + expect(Priority::fromTimestamp(time() - (30 * 86400)))->toBe(Priority::Prime); + expect(Priority::fromTimestamp(time() - (60 * 86400)))->toBe(Priority::Elevated); + expect(Priority::fromTimestamp(time() - (90 * 86400)))->toBe(Priority::Base); +}); diff --git a/tests/Services/SitemapGeneratorTest.php b/tests/Services/SitemapGeneratorTest.php index c2fd723..3dca370 100644 --- a/tests/Services/SitemapGeneratorTest.php +++ b/tests/Services/SitemapGeneratorTest.php @@ -154,29 +154,6 @@ public function getTopicLinks(): array { expect($result)->toMatch('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{2}:00$/'); }); - - it('returns correct frequency', function () { - $now = time(); - - $method = new ReflectionMethod($this->generator, 'getFrequency'); - - expect($method->invoke($this->generator, $now - 3600))->toBe('hourly') - ->and($method->invoke($this->generator, $now - 86400 * 2))->toBe('daily') - ->and($method->invoke($this->generator, $now - 86400 * 14))->toBe('weekly') - ->and($method->invoke($this->generator, $now - 86400 * 60))->toBe('monthly') - ->and($method->invoke($this->generator, $now - 86400 * 400))->toBe('yearly'); - }); - - it('returns correct priority', function () { - $now = time(); - - $method = new ReflectionMethod($this->generator, 'getPriority'); - - expect($method->invoke($this->generator, $now - 86400 * 15))->toBe('0.8') - ->and($method->invoke($this->generator, $now - 86400 * 45))->toBe('0.6') - ->and($method->invoke($this->generator, $now - 86400 * 75))->toBe('0.4') - ->and($method->invoke($this->generator, $now - 86400 * 100))->toBe('0.2'); - }); }); it('allows adding custom links through event dispatcher', function () { diff --git a/tests/Services/XmlGeneratorTest.php b/tests/Services/XmlGeneratorTest.php index 8b6c2d6..ccf117c 100644 --- a/tests/Services/XmlGeneratorTest.php +++ b/tests/Services/XmlGeneratorTest.php @@ -44,17 +44,19 @@ $data = [ [ 'loc' => 'https://example.com/item1', - 'image:image' => ['image:loc' => 'https://example.com/image.png'], 'mobile:mobile' => ['mobile:loc' => 'https://example.com/mobile'], + 'image:image' => ['image:loc' => 'https://example.com/image.png'], + 'video:video' => ['video:content_loc' => 'https://example.com/video.mp4'], ], ]; - $xml = $this->xmlGenerator->generate($data, ['mobile' => true, 'images' => true]); + $xml = $this->xmlGenerator->generate($data, ['mobile' => true, 'images' => true, 'videos' => true]); expect($xml)->toContain('http://www.google.com/schemas/sitemap-mobile/1.0') ->and($xml)->toContain('http://www.google.com/schemas/sitemap-image/1.1') + ->and($xml)->toContain('https://example.com/mobile') ->and($xml)->toContain('https://example.com/image.png') - ->and($xml)->toContain('https://example.com/mobile'); + ->and($xml)->toContain('https://example.com/video.mp4'); }); it('generates empty XML node for invalid data', function () {