Passed
Pull Request — master (#111)
by Jason
02:35
created

VariationTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Foxy\Test\Model;
4
5
use Dynamic\Foxy\Extension\Purchasable;
6
use Dynamic\Foxy\Model\OptionType;
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Model\OptionType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Dynamic\Foxy\Model\Variation;
8
use Dynamic\Foxy\Test\TestOnly\TestProduct;
9
use Dynamic\Foxy\Test\TestOnly\TestVariation;
10
use Dynamic\Foxy\Test\TestOnly\TestVariationDataExtension;
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Test\TestOn...tVariationDataExtension was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SilverStripe\Core\Config\Config;
12
use SilverStripe\Core\Injector\Injector;
13
use SilverStripe\Dev\SapphireTest;
14
use SilverStripe\Forms\FieldList;
15
use SilverStripe\Security\Member;
16
use SilverStripe\Versioned\Versioned;
17
18
/**
19
 * Class ProductOptionTest
20
 * @package Dynamic\Foxy\Test\Model
21
 */
22
class VariationTest extends SapphireTest
23
{
24
    /**
25
     * @var string
26
     */
27
    protected static $fixture_file = '../fixtures.yml';
28
29
    /**
30
     * @var array
31
     */
32
    protected static $extra_dataobjects = [
33
        TestProduct::class,
34
        TestVariation::class,
35
    ];
36
37
    /**
38
     * @var \string[][]
39
     */
40
    protected static $required_extensions = [
41
        TestProduct::class => [
42
            Purchasable::class,
43
        ],
44
    ];
45
46
    /**
47
     *
48
     */
49
    public function setUp()
50
    {
51
        Config::modify()->set(TestVariation::class, 'has_one', ['Product' => TestProduct::class]);
52
53
        return parent::setUp();
54
    }
55
56
    /**
57
     *
58
     */
59
    public function testGetCMSFields()
60
    {
61
        $object = singleton(Variation::class);
62
        $fields = $object->getCMSFields();
63
        $this->assertInstanceOf(FieldList::class, $fields);
64
    }
65
66
    /**
67
     *
68
     */
69
    public function testCanCreate()
70
    {
71
        /** @var ProductOption $object */
72
        $object = singleton(Variation::class);
73
        /** @var \SilverStripe\Security\Member $admin */
74
        $admin = $this->objFromFixture(Member::class, 'admin');
75
        /** @var \SilverStripe\Security\Member $siteOwner */
76
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
77
        /** @var \SilverStripe\Security\Member $default */
78
        $default = $this->objFromFixture(Member::class, 'default');
79
80
        $this->assertFalse($object->canCreate($default));
81
        $this->assertTrue($object->canCreate($admin));
82
        $this->assertTrue($object->canCreate($siteOwner));
83
    }
84
85
    /**
86
     *
87
     */
88
    public function testCanEdit()
89
    {
90
        /** @var ProductOption $object */
91
        $object = singleton(Variation::class);
92
        /** @var \SilverStripe\Security\Member $admin */
93
        $admin = $this->objFromFixture(Member::class, 'admin');
94
        /** @var \SilverStripe\Security\Member $siteOwner */
95
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
96
        /** @var \SilverStripe\Security\Member $default */
97
        $default = $this->objFromFixture(Member::class, 'default');
98
99
        $this->assertFalse($object->canEdit($default));
100
        $this->assertTrue($object->canEdit($admin));
101
        $this->assertTrue($object->canEdit($siteOwner));
102
    }
103
104
    /**
105
     *
106
     */
107
    public function testCanDelete()
108
    {
109
        /** @var ProductOption $object */
110
        $object = singleton(Variation::class);
111
        /** @var \SilverStripe\Security\Member $admin */
112
        $admin = $this->objFromFixture(Member::class, 'admin');
113
        /** @var \SilverStripe\Security\Member $siteOwner */
114
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
115
        /** @var \SilverStripe\Security\Member $default */
116
        $default = $this->objFromFixture(Member::class, 'default');
117
118
        $this->assertFalse($object->canDelete($default));
119
        $this->assertTrue($object->canDelete($admin));
120
        $this->assertTrue($object->canDelete($siteOwner));
121
    }
122
123
    /**
124
     * @throws \SilverStripe\ORM\ValidationException
125
     */
126
    public function testGenerateKey()
127
    {
128
        $this->markTestSkipped();
129
        /*$product = $this->findOrMakeProduct();
130
        $option = ProductOption::create();
131
        //$option->write();
132
133
        $option->Title = $title = 'My Title';
134
        $price = 150;
135
        $action = 'Set';
136
137
        $product->Options()->add(
138
            $option,
139
            [
140
                'Available' => true,
141
                'PriceModifier' => $price,
142
                'PriceModifierAction' => $action,
143
            ]
144
        );
145
146
        $actionSymbol = ProductOption::getOptionModifierActionSymbol($action);
147
148
        $expected = "{$title}{p{$actionSymbol}{$price}|w+0|c+0}";
149
150
        $option = $product->Options()->filter('ProductOptionID', $option->ID)->first();
151
152
        $this->assertEquals(
153
            $expected,
154
            $option->OptionModifierKey
155
        );//*/
156
    }
157
158
    /**
159
     * @return TestProduct|\SilverStripe\ORM\DataObject
160
     */
161
    protected function findOrMakeProduct()
162
    {
163
        if (!$product = TestProduct::get()->first()) {
164
            $product = TestProduct::create();
165
            $product->Title = 'My Product';
166
            $product->writeToStage(Versioned::DRAFT);
167
            $product->publishSingle();
168
        }
169
170
        return $product;
171
    }
172
}
173