|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Answear\GetdressedMeBundle\Tests\Response; |
| 6 | + |
| 7 | +use Answear\GetdressedMeBundle\Enum\OutfitsResponsePropertyEnum; |
| 8 | +use Answear\GetdressedMeBundle\Enum\ProductPropertyEnum; |
| 9 | +use Answear\GetdressedMeBundle\Response\OutfitsResponse; |
| 10 | +use Answear\GetdressedMeBundle\ValueObject\Product; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Webmozart\Assert\InvalidArgumentException; |
| 13 | + |
| 14 | +class OutfitsResponseTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @test |
| 18 | + */ |
| 19 | + public function validObjectCreation(): void |
| 20 | + { |
| 21 | + $outfitsResponse = OutfitsResponse::fromArray($this->getValidData()); |
| 22 | + self::assertInstanceOf(OutfitsResponse::class, $outfitsResponse); |
| 23 | + |
| 24 | + self::assertEquals($this->getExpectedProduct(), $this->getComparisonArrayProduct($outfitsResponse->getProduct())); |
| 25 | + |
| 26 | + $outfits = $outfitsResponse->getOutfits(); |
| 27 | + self::assertCount(3, $outfits); |
| 28 | + self::assertCount(2, $outfits[0]->getProducts()); |
| 29 | + self::assertCount(4, $outfits[1]->getProducts()); |
| 30 | + self::assertCount(2, $outfits[2]->getProducts()); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @test |
| 35 | + * @dataProvider invalidDataProvider |
| 36 | + */ |
| 37 | + public function invalidObjectCreation(array $invalidData): void |
| 38 | + { |
| 39 | + $this->expectException(InvalidArgumentException::class); |
| 40 | + |
| 41 | + OutfitsResponse::fromArray($invalidData); |
| 42 | + } |
| 43 | + |
| 44 | + public function getValidData(): array |
| 45 | + { |
| 46 | + $jsonFile = __DIR__ . '/../DataFixtures/getdressedme.json'; |
| 47 | + |
| 48 | + return \json_decode(file_get_contents($jsonFile), true, 512, JSON_THROW_ON_ERROR); |
| 49 | + } |
| 50 | + |
| 51 | + public function invalidDataProvider(): iterable |
| 52 | + { |
| 53 | + yield 'no outfits key' => [ |
| 54 | + [OutfitsResponsePropertyEnum::PRODUCT => $this->getProductData()], |
| 55 | + ]; |
| 56 | + |
| 57 | + yield 'outfits not array' => [ |
| 58 | + [ |
| 59 | + OutfitsResponsePropertyEnum::PRODUCT => $this->getProductData(), |
| 60 | + OutfitsResponsePropertyEnum::OUTFITS => 'invalid type', |
| 61 | + ], |
| 62 | + ]; |
| 63 | + |
| 64 | + yield 'no product key' => [ |
| 65 | + [OutfitsResponsePropertyEnum::OUTFITS => []], |
| 66 | + ]; |
| 67 | + |
| 68 | + yield 'product not array' => [ |
| 69 | + [ |
| 70 | + OutfitsResponsePropertyEnum::PRODUCT => 'invalid type', |
| 71 | + OutfitsResponsePropertyEnum::OUTFITS => [], |
| 72 | + ], |
| 73 | + ]; |
| 74 | + } |
| 75 | + |
| 76 | + private function getProductData(): array |
| 77 | + { |
| 78 | + return [ |
| 79 | + ProductPropertyEnum::CLIENT_PRODUCT_ID => '123', |
| 80 | + ProductPropertyEnum::PRODUCT_NAME => 'My product name', |
| 81 | + ProductPropertyEnum::PRODUCT_NAME_LANG => 'pl', |
| 82 | + ProductPropertyEnum::PRODUCT_PAGE_URL => 'https://my-site.local/my-product-123', |
| 83 | + ProductPropertyEnum::MAIN_IMAGE_URL => 'https://img.my-site.local/my-product-123.jpg', |
| 84 | + ProductPropertyEnum::PRICE => 123.99, |
| 85 | + ProductPropertyEnum::PRICE_CURRENCY => 'PLN', |
| 86 | + ProductPropertyEnum::SALE_PRICE => 123.99, |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + private function getExpectedProduct(): array |
| 91 | + { |
| 92 | + return [ |
| 93 | + ProductPropertyEnum::CLIENT_PRODUCT_ID => '18877', |
| 94 | + ProductPropertyEnum::PRODUCT_NAME => 'Some brand - product', |
| 95 | + ProductPropertyEnum::PRODUCT_NAME_LANG => 'hu', |
| 96 | + ProductPropertyEnum::PRODUCT_PAGE_URL => 'https://yoursite.dev/p/some-product-18877', |
| 97 | + ProductPropertyEnum::MAIN_IMAGE_URL => 'https://img.yoursite.dev/i/855x1290/XX-KUD03K_99X_F1.jpg?v=1552489483', |
| 98 | + ProductPropertyEnum::PRICE => 23990.0, |
| 99 | + ProductPropertyEnum::PRICE_CURRENCY => 'HUF', |
| 100 | + ProductPropertyEnum::SALE_PRICE => 15990.0, |
| 101 | + ]; |
| 102 | + } |
| 103 | + |
| 104 | + private function getComparisonArrayProduct(Product $product): array |
| 105 | + { |
| 106 | + return [ |
| 107 | + ProductPropertyEnum::CLIENT_PRODUCT_ID => $product->getClientProductId(), |
| 108 | + ProductPropertyEnum::PRODUCT_NAME => $product->getProductName(), |
| 109 | + ProductPropertyEnum::PRODUCT_NAME_LANG => $product->getProductNameLang(), |
| 110 | + ProductPropertyEnum::PRODUCT_PAGE_URL => $product->getProductPageUrl(), |
| 111 | + ProductPropertyEnum::MAIN_IMAGE_URL => $product->getMainImageUrl(), |
| 112 | + ProductPropertyEnum::PRICE => $product->getPrice(), |
| 113 | + ProductPropertyEnum::PRICE_CURRENCY => $product->getPriceCurrency(), |
| 114 | + ProductPropertyEnum::SALE_PRICE => $product->getSalePrice(), |
| 115 | + ]; |
| 116 | + } |
| 117 | +} |
0 commit comments