Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ migrations:
workbench:
install: false
discovers:
web: false
web: true
api: false
config: true
commands: false
Expand Down
13 changes: 13 additions & 0 deletions tests/.pest/snapshots/Feature/SitemapTest/export.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<url>
<loc>http://localhost/products/GD-PRDCT-1</loc>
<lastmod>2025-08-31T20:00:00+00:00</lastmod>
<priority>0.9</priority>
</url>
<url>
<loc>http://localhost/products/GD-PRDCT-2</loc>
<lastmod>2025-08-30T19:00:00+00:00</lastmod>
<priority>0.9</priority>
</url>
</urlset>
51 changes: 51 additions & 0 deletions tests/.pest/snapshots/Feature/YandexTest/export.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE yml_catalog SYSTEM "shops.dtd">
<yml_catalog date="2025-08-30T21:14:49+00:00">
<shop>
<name>Laravel</name>
<company>Laravel</company>
<url>http://localhost</url>
<platform>Laravel</platform>
<email>test@example.com</email>
<currencies>
<currency id="RUR" rate="1"/>
</currencies>
<categories>
<category id="41">Домашние майки</category>
<category id="539">Велосипедки</category>
<category id="44">Ремни</category>
</categories>
<offers>
<offer id="1" available="true" type="vendor.model">
<url>http://localhost/products/GD-PRDCT-1</url>
<barcode>GD-PRDCT-1</barcode>
<name>Some 1</name>
<description>Some description 1</description>
<delivery>true</delivery>
<price>100</price>
<currencyId>RUR</currencyId>
<vendor>The Best</vendor>
</offer>
<offer id="2" available="true" type="vendor.model">
<url>http://localhost/products/GD-PRDCT-2</url>
<barcode>GD-PRDCT-2</barcode>
<name>Some 2</name>
<description>Some description 2</description>
<delivery>true</delivery>
<price>250</price>
<currencyId>RUR</currencyId>
<vendor>The Best</vendor>
</offer>
<offer id="3" available="false" type="vendor.model">
<url>http://localhost/products/GD-PRDCT-3</url>
<barcode>GD-PRDCT-3</barcode>
<name>Some 3</name>
<description>Some description 3</description>
<delivery>true</delivery>
<price>400</price>
<currencyId>RUR</currencyId>
<vendor>The Best</vendor>
</offer>
</offers>

</shop>
</yml_catalog>
5 changes: 1 addition & 4 deletions tests/Feature/FullTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
use Workbench\App\Data\NewsFakeData;
use Workbench\App\Feeds\FilledFeed;
use Workbench\App\Models\News;

use function Pest\Laravel\artisan;

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

News::factory()->count(3)->sequence(
...NewsFakeData::toArray()
)->createMany();
createNews(...NewsFakeData::toArray());

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

Expand Down
9 changes: 3 additions & 6 deletions tests/Feature/PartialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@
use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
use Workbench\App\Data\NewsFakeData;
use Workbench\App\Feeds\FilledFeed;
use Workbench\App\Models\News;

use function Pest\Laravel\artisan;

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

News::factory()->count(5)->sequence(fn () => [
createNews(static fn () => [
'updated_at' => fake()->dateTimeBetween(endDate: '-1 month'),
])->createMany();
]);

News::factory()->count(3)->sequence(
...NewsFakeData::toArray()
)->createMany();
createNews(...NewsFakeData::toArray());

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

Expand Down
19 changes: 19 additions & 0 deletions tests/Feature/SitemapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
use Workbench\App\Feeds\SitemapFeed;

use function Pest\Laravel\artisan;

test('export', function () {
createProducts();

$feed = app()->make(SitemapFeed::class);

artisan(FeedGenerateCommand::class)->run();

expect($feed->path())->toBeReadableFile();
expect(file_get_contents($feed->path()))->toMatchSnapshot();
});
19 changes: 19 additions & 0 deletions tests/Feature/YandexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
use Workbench\App\Feeds\YandexFeed;

use function Pest\Laravel\artisan;

test('export', function () {
createProducts();

$feed = app()->make(YandexFeed::class);

artisan(FeedGenerateCommand::class)->run();

expect($feed->path())->toBeReadableFile();
expect(file_get_contents($feed->path()))->toMatchSnapshot();
});
21 changes: 21 additions & 0 deletions tests/Helpers/models.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use Workbench\App\Data\ProductFakeData;
use Workbench\App\Models\News;
use Workbench\App\Models\Product;

function createNews(...$sequence): void
{
News::factory()->count(3)->sequence(
...$sequence
)->createMany();
}

function createProducts(): void
{
Product::factory()->count(3)->sequence(
...ProductFakeData::toArray()
)->create();
}
73 changes: 73 additions & 0 deletions workbench/app/Data/ProductFakeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Workbench\App\Data;

use function json_encode;

class ProductFakeData
{
public static function toArray(): array
{
return [
[
'article' => 'GD-PRDCT-1',
'title' => 'Some 1',
'description' => 'Some description 1',

'price' => 100,
'quantity' => 5,
'currency' => 'USD',

'brand' => 'The Best',

'images' => json_encode([
'https://via.placeholder.com/640x480.png/00ff55?text=s1',
]),

'created_at' => '2025-08-31 00:00:00',
'updated_at' => '2025-08-31 20:00:00',
],
[
'article' => 'GD-PRDCT-2',
'title' => 'Some 2',
'description' => 'Some description 2',

'price' => 250,
'quantity' => 20,
'currency' => 'USD',

'brand' => 'The Best',

'images' => json_encode([
'https://via.placeholder.com/640x480.png/00ff55?text=s20',
'https://via.placeholder.com/640x480.png/00ff55?text=s21',
'https://via.placeholder.com/640x480.png/00ff55?text=s22',
]),

'created_at' => '2025-08-30 00:00:00',
'updated_at' => '2025-08-30 19:00:00',
],
[
'article' => 'GD-PRDCT-3',
'title' => 'Some 3',
'description' => 'Some description 3',

'price' => 400,
'quantity' => 0,
'currency' => 'USD',

'brand' => 'The Best',

'images' => json_encode([
'https://via.placeholder.com/640x480.png/00ff55?text=s30',
'https://via.placeholder.com/640x480.png/00ff55?text=s31',
]),

'created_at' => '2025-08-29 00:00:00',
'updated_at' => '2025-08-29 18:00:00',
],
];
}
}
27 changes: 27 additions & 0 deletions workbench/app/Feeds/Items/SitemapFeedItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Workbench\App\Feeds\Items;

use DragonCode\LaravelFeed\Feeds\Items\FeedItem;

/** @property-read \Workbench\App\Models\Product $model */
class SitemapFeedItem extends FeedItem
{
public function name(): string
{
return 'url';
}

public function toArray(): array
{
return [
'loc' => route('products.show', $this->model->article),

'lastmod' => $this->model->updated_at->toIso8601String(),

'priority' => 0.9,
];
}
}
46 changes: 46 additions & 0 deletions workbench/app/Feeds/Items/YandexFeedItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Workbench\App\Feeds\Items;

use DragonCode\LaravelFeed\Feeds\Items\FeedItem;

/** @property-read \Workbench\App\Models\Product $model */
class YandexFeedItem extends FeedItem
{
public function name(): string
{
return 'offer';
}

public function attributes(): array
{
return [
'id' => $this->model->id,

'available' => ! empty($this->model->quantity) ? 'true' : 'false',

'type' => 'vendor.model',
];
}

public function toArray(): array
{
return [
'url' => route('products.show', $this->model->article),

'barcode' => $this->model->article,
'name' => $this->model->title,
'description' => $this->model->description,

'delivery' => 'true',
'price' => $this->model->price,

'currencyId' => 'RUR',
'vendor' => $this->model->brand,

// 'picture' => $this->model->images,
];
}
}
49 changes: 49 additions & 0 deletions workbench/app/Feeds/SitemapFeed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Workbench\App\Feeds;

use DragonCode\LaravelFeed\Data\ElementData;
use DragonCode\LaravelFeed\Feeds\Feed;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Workbench\App\Feeds\Items\SitemapFeedItem;
use Workbench\App\Models\Product;

class SitemapFeed extends Feed
{
protected string $name = 'urlset';

protected array $attributes = [
'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',
];

public function builder(): Builder
{
return Product::query()->where('quantity', '>', 0);
}

public function root(): ElementData
{
return new ElementData(
$this->name,
$this->attributes
);
}

public function filename(): string
{
return 'sitemaps/products.xml';
}

public function item(Model $model): FeedItem
{
return new SitemapFeedItem($model);
}
}
Loading