Skip to content

Commit

Permalink
OP-327: Spec & repo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jkindly committed Jul 12, 2024
1 parent e9185e7 commit 6c841bd
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
76 changes: 76 additions & 0 deletions spec/Form/DataTransformer/MultipleMediaToCodesTransformerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace spec\BitBag\SyliusCmsPlugin\Form\DataTransformer;

use BitBag\SyliusCmsPlugin\Entity\MediaInterface;
use BitBag\SyliusCmsPlugin\Form\DataTransformer\MultipleMediaToCodesTransformer;
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface;
use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;

final class MultipleMediaToCodesTransformerSpec extends ObjectBehavior
{
public function let(MediaRepositoryInterface $mediaRepository): void
{
$this->beConstructedWith($mediaRepository);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(MultipleMediaToCodesTransformer::class);
}

public function it_transforms_null_value_to_empty_collection(): void
{
$this->transform(null)->shouldBeAnInstanceOf(ArrayCollection::class);
$this->transform(null)->count()->shouldBe(0);
}

public function it_transforms_empty_array_to_empty_collection(): void
{
$this->transform([])->shouldBeAnInstanceOf(ArrayCollection::class);
$this->transform([])->count()->shouldBe(0);
}

public function it_transforms_non_empty_array_to_collection(
MediaRepositoryInterface $mediaRepository,
MediaInterface $media1,
MediaInterface $media2
): void
{
$mediaCodes = ['code1', 'code2'];
$mediaRepository->findBy(['code' => $mediaCodes])->willReturn([$media1, $media2]);

$this->transform($mediaCodes)->shouldBeAnInstanceOf(ArrayCollection::class);
$this->transform($mediaCodes)->shouldHaveCount(2);
$this->transform($mediaCodes)->toArray()->shouldBe([$media1, $media2]);
}

public function it_reverse_transforms_empty_collection_to_empty_array(): void
{
$collection = new ArrayCollection();
$this->reverseTransform($collection)->shouldBeArray();
$this->reverseTransform($collection)->shouldBe([]);
}

public function it_reverse_transforms_collection_to_array_of_media_codes(
MediaInterface $media1,
MediaInterface $media2
): void
{
$media1->getCode()->willReturn('code1');
$media2->getCode()->willReturn('code2');

$collection = new ArrayCollection([$media1->getWrappedObject(), $media2->getWrappedObject()]);

$this->reverseTransform($collection)->shouldBe(['code1', 'code2']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Sylius\Component\Addressing\Model\Country:
USA:
code: 'US'
Sylius\Component\Currency\Model\Currency:
dollar:
code: 'USD'
Sylius\Component\Locale\Model\Locale:
locale:
createdAt: '<dateTimeBetween("-200 days", "now")>'
code: 'en_US'
Sylius\Component\Core\Model\Channel:
channel:
code: "code"
name: "name"
locales:
- '@locale'
default_locale: '@locale'
tax_calculation_strategy: 'order_items_based'
base_currency: '@dollar'
BitBag\SyliusCmsPlugin\Entity\MediaTranslation:
media1_translation:
locale: 'en_US'
content: 'translation_content_en_US'
link: 'translation_link_en_US'
media2_translation:
locale: 'en_US'
content: 'translation_content_en_US'
link: 'translation_link_en_US'
media3_translation:
locale: 'en_US'
content: 'translation_content_en_US'
link: 'translation_link_en_US'
BitBag\SyliusCmsPlugin\Entity\Media:
media1:
code: 'media1-code'
name: 'media1-name'
enabled: true
type: 'image'
path: '/path/to/media1'
channels:
- '@channel'
translations:
- '@media1_translation'
media2:
code: 'media2-code'
name: 'media2-name'
enabled: true
type: 'image'
path: '/path/to/media2'
channels:
- '@channel'
translations:
- '@media2_translation'
media3:
code: 'media3-code'
name: 'media3-name'
enabled: false
type: 'image'
path: '/path/to/media3'
channels:
- '@channel'
translations:
- '@media3_translation'
13 changes: 13 additions & 0 deletions tests/Integration/Repository/MediaRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ public function test_it_finds_enabled_media_by_collection_code(): void
self::assertEmpty($media3);
}

public function test_it_finds_media_by_name_part(): void
{
$this->loadFixturesFromFile('MediaRepositoryTest/test_it_finds_media_by_name.yml');

$repository = $this->getRepository();

$phrase = 'media';
$media = $repository->findByNamePart($phrase);

self::assertIsArray($media);
self::assertCount(3, $media);
}

private function getRepository(): MediaRepositoryInterface
{
/** @var MediaRepositoryInterface $repository */
Expand Down

0 comments on commit 6c841bd

Please sign in to comment.