Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomano committed Jan 29, 2025
1 parent 677a6ed commit ae886c6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 27 deletions.
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
</include>
<exclude>
<directory>src/Sources/Optimus/Addons</directory>
<directory>src/Sources/Optimus/Events</directory>
<directory>src/Sources/Optimus/Libs</directory>
<file>src/Sources/Optimus/Handlers/index.php</file>
<file>src/Sources/Optimus/Enums/index.php</file>
<file>src/Sources/Optimus/Events/index.php</file>
<file>src/Sources/Optimus/Services/index.php</file>
<file>src/Sources/Optimus/Tasks/index.php</file>
<file>src/Sources/Optimus/Utils/index.php</file>
Expand Down
35 changes: 35 additions & 0 deletions tests/Enums/FrequencyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

use Bugo\Optimus\Enums\Frequency;

it('returns Hourly for timestamps within the last 24 hours', function () {
$timestamp = time() - (23 * 3600); // 23 hours ago
expect(Frequency::fromTimestamp($timestamp))->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
});
29 changes: 29 additions & 0 deletions tests/Enums/PriorityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

use Bugo\Optimus\Enums\Priority;

it('returns Prime for timestamps within 30 days', function () {
$timestamp = time() - (29 * 86400);
expect(Priority::fromTimestamp($timestamp))->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);
});
23 changes: 0 additions & 23 deletions tests/Services/SitemapGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
8 changes: 5 additions & 3 deletions tests/Services/XmlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<mobile:loc>https://example.com/mobile</mobile:loc>')
->and($xml)->toContain('<image:loc>https://example.com/image.png</image:loc>')
->and($xml)->toContain('<mobile:loc>https://example.com/mobile</mobile:loc>');
->and($xml)->toContain('<video:content_loc>https://example.com/video.mp4</video:content_loc>');
});

it('generates empty XML node for invalid data', function () {
Expand Down

0 comments on commit ae886c6

Please sign in to comment.