Skip to content

Commit e3e0465

Browse files
Merge pull request #22 from TheDragonCode/1.x
Added tests for real feeds
2 parents 3ecc184 + 344e6ba commit e3e0465

File tree

20 files changed

+505
-16
lines changed

20 files changed

+505
-16
lines changed

testbench.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ migrations:
99
workbench:
1010
install: false
1111
discovers:
12-
web: false
12+
web: true
1313
api: false
1414
config: true
1515
commands: false
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
3+
<url>
4+
<loc>http://localhost/products/GD-PRDCT-1</loc>
5+
<lastmod>2025-08-31T20:00:00+00:00</lastmod>
6+
<priority>0.9</priority>
7+
</url>
8+
<url>
9+
<loc>http://localhost/products/GD-PRDCT-2</loc>
10+
<lastmod>2025-08-30T19:00:00+00:00</lastmod>
11+
<priority>0.9</priority>
12+
</url>
13+
</urlset>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!DOCTYPE yml_catalog SYSTEM "shops.dtd">
2+
<yml_catalog date="2025-08-30T21:14:49+00:00">
3+
<shop>
4+
<name>Laravel</name>
5+
<company>Laravel</company>
6+
<url>http://localhost</url>
7+
<platform>Laravel</platform>
8+
<email>[email protected]</email>
9+
<currencies>
10+
<currency id="RUR" rate="1"/>
11+
</currencies>
12+
<categories>
13+
<category id="41">Домашние майки</category>
14+
<category id="539">Велосипедки</category>
15+
<category id="44">Ремни</category>
16+
</categories>
17+
<offers>
18+
<offer id="1" available="true" type="vendor.model">
19+
<url>http://localhost/products/GD-PRDCT-1</url>
20+
<barcode>GD-PRDCT-1</barcode>
21+
<name>Some 1</name>
22+
<description>Some description 1</description>
23+
<delivery>true</delivery>
24+
<price>100</price>
25+
<currencyId>RUR</currencyId>
26+
<vendor>The Best</vendor>
27+
</offer>
28+
<offer id="2" available="true" type="vendor.model">
29+
<url>http://localhost/products/GD-PRDCT-2</url>
30+
<barcode>GD-PRDCT-2</barcode>
31+
<name>Some 2</name>
32+
<description>Some description 2</description>
33+
<delivery>true</delivery>
34+
<price>250</price>
35+
<currencyId>RUR</currencyId>
36+
<vendor>The Best</vendor>
37+
</offer>
38+
<offer id="3" available="false" type="vendor.model">
39+
<url>http://localhost/products/GD-PRDCT-3</url>
40+
<barcode>GD-PRDCT-3</barcode>
41+
<name>Some 3</name>
42+
<description>Some description 3</description>
43+
<delivery>true</delivery>
44+
<price>400</price>
45+
<currencyId>RUR</currencyId>
46+
<vendor>The Best</vendor>
47+
</offer>
48+
</offers>
49+
50+
</shop>
51+
</yml_catalog>

tests/Feature/FullTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
66
use Workbench\App\Data\NewsFakeData;
77
use Workbench\App\Feeds\FilledFeed;
8-
use Workbench\App\Models\News;
98

109
use function Pest\Laravel\artisan;
1110

1211
test('export', function (bool $pretty) {
1312
setPrettyXml($pretty);
1413

15-
News::factory()->count(3)->sequence(
16-
...NewsFakeData::toArray()
17-
)->createMany();
14+
createNews(...NewsFakeData::toArray());
1815

1916
$feed = app()->make(FilledFeed::class);
2017

tests/Feature/PartialTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@
55
use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
66
use Workbench\App\Data\NewsFakeData;
77
use Workbench\App\Feeds\FilledFeed;
8-
use Workbench\App\Models\News;
98

109
use function Pest\Laravel\artisan;
1110

1211
test('export', function (bool $pretty) {
1312
setPrettyXml($pretty);
1413

15-
News::factory()->count(5)->sequence(fn () => [
14+
createNews(static fn () => [
1615
'updated_at' => fake()->dateTimeBetween(endDate: '-1 month'),
17-
])->createMany();
16+
]);
1817

19-
News::factory()->count(3)->sequence(
20-
...NewsFakeData::toArray()
21-
)->createMany();
18+
createNews(...NewsFakeData::toArray());
2219

2320
$feed = app()->make(FilledFeed::class);
2421

tests/Feature/SitemapTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
6+
use Workbench\App\Feeds\SitemapFeed;
7+
8+
use function Pest\Laravel\artisan;
9+
10+
test('export', function () {
11+
createProducts();
12+
13+
$feed = app()->make(SitemapFeed::class);
14+
15+
artisan(FeedGenerateCommand::class)->run();
16+
17+
expect($feed->path())->toBeReadableFile();
18+
expect(file_get_contents($feed->path()))->toMatchSnapshot();
19+
});

tests/Feature/YandexTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
6+
use Workbench\App\Feeds\YandexFeed;
7+
8+
use function Pest\Laravel\artisan;
9+
10+
test('export', function () {
11+
createProducts();
12+
13+
$feed = app()->make(YandexFeed::class);
14+
15+
artisan(FeedGenerateCommand::class)->run();
16+
17+
expect($feed->path())->toBeReadableFile();
18+
expect(file_get_contents($feed->path()))->toMatchSnapshot();
19+
});

tests/Helpers/models.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Workbench\App\Data\ProductFakeData;
6+
use Workbench\App\Models\News;
7+
use Workbench\App\Models\Product;
8+
9+
function createNews(...$sequence): void
10+
{
11+
News::factory()->count(3)->sequence(
12+
...$sequence
13+
)->createMany();
14+
}
15+
16+
function createProducts(): void
17+
{
18+
Product::factory()->count(3)->sequence(
19+
...ProductFakeData::toArray()
20+
)->create();
21+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Workbench\App\Data;
6+
7+
use function json_encode;
8+
9+
class ProductFakeData
10+
{
11+
public static function toArray(): array
12+
{
13+
return [
14+
[
15+
'article' => 'GD-PRDCT-1',
16+
'title' => 'Some 1',
17+
'description' => 'Some description 1',
18+
19+
'price' => 100,
20+
'quantity' => 5,
21+
'currency' => 'USD',
22+
23+
'brand' => 'The Best',
24+
25+
'images' => json_encode([
26+
'https://via.placeholder.com/640x480.png/00ff55?text=s1',
27+
]),
28+
29+
'created_at' => '2025-08-31 00:00:00',
30+
'updated_at' => '2025-08-31 20:00:00',
31+
],
32+
[
33+
'article' => 'GD-PRDCT-2',
34+
'title' => 'Some 2',
35+
'description' => 'Some description 2',
36+
37+
'price' => 250,
38+
'quantity' => 20,
39+
'currency' => 'USD',
40+
41+
'brand' => 'The Best',
42+
43+
'images' => json_encode([
44+
'https://via.placeholder.com/640x480.png/00ff55?text=s20',
45+
'https://via.placeholder.com/640x480.png/00ff55?text=s21',
46+
'https://via.placeholder.com/640x480.png/00ff55?text=s22',
47+
]),
48+
49+
'created_at' => '2025-08-30 00:00:00',
50+
'updated_at' => '2025-08-30 19:00:00',
51+
],
52+
[
53+
'article' => 'GD-PRDCT-3',
54+
'title' => 'Some 3',
55+
'description' => 'Some description 3',
56+
57+
'price' => 400,
58+
'quantity' => 0,
59+
'currency' => 'USD',
60+
61+
'brand' => 'The Best',
62+
63+
'images' => json_encode([
64+
'https://via.placeholder.com/640x480.png/00ff55?text=s30',
65+
'https://via.placeholder.com/640x480.png/00ff55?text=s31',
66+
]),
67+
68+
'created_at' => '2025-08-29 00:00:00',
69+
'updated_at' => '2025-08-29 18:00:00',
70+
],
71+
];
72+
}
73+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Workbench\App\Feeds\Items;
6+
7+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
8+
9+
/** @property-read \Workbench\App\Models\Product $model */
10+
class SitemapFeedItem extends FeedItem
11+
{
12+
public function name(): string
13+
{
14+
return 'url';
15+
}
16+
17+
public function toArray(): array
18+
{
19+
return [
20+
'loc' => route('products.show', $this->model->article),
21+
22+
'lastmod' => $this->model->updated_at->toIso8601String(),
23+
24+
'priority' => 0.9,
25+
];
26+
}
27+
}

0 commit comments

Comments
 (0)