Skip to content

Commit 914caf5

Browse files
committed
Add tests
1 parent 6e10b62 commit 914caf5

11 files changed

+1077
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusSEOPlugin\Tests\Event;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Prophecy\PhpUnit\ProphecyTrait;
9+
use Setono\SyliusSEOPlugin\Event\OnlineStoreAddedToGraph;
10+
use Spatie\SchemaOrg\OnlineStore;
11+
use Sylius\Component\Core\Model\ChannelInterface;
12+
13+
final class OnlineStoreAddedToGraphTest extends TestCase
14+
{
15+
use ProphecyTrait;
16+
17+
/**
18+
* @test
19+
*/
20+
public function it_holds_online_store_and_channel(): void
21+
{
22+
$onlineStore = new OnlineStore();
23+
$channel = $this->prophesize(ChannelInterface::class);
24+
25+
$event = new OnlineStoreAddedToGraph($onlineStore, $channel->reveal());
26+
27+
self::assertSame($onlineStore, $event->onlineStore);
28+
self::assertSame($channel->reveal(), $event->channel);
29+
}
30+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusSEOPlugin\Tests\Event;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Prophecy\PhpUnit\ProphecyTrait;
9+
use Setono\SyliusSEOPlugin\Event\ProductAddedToGraph;
10+
use Spatie\SchemaOrg\Product;
11+
use Sylius\Component\Product\Model\ProductInterface;
12+
use Sylius\Component\Product\Model\ProductVariantInterface;
13+
14+
final class ProductAddedToGraphTest extends TestCase
15+
{
16+
use ProphecyTrait;
17+
18+
/**
19+
* @test
20+
*/
21+
public function it_holds_product_only(): void
22+
{
23+
$product = new Product();
24+
25+
$event = new ProductAddedToGraph($product);
26+
27+
self::assertSame($product, $event->product);
28+
self::assertNull($event->storeProduct);
29+
self::assertNull($event->storeProductVariant);
30+
}
31+
32+
/**
33+
* @test
34+
*/
35+
public function it_holds_product_with_store_product(): void
36+
{
37+
$product = new Product();
38+
$storeProduct = $this->prophesize(ProductInterface::class);
39+
40+
$event = new ProductAddedToGraph($product, $storeProduct->reveal());
41+
42+
self::assertSame($product, $event->product);
43+
self::assertSame($storeProduct->reveal(), $event->storeProduct);
44+
self::assertNull($event->storeProductVariant);
45+
}
46+
47+
/**
48+
* @test
49+
*/
50+
public function it_holds_product_with_store_product_variant(): void
51+
{
52+
$product = new Product();
53+
$storeProduct = $this->prophesize(ProductInterface::class);
54+
$storeProductVariant = $this->prophesize(ProductVariantInterface::class);
55+
56+
$event = new ProductAddedToGraph($product, $storeProduct->reveal(), $storeProductVariant->reveal());
57+
58+
self::assertSame($product, $event->product);
59+
self::assertSame($storeProduct->reveal(), $event->storeProduct);
60+
self::assertSame($storeProductVariant->reveal(), $event->storeProductVariant);
61+
}
62+
63+
/**
64+
* @test
65+
*/
66+
public function it_holds_product_with_only_variant(): void
67+
{
68+
$product = new Product();
69+
$storeProductVariant = $this->prophesize(ProductVariantInterface::class);
70+
71+
$event = new ProductAddedToGraph($product, null, $storeProductVariant->reveal());
72+
73+
self::assertSame($product, $event->product);
74+
self::assertNull($event->storeProduct);
75+
self::assertSame($storeProductVariant->reveal(), $event->storeProductVariant);
76+
}
77+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusSEOPlugin\Tests\Event;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Prophecy\PhpUnit\ProphecyTrait;
9+
use Setono\SyliusSEOPlugin\Event\ProductGroupAddedToGraph;
10+
use Spatie\SchemaOrg\ProductGroup;
11+
use Sylius\Component\Product\Model\ProductInterface;
12+
13+
final class ProductGroupAddedToGraphTest extends TestCase
14+
{
15+
use ProphecyTrait;
16+
17+
/**
18+
* @test
19+
*/
20+
public function it_holds_product_group_only(): void
21+
{
22+
$productGroup = new ProductGroup();
23+
24+
$event = new ProductGroupAddedToGraph($productGroup);
25+
26+
self::assertSame($productGroup, $event->productGroup);
27+
self::assertNull($event->storeProduct);
28+
}
29+
30+
/**
31+
* @test
32+
*/
33+
public function it_holds_product_group_with_store_product(): void
34+
{
35+
$productGroup = new ProductGroup();
36+
$storeProduct = $this->prophesize(ProductInterface::class);
37+
38+
$event = new ProductGroupAddedToGraph($productGroup, $storeProduct->reveal());
39+
40+
self::assertSame($productGroup, $event->productGroup);
41+
self::assertSame($storeProduct->reveal(), $event->storeProduct);
42+
}
43+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusSEOPlugin\Tests\Event;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Prophecy\PhpUnit\ProphecyTrait;
9+
use Setono\SyliusSEOPlugin\Event\WebsiteAddedToGraph;
10+
use Spatie\SchemaOrg\WebSite;
11+
use Sylius\Component\Core\Model\ChannelInterface;
12+
13+
final class WebsiteAddedToGraphTest extends TestCase
14+
{
15+
use ProphecyTrait;
16+
17+
/**
18+
* @test
19+
*/
20+
public function it_holds_website_and_channel(): void
21+
{
22+
$webSite = new WebSite();
23+
$channel = $this->prophesize(ChannelInterface::class);
24+
25+
$event = new WebsiteAddedToGraph($webSite, $channel->reveal());
26+
27+
self::assertSame($webSite, $event->webSite);
28+
self::assertSame($channel->reveal(), $event->channel);
29+
}
30+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusSEOPlugin\Tests\EventSubscriber\OpenGraph;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Prophecy\PhpUnit\ProphecyTrait;
9+
use Setono\SyliusSEOPlugin\EventSubscriber\OpenGraph\AddChannelInformationSubscriber;
10+
use Setono\SyliusSEOPlugin\OpenGraph\OpenGraph;
11+
use Sylius\Component\Channel\Context\ChannelContextInterface;
12+
use Sylius\Component\Channel\Model\ChannelInterface;
13+
use Sylius\Component\Locale\Context\LocaleContextInterface;
14+
use Symfony\Component\HttpKernel\KernelEvents;
15+
16+
final class AddChannelInformationSubscriberTest extends TestCase
17+
{
18+
use ProphecyTrait;
19+
20+
/**
21+
* @test
22+
*/
23+
public function it_subscribes_to_kernel_request(): void
24+
{
25+
$events = AddChannelInformationSubscriber::getSubscribedEvents();
26+
27+
self::assertArrayHasKey(KernelEvents::REQUEST, $events);
28+
self::assertSame('add', $events[KernelEvents::REQUEST]);
29+
}
30+
31+
/**
32+
* @test
33+
*/
34+
public function it_adds_channel_information_to_open_graph(): void
35+
{
36+
$channel = $this->prophesize(ChannelInterface::class);
37+
$channel->getName()->willReturn('My Store');
38+
39+
$channelContext = $this->prophesize(ChannelContextInterface::class);
40+
$channelContext->getChannel()->willReturn($channel->reveal());
41+
42+
$localeContext = $this->prophesize(LocaleContextInterface::class);
43+
$localeContext->getLocaleCode()->willReturn('en_US');
44+
45+
$openGraph = new OpenGraph();
46+
47+
$subscriber = new AddChannelInformationSubscriber(
48+
$channelContext->reveal(),
49+
$localeContext->reveal(),
50+
$openGraph,
51+
);
52+
53+
$subscriber->add();
54+
55+
self::assertSame('My Store', $openGraph->getSiteName());
56+
self::assertSame('en_US', $openGraph->getLocale());
57+
}
58+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusSEOPlugin\Tests\EventSubscriber\OpenGraph;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Prophecy\PhpUnit\ProphecyTrait;
9+
use Setono\SyliusSEOPlugin\EventSubscriber\OpenGraph\AddProductInformationSubscriber;
10+
use Setono\SyliusSEOPlugin\OpenGraph\OpenGraph;
11+
use Setono\SyliusSEOPlugin\Resolver\ProductImagesResolverInterface;
12+
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
13+
use Sylius\Component\Core\Model\ProductInterface;
14+
15+
final class AddProductInformationSubscriberTest extends TestCase
16+
{
17+
use ProphecyTrait;
18+
19+
/**
20+
* @test
21+
*/
22+
public function it_subscribes_to_product_show(): void
23+
{
24+
$events = AddProductInformationSubscriber::getSubscribedEvents();
25+
26+
self::assertArrayHasKey('sylius.product.show', $events);
27+
self::assertSame('add', $events['sylius.product.show']);
28+
}
29+
30+
/**
31+
* @test
32+
*/
33+
public function it_adds_product_information_with_image(): void
34+
{
35+
$product = $this->prophesize(ProductInterface::class);
36+
$product->getName()->willReturn('Cool Product');
37+
$product->getDescription()->willReturn('A cool description');
38+
39+
$productImagesResolver = $this->prophesize(ProductImagesResolverInterface::class);
40+
$productImagesResolver->resolve($product->reveal())
41+
->willReturn(['https://example.com/image.jpg']);
42+
43+
$openGraph = new OpenGraph();
44+
45+
$event = $this->prophesize(ResourceControllerEvent::class);
46+
$event->getSubject()->willReturn($product->reveal());
47+
48+
$subscriber = new AddProductInformationSubscriber(
49+
$productImagesResolver->reveal(),
50+
$openGraph,
51+
);
52+
53+
$subscriber->add($event->reveal());
54+
55+
self::assertSame('A cool description', $openGraph->getDescription());
56+
self::assertCount(1, $openGraph->getImages());
57+
}
58+
59+
/**
60+
* @test
61+
*/
62+
public function it_adds_product_information_without_image(): void
63+
{
64+
$product = $this->prophesize(ProductInterface::class);
65+
$product->getDescription()->willReturn('A product without images');
66+
67+
$productImagesResolver = $this->prophesize(ProductImagesResolverInterface::class);
68+
$productImagesResolver->resolve($product->reveal())->willReturn([]);
69+
70+
$openGraph = new OpenGraph();
71+
72+
$event = $this->prophesize(ResourceControllerEvent::class);
73+
$event->getSubject()->willReturn($product->reveal());
74+
75+
$subscriber = new AddProductInformationSubscriber(
76+
$productImagesResolver->reveal(),
77+
$openGraph,
78+
);
79+
80+
$subscriber->add($event->reveal());
81+
82+
self::assertSame('A product without images', $openGraph->getDescription());
83+
self::assertCount(0, $openGraph->getImages());
84+
}
85+
}

0 commit comments

Comments
 (0)