/
ProductCartTest.php
76 lines (60 loc) · 2.69 KB
/
ProductCartTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php declare(strict_types=1);
namespace Shopware\Core\Content\Test\Product\Cart;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
use Shopware\Core\Content\Test\Product\ProductBuilder;
use Shopware\Core\Framework\Test\IdsCollection;
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Test\Integration\Traits\TestShortHands;
/**
* @internal
* This test is used as "good" reference integration tests inside our guidelines.
*/
class ProductCartTest extends TestCase
{
use IntegrationTestBehaviour;
use TestShortHands;
/**
* @param array<string, mixed> $contextOptions
*/
#[DataProvider('priceInCartProvider')]
public function testPriceInCart(ProductBuilder $builder, float $expected, array $contextOptions = []): void
{
// the product builder has a helper function to write the product values to the database, including all dependencies (rules, currencies, properties, etc)
$builder->write($this->getContainer());
$context = $this->getContext(Uuid::randomHex(), $contextOptions);
// `addProductToCart` is a small generic helper method to create a product and add it into the cart within as one liner
$cart = $this->addProductToCart($builder->id, $context);
static::assertTrue($cart->has($builder->id));
$item = $cart->get($builder->id);
static::assertInstanceOf(LineItem::class, $item);
static::assertEquals($builder->id, $item->getId());
static::assertInstanceOf(CalculatedPrice::class, $item->getPrice());
static::assertEquals($expected, $item->getPrice()->getTotalPrice());
}
public static function priceInCartProvider(): \Generator
{
$ids = new IdsCollection();
// Important hint: You are not allowed to write values within this function, they are not detected by our database
// transaction behaviour. So they will not be deleted, and you remain artifacts inside the database
yield 'Test simple price' => [
(new ProductBuilder($ids, 'example-1'))->price(100)->visibility(),
100,
];
yield 'Test another price' => [
(new ProductBuilder($ids, 'example-1'))->price(200)->visibility(),
200,
];
yield 'Test different currency' => [
(new ProductBuilder($ids, 'example-1'))
->visibility()
->price(30)
->price(200, 100, 'dollar'),
200,
['currencyId' => $ids->get('dollar')],
];
}
}