VariationTest::testVaraitionsBulkLoader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SilverShop\Tests\Model\Variation;
4
5
use SilverShop\Cart\ShoppingCart;
6
use SilverShop\Model\Variation\AttributeType;
7
use SilverShop\Model\Variation\AttributeValue;
8
use SilverShop\Model\Variation\Variation;
9
use SilverShop\Page\Product;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Dev\SapphireTest;
12
13
/**
14
 * Test product variation capabilities.
15
 *
16
 * @link       ProductVariation
17
 * @link       ProductVariationDecorator
18
 * @package    shop
19
 * @subpackage tests
20
 */
21
class VariationTest extends SapphireTest
22
{
23
    public static $fixture_file   = __DIR__ . '/../../Fixtures/variations.yml';
24
    public static $disable_theme  = true;
25
    protected static $use_draft_site = true;
26
27
    /**
28
     * @var Product
29
     */
30
    protected $mp3player;
31
32
    /**
33
     * @var Product
34
     */
35
    protected $socks;
36
37
    /**
38
     * @var Variation
39
     */
40
    protected $redlarge;
41
42
    public function setUp(): void
43
    {
44
        parent::setUp();
45
        ShoppingCart::singleton()->clear();
46
        $this->ball = $this->objFromFixture(Product::class, "ball");
0 ignored issues
show
Bug Best Practice introduced by
The property ball does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
        $this->mp3player = $this->objFromFixture(Product::class, "mp3player");
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->objFromFixture(Si...ct::class, 'mp3player') of type SilverStripe\ORM\DataObject is incompatible with the declared type SilverShop\Page\Product of property $mp3player.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
        $this->redlarge = $this->objFromFixture(Variation::class, "redlarge");
49
    }
50
51
    public function testVariationOrderItem()
52
    {
53
        $cart = ShoppingCart::singleton();
54
55
        //config
56
        Config::modify()
57
            ->set(Variation::class, 'title_has_label', true)
58
            ->set(Variation::class, 'title_separator', ':')
59
            ->set(Variation::class, 'title_glue', ', ');
60
61
        $emptyitem = $this->redlarge->Item();
62
        $this->assertEquals(1, $emptyitem->Quantity, "Items always have a quantity of at least 1.");
63
64
        $cart->add($this->redlarge);
65
        $item = $cart->get($this->redlarge);
66
        $this->assertTrue((bool)$item, "item exists");
67
        $this->assertEquals(1, $item->Quantity);
68
        $this->assertEquals(22, $item->UnitPrice());
69
        $this->assertEquals("Size:Large, Color:Red", $item->SubTitle());
70
    }
71
72
    public function testGetVariation()
73
    {
74
        $colorred = $this->objFromFixture(AttributeValue::class, "color_red");
75
        $sizelarge = $this->objFromFixture(AttributeValue::class, "size_large");
76
        $attributes = [$colorred->ID, $sizelarge->ID];
77
        $variation = $this->ball->getVariationByAttributes($attributes);
78
        $this->assertInstanceOf(Variation::class, $variation, "Variation exists");
79
        $this->assertEquals(22, $variation->sellingPrice(), "Variation price is $22 (price of ball");
80
81
        $attributes = [$colorred->ID, 999];
82
        $variation = $this->ball->getVariationByAttributes($attributes);
83
        $this->assertNull($variation, "Variation does not exist");
84
    }
85
86
    public function testGenerateVariations()
87
    {
88
        $color = $this->objFromFixture(AttributeType::class, "color");
89
        $values = ['Black', 'Blue']; //Note: black doesn't exist in the yaml
90
        $this->mp3player->generateVariationsFromAttributes($color, $values);
91
92
        $capacity = $this->objFromFixture(AttributeType::class, "capacity");
93
        $values = ["120GB", "300GB"]; //Note: 300GB doesn't exist in the yaml
94
        $this->mp3player->generateVariationsFromAttributes($capacity, $values);
95
96
        $variations = $this->mp3player->Variations();
97
        $this->assertEquals($variations->Count(), 4, "four variations created");
98
99
        $titles = $variations->map('ID', 'Title')->toArray();
100
        $this->assertStringContainsString('Color:Black, Capacity:120GB', $titles[5]);
101
        $this->assertStringContainsString('Color:Black, Capacity:300GB', $titles[6]);
102
        $this->assertStringContainsString('Color:Blue, Capacity:120GB', $titles[7]);
103
        $this->assertStringContainsString('Color:Blue, Capacity:300GB', $titles[8]);
104
    }
105
106
    public function testPriceRange()
107
    {
108
        $range = $this->ball->PriceRange();
109
        $this->assertTrue($range->HasRange);
110
        $this->assertEquals(20, $range->Min->getValue());
111
        $this->assertEquals(22, $range->Max->getValue());
112
        $this->assertEquals(21, $range->Average->getValue());
113
    }
114
115
    public function testVaraitionsBulkLoader()
116
    {
117
        $this->markTestIncomplete('try bulk loading some variations ... generate, and exact entries');
118
    }
119
}
120