Passed
Push — master ( 4dcbe5...178913 )
by Jason
02:31
created

PurchasableTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 90
c 5
b 0
f 0
dl 0
loc 217
rs 10
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanArchive() 0 14 1
A testCanUnpublish() 0 14 1
A testCanEdit() 0 14 1
A testProvidePermissions() 0 15 1
A testCanDelete() 0 14 1
A testCanCreate() 0 14 1
A testIsProduct() 0 4 1
A testUpdateCMSFields() 0 13 1
A setUp() 0 5 1
A testGetIsAvailable() 0 36 1
1
<?php
2
3
namespace Dynamic\Foxy\Test\Extension;
4
5
use Dynamic\Foxy\Extension\Purchasable;
6
use Dynamic\Foxy\Model\FoxyCategory;
7
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...
8
use Dynamic\Foxy\Model\ProductOption;
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Model\ProductOption 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...
9
use Dynamic\Foxy\Model\Setting;
10
use Dynamic\Foxy\Model\Variation;
11
use Dynamic\Foxy\Test\TestOnly\TestProduct;
12
use Dynamic\Foxy\Test\TestOnly\TestVariationDataExtension;
13
use SilverStripe\Core\Config\Config;
14
use SilverStripe\Core\Injector\Injector;
15
use SilverStripe\Dev\Debug;
16
use SilverStripe\Dev\SapphireTest;
17
use SilverStripe\Forms\FieldList;
18
use SilverStripe\i18n\i18n;
19
use SilverStripe\ORM\ValidationException;
20
use SilverStripe\Security\Member;
21
22
/**
23
 * Class PurchasableTest
24
 * @package Dynamic\Foxy\Test\Extension
25
 */
26
class PurchasableTest extends SapphireTest
27
{
28
    /**
29
     * @var string
30
     */
31
    protected static $fixture_file = '../fixtures.yml';
32
33
    /**
34
     * @var array
35
     */
36
    protected static $extra_dataobjects = [
37
        TestProduct::class,
38
    ];
39
40
    /**
41
     * @var \string[][]
42
     */
43
    protected static $required_extensions = [
44
        TestProduct::class => [
45
            Purchasable::class,
46
        ],
47
        Variation::class => [
48
            TestVariationDataExtension::class,
49
        ],
50
    ];
51
52
    /**
53
     *
54
     */
55
    public function setUp()
56
    {
57
        Config::modify()->set(Variation::class, 'has_one', ['Product' => TestProduct::class]);
58
59
        return parent::setUp();
60
    }
61
62
    /**
63
     *
64
     */
65
    public function testUpdateCMSFields()
66
    {
67
        $object = Injector::inst()->create(TestProduct::class);
68
        $fields = $object->getCMSFields();
69
        $this->assertInstanceOf(FieldList::class, $fields);
70
71
        $object->Price = 10.00;
72
        $object->Code = 000123;
73
        $object->FoxyCategoryID = $this->objFromFixture(FoxyCategory::class, 'one')->ID;
74
75
        $object->write();
76
        $fields = $object->getCMSFields();
77
        $this->assertInstanceOf(FieldList::class, $fields);
78
    }
79
80
    /**
81
     *
82
     */
83
    public function testGetIsAvailable()
84
    {
85
        /** @var TestProduct $object */
86
        $object = Injector::inst()->create(TestProduct::class);
87
        $object->Available = 1;
0 ignored issues
show
Bug Best Practice introduced by
The property Available does not exist on Dynamic\Foxy\Test\TestOnly\TestProduct. Since you implemented __set, consider adding a @property annotation.
Loading history...
88
        $this->assertTrue($object->getIsAvailable());
0 ignored issues
show
Bug introduced by
The method getIsAvailable() does not exist on Dynamic\Foxy\Test\TestOnly\TestProduct. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        $this->assertTrue($object->/** @scrutinizer ignore-call */ getIsAvailable());
Loading history...
89
90
        /** @var TestProduct $object2 */
91
        $object2 = Injector::inst()->create(TestProduct::class);
92
        $object2->Available = false;
93
        $this->assertFalse($object2->getIsAvailable());
94
95
        /** @var TestProduct $object3 */
96
        $object3 = Injector::inst()->create(TestProduct::class);
97
        $object3->Available = 1;
98
        /** @var Variation $variation1 */
99
        $variation1 = Injector::inst()->create(Variation::class);
100
        $variation1->Title = 'small';
101
        $variation1->Available = 1;
0 ignored issues
show
Documentation Bug introduced by
The property $Available was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
102
        /** @var Variation $variation2 */
103
        $variation2 = Injector::inst()->create(Variation::class);
104
        $variation2->Title = 'large';
105
        $variation2->Available = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $Available was declared of type boolean, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
106
        $object3->Variations()->add($variation1);
0 ignored issues
show
Bug introduced by
The method Variations() does not exist on Dynamic\Foxy\Test\TestOnly\TestProduct. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        $object3->/** @scrutinizer ignore-call */ 
107
                  Variations()->add($variation1);
Loading history...
107
        $object3->Variations()->add($variation2);
108
        $this->assertTrue($object3->getIsAvailable());
109
110
        /** @var TestProduct $object4 */
111
        $object4 = Injector::inst()->create(TestProduct::class);
112
        $object4->Available = 1;
113
        /** @var Variation $variation3 */
114
        $variation3 = Injector::inst()->create(Variation::class);
115
        $variation3->Title = 'red';
116
        $variation3->Available = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $Available was declared of type boolean, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
117
        $object4->Variations()->add($variation3);
118
        $this->assertFalse($object4->getIsAvailable());
119
    }
120
121
    /**
122
     *
123
     */
124
    public function testIsProduct()
125
    {
126
        $object = Injector::inst()->create(TestProduct::class);
127
        $this->assertTrue($object->isProduct());
128
    }
129
130
    /**
131
     *
132
     */
133
    public function testProvidePermissions()
134
    {
135
        /** @var Purchasable $object */
136
        $object = singleton(Purchasable::class);
137
138
        i18n::set_locale('en');
139
        $expected = [
140
            'MANAGE_FOXY_PRODUCTS' => [
141
                'name' => 'Manage products',
142
                'category' => 'Foxy',
143
                'help' => 'Manage products and related settings',
144
                'sort' => 400
145
            ]
146
        ];
147
        $this->assertEquals($expected, $object->providePermissions());
148
    }
149
150
    /**
151
     *
152
     */
153
    public function testCanCreate()
154
    {
155
        /** @var TestProduct $object */
156
        $object = singleton(TestProduct::class);
157
        /** @var \SilverStripe\Security\Member $admin */
158
        $admin = $this->objFromFixture(Member::class, 'admin');
159
        /** @var \SilverStripe\Security\Member $siteOwner */
160
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
161
        /** @var \SilverStripe\Security\Member $default */
162
        $default = $this->objFromFixture(Member::class, 'default');
163
164
        $this->assertFalse($object->canCreate($default));
165
        $this->assertTrue($object->canCreate($admin));
166
        $this->assertTrue($object->canCreate($siteOwner));
167
    }
168
169
    /**
170
     *
171
     */
172
    public function testCanEdit()
173
    {
174
        /** @var TestProduct $object */
175
        $object = singleton(TestProduct::class);
176
        /** @var \SilverStripe\Security\Member $admin */
177
        $admin = $this->objFromFixture(Member::class, 'admin');
178
        /** @var \SilverStripe\Security\Member $siteOwner */
179
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
180
        /** @var \SilverStripe\Security\Member $default */
181
        $default = $this->objFromFixture(Member::class, 'default');
182
183
        $this->assertFalse($object->canEdit($default));
184
        $this->assertTrue($object->canEdit($admin));
185
        $this->assertTrue($object->canEdit($siteOwner));
186
    }
187
188
    /**
189
     *
190
     */
191
    public function testCanDelete()
192
    {
193
        /** @var TestProduct $object */
194
        $object = singleton(TestProduct::class);
195
        /** @var \SilverStripe\Security\Member $admin */
196
        $admin = $this->objFromFixture(Member::class, 'admin');
197
        /** @var \SilverStripe\Security\Member $siteOwner */
198
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
199
        /** @var \SilverStripe\Security\Member $default */
200
        $default = $this->objFromFixture(Member::class, 'default');
201
202
        $this->assertFalse($object->canDelete($default));
203
        $this->assertTrue($object->canDelete($admin));
204
        $this->assertTrue($object->canDelete($siteOwner));
205
    }
206
207
    /**
208
     *
209
     */
210
    public function testCanUnpublish()
211
    {
212
        /** @var TestProduct $object */
213
        $object = singleton(TestProduct::class);
214
        /** @var \SilverStripe\Security\Member $admin */
215
        $admin = $this->objFromFixture(Member::class, 'admin');
216
        /** @var \SilverStripe\Security\Member $siteOwner */
217
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
218
        /** @var \SilverStripe\Security\Member $default */
219
        $default = $this->objFromFixture(Member::class, 'default');
220
221
        $this->assertFalse($object->canUnpublish($default));
0 ignored issues
show
Bug introduced by
The method canUnpublish() does not exist on Dynamic\Foxy\Test\TestOnly\TestProduct. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

221
        $this->assertFalse($object->/** @scrutinizer ignore-call */ canUnpublish($default));
Loading history...
222
        $this->assertTrue($object->canUnpublish($admin));
223
        $this->assertTrue($object->canUnpublish($siteOwner));
224
    }
225
226
    /**
227
     *
228
     */
229
    public function testCanArchive()
230
    {
231
        /** @var TestProduct $object */
232
        $object = singleton(TestProduct::class);
233
        /** @var \SilverStripe\Security\Member $admin */
234
        $admin = $this->objFromFixture(Member::class, 'admin');
235
        /** @var \SilverStripe\Security\Member $siteOwner */
236
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
237
        /** @var \SilverStripe\Security\Member $default */
238
        $default = $this->objFromFixture(Member::class, 'default');
239
240
        $this->assertFalse($object->canArchive($default));
0 ignored issues
show
Bug introduced by
The method canArchive() does not exist on Dynamic\Foxy\Test\TestOnly\TestProduct. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

240
        $this->assertFalse($object->/** @scrutinizer ignore-call */ canArchive($default));
Loading history...
241
        $this->assertTrue($object->canArchive($admin));
242
        $this->assertTrue($object->canArchive($siteOwner));
243
    }
244
}
245