Skip to content

Commit f2da668

Browse files
committed
Add tests
1 parent 6560f91 commit f2da668

8 files changed

Lines changed: 736 additions & 12 deletions
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Loevgaard\SyliusBrandPlugin\Tests\EventSubscriber;
6+
7+
use Knp\Menu\ItemInterface;
8+
use Loevgaard\SyliusBrandPlugin\EventSubscriber\AdminMenuSubscriber;
9+
use PHPUnit\Framework\TestCase;
10+
use Prophecy\PhpUnit\ProphecyTrait;
11+
use Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent;
12+
13+
class AdminMenuSubscriberTest extends TestCase
14+
{
15+
use ProphecyTrait;
16+
17+
/** @test */
18+
public function it_subscribes_to_admin_menu_event(): void
19+
{
20+
$events = AdminMenuSubscriber::getSubscribedEvents();
21+
22+
self::assertArrayHasKey('sylius.menu.admin.main', $events);
23+
self::assertSame('add', $events['sylius.menu.admin.main']);
24+
}
25+
26+
/** @test */
27+
public function it_adds_brands_menu_item_to_catalog_section(): void
28+
{
29+
$brandsItem = $this->prophesize(ItemInterface::class);
30+
$brandsItem->setLabel('loevgaard_sylius_brand.ui.brands')->willReturn($brandsItem->reveal())->shouldBeCalledOnce();
31+
$brandsItem->setLabelAttribute('icon', 'building')->shouldBeCalledOnce();
32+
33+
$catalogItem = $this->prophesize(ItemInterface::class);
34+
$catalogItem->addChild('brands', ['route' => 'loevgaard_sylius_brand_admin_brand_index'])
35+
->willReturn($brandsItem->reveal())
36+
->shouldBeCalledOnce();
37+
38+
$menu = $this->prophesize(ItemInterface::class);
39+
$menu->getChild('catalog')->willReturn($catalogItem->reveal());
40+
41+
$event = $this->prophesize(MenuBuilderEvent::class);
42+
$event->getMenu()->willReturn($menu->reveal());
43+
44+
$subscriber = new AdminMenuSubscriber();
45+
$subscriber->add($event->reveal());
46+
}
47+
48+
/** @test */
49+
public function it_adds_brands_menu_item_to_first_child_when_catalog_does_not_exist(): void
50+
{
51+
$brandsItem = $this->prophesize(ItemInterface::class);
52+
$brandsItem->setLabel('loevgaard_sylius_brand.ui.brands')->willReturn($brandsItem->reveal())->shouldBeCalledOnce();
53+
$brandsItem->setLabelAttribute('icon', 'building')->shouldBeCalledOnce();
54+
55+
$firstChild = $this->prophesize(ItemInterface::class);
56+
$firstChild->addChild('brands', ['route' => 'loevgaard_sylius_brand_admin_brand_index'])
57+
->willReturn($brandsItem->reveal())
58+
->shouldBeCalledOnce();
59+
60+
$menu = $this->prophesize(ItemInterface::class);
61+
$menu->getChild('catalog')->willReturn(null);
62+
$menu->getFirstChild()->willReturn($firstChild->reveal());
63+
64+
$event = $this->prophesize(MenuBuilderEvent::class);
65+
$event->getMenu()->willReturn($menu->reveal());
66+
67+
$subscriber = new AdminMenuSubscriber();
68+
$subscriber->add($event->reveal());
69+
}
70+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Loevgaard\SyliusBrandPlugin\Tests\EventSubscriber;
6+
7+
use Loevgaard\SyliusBrandPlugin\EventSubscriber\BrandDeletionSubscriber;
8+
use Loevgaard\SyliusBrandPlugin\Model\BrandInterface;
9+
use PHPUnit\Framework\TestCase;
10+
use Prophecy\PhpUnit\ProphecyTrait;
11+
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
12+
use Sylius\Component\Core\Model\ProductInterface;
13+
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
14+
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
15+
16+
class BrandDeletionSubscriberTest extends TestCase
17+
{
18+
use ProphecyTrait;
19+
20+
/** @test */
21+
public function it_subscribes_to_brand_pre_delete_event(): void
22+
{
23+
$events = BrandDeletionSubscriber::getSubscribedEvents();
24+
25+
self::assertArrayHasKey('loevgaard_sylius_brand.brand.pre_delete', $events);
26+
self::assertSame('guard', $events['loevgaard_sylius_brand.brand.pre_delete']);
27+
}
28+
29+
/** @test */
30+
public function it_allows_deletion_when_brand_has_no_products(): void
31+
{
32+
$brand = $this->prophesize(BrandInterface::class);
33+
34+
$productRepository = $this->prophesize(ProductRepositoryInterface::class);
35+
$productRepository->findOneBy(['brand' => $brand->reveal()])->willReturn(null);
36+
37+
$event = $this->prophesize(ResourceControllerEvent::class);
38+
$event->getSubject()->willReturn($brand->reveal());
39+
$event->stop()->shouldNotBeCalled();
40+
41+
$subscriber = new BrandDeletionSubscriber($productRepository->reveal());
42+
$subscriber->guard($event->reveal());
43+
}
44+
45+
/** @test */
46+
public function it_stops_deletion_when_brand_is_used_in_products(): void
47+
{
48+
$brand = $this->prophesize(BrandInterface::class);
49+
$product = $this->prophesize(ProductInterface::class);
50+
51+
$productRepository = $this->prophesize(ProductRepositoryInterface::class);
52+
$productRepository->findOneBy(['brand' => $brand->reveal()])->willReturn($product->reveal());
53+
54+
$event = $this->prophesize(ResourceControllerEvent::class);
55+
$event->getSubject()->willReturn($brand->reveal());
56+
$event->stop('loevgaard_sylius_brand.brand.delete_error')->shouldBeCalledOnce();
57+
58+
$subscriber = new BrandDeletionSubscriber($productRepository->reveal());
59+
$subscriber->guard($event->reveal());
60+
}
61+
62+
/** @test */
63+
public function it_throws_exception_for_non_brand_subject(): void
64+
{
65+
$productRepository = $this->prophesize(ProductRepositoryInterface::class);
66+
67+
$event = $this->prophesize(ResourceControllerEvent::class);
68+
$event->getSubject()->willReturn(new \stdClass());
69+
70+
$subscriber = new BrandDeletionSubscriber($productRepository->reveal());
71+
72+
$this->expectException(UnexpectedTypeException::class);
73+
$subscriber->guard($event->reveal());
74+
}
75+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Loevgaard\SyliusBrandPlugin\Tests\EventSubscriber;
6+
7+
use Loevgaard\SyliusBrandPlugin\EventSubscriber\ImageUploadSubscriber;
8+
use PHPUnit\Framework\TestCase;
9+
use Prophecy\PhpUnit\ProphecyTrait;
10+
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
11+
use Sylius\Component\Core\Model\ImageInterface;
12+
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
13+
14+
class ImageUploadSubscriberTest extends TestCase
15+
{
16+
use ProphecyTrait;
17+
18+
/** @test */
19+
public function it_subscribes_to_brand_image_pre_create_event(): void
20+
{
21+
$events = ImageUploadSubscriber::getSubscribedEvents();
22+
23+
self::assertArrayHasKey('loevgaard_sylius_brand.brand_image.pre_create', $events);
24+
self::assertSame('uploadImage', $events['loevgaard_sylius_brand.brand_image.pre_create']);
25+
}
26+
27+
/** @test */
28+
public function it_uploads_image_when_file_exists(): void
29+
{
30+
$image = $this->prophesize(ImageInterface::class);
31+
$image->hasFile()->willReturn(true);
32+
$image->getPath()->willReturn('/path/to/image.jpg');
33+
34+
$uploader = $this->prophesize(ImageUploaderInterface::class);
35+
$uploader->upload($image->reveal())->shouldBeCalledOnce();
36+
37+
$event = $this->prophesize(ResourceControllerEvent::class);
38+
$event->getSubject()->willReturn($image->reveal());
39+
$event->stop()->shouldNotBeCalled();
40+
41+
$subscriber = new ImageUploadSubscriber($uploader->reveal());
42+
$subscriber->uploadImage($event->reveal());
43+
}
44+
45+
/** @test */
46+
public function it_does_not_upload_when_file_does_not_exist(): void
47+
{
48+
$image = $this->prophesize(ImageInterface::class);
49+
$image->hasFile()->willReturn(false);
50+
$image->getPath()->willReturn('/existing/path.jpg');
51+
52+
$uploader = $this->prophesize(ImageUploaderInterface::class);
53+
$uploader->upload()->shouldNotBeCalled();
54+
55+
$event = $this->prophesize(ResourceControllerEvent::class);
56+
$event->getSubject()->willReturn($image->reveal());
57+
$event->stop()->shouldNotBeCalled();
58+
59+
$subscriber = new ImageUploadSubscriber($uploader->reveal());
60+
$subscriber->uploadImage($event->reveal());
61+
}
62+
63+
/** @test */
64+
public function it_stops_event_when_path_is_null_after_upload(): void
65+
{
66+
$image = $this->prophesize(ImageInterface::class);
67+
$image->hasFile()->willReturn(true);
68+
$image->getPath()->willReturn(null);
69+
70+
$uploader = $this->prophesize(ImageUploaderInterface::class);
71+
$uploader->upload($image->reveal())->shouldBeCalledOnce();
72+
73+
$event = $this->prophesize(ResourceControllerEvent::class);
74+
$event->getSubject()->willReturn($image->reveal());
75+
$event->stop('loevgaard_sylius_brand.brand_image.upload_error')->shouldBeCalledOnce();
76+
77+
$subscriber = new ImageUploadSubscriber($uploader->reveal());
78+
$subscriber->uploadImage($event->reveal());
79+
}
80+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Loevgaard\SyliusBrandPlugin\Tests\Factory;
6+
7+
use Loevgaard\SyliusBrandPlugin\Factory\BrandImageFactory;
8+
use Loevgaard\SyliusBrandPlugin\Model\BrandImage;
9+
use Loevgaard\SyliusBrandPlugin\Model\BrandImageInterface;
10+
use Loevgaard\SyliusBrandPlugin\Model\BrandInterface;
11+
use PHPUnit\Framework\TestCase;
12+
use Prophecy\PhpUnit\ProphecyTrait;
13+
use Sylius\Component\Resource\Factory\FactoryInterface;
14+
15+
class BrandImageFactoryTest extends TestCase
16+
{
17+
use ProphecyTrait;
18+
19+
/** @test */
20+
public function it_creates_new_brand_image(): void
21+
{
22+
$brandImage = new BrandImage();
23+
24+
$innerFactory = $this->prophesize(FactoryInterface::class);
25+
$innerFactory->createNew()->willReturn($brandImage);
26+
27+
$factory = new BrandImageFactory($innerFactory->reveal());
28+
29+
$result = $factory->createNew();
30+
31+
self::assertSame($brandImage, $result);
32+
}
33+
34+
/** @test */
35+
public function it_creates_brand_image_for_brand(): void
36+
{
37+
$brand = $this->prophesize(BrandInterface::class);
38+
$brandImage = $this->prophesize(BrandImageInterface::class);
39+
40+
$brandImage->setBrand($brand->reveal())->shouldBeCalledOnce();
41+
42+
$innerFactory = $this->prophesize(FactoryInterface::class);
43+
$innerFactory->createNew()->willReturn($brandImage->reveal());
44+
45+
$factory = new BrandImageFactory($innerFactory->reveal());
46+
47+
$result = $factory->createForBrand($brand->reveal());
48+
49+
self::assertSame($brandImage->reveal(), $result);
50+
}
51+
}

0 commit comments

Comments
 (0)