Completed
Push — master ( b7abe3...4d4cc4 )
by Jason
12s
created

CatalogProductTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 105
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testCategoryList() 0 6 1
A testGetImage() 0 5 1
A testGetCMSFields() 0 11 1
A testGetPrimaryCategory() 0 5 1
A testGetDefaultSearchContext() 0 5 1
A testCanView() 0 10 1
A testCanEdit() 0 10 1
A testCanDelete() 0 10 1
A testCanCreate() 0 10 1
A testProvidePermissions() 0 10 1
1
<?php
2
3
namespace Dynamic\ProductCatalog\Test;
4
5
use Dynamic\ProductCatalog\ORM\CatalogProduct;
6
use Dynamic\ProductCatalog\Page\CatalogCategory;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\ORM\Search\SearchContext;
10
use SilverStripe\Security\Member;
11
12
class CatalogProductTest extends SapphireTest
13
{
14
    /**
15
     * @var string
16
     */
17
    protected static $fixture_file = 'product-catalog/tests/fixtures.yml';
18
19
    /**
20
     *
21
     */
22
    public function testCategoryList()
23
    {
24
        $object = $this->objFromFixture(CatalogProduct::class, 'one');
25
        $expected = $this->objFromFixture(CatalogCategory::class, 'default')->Title;
26
        $this->assertEquals($expected, $object->CategoryList());
27
    }
28
29
    /**
30
     *
31
     */
32
    public function testGetImage()
33
    {
34
        $object = $this->objFromFixture(CatalogProduct::class, 'one');
35
        $this->assertEquals($object->getImage(), $object->Slides()->first());
36
    }
37
38
    public function testGetCMSFields()
39
    {
40
        $object = new CatalogProduct();
41
        $fieldset = $object->getCMSFields();
42
        $this->assertTrue(is_a($fieldset, FieldList::class));
43
        //$this->assertNull($fieldset->dataFieldByName('TechDocs'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
45
        $object = $this->objFromFixture(CatalogProduct::class, 'one');
46
        $fieldset = $object->getCMSFields();
47
        $this->assertTrue(is_a($fieldset, FieldList::class));
48
    }
49
50
    public function testGetPrimaryCategory()
51
    {
52
        $object = $this->objFromFixture(CatalogProduct::class, 'one');
53
        $this->assertEquals($object->getPrimaryCategory(), $object->Categories()->sort('SortOrder')->first());
54
    }
55
56
    public function testGetDefaultSearchContext()
57
    {
58
        $object = $this->objFromFixture(CatalogProduct::class, 'one');
59
        $this->assertInstanceOf(SearchContext::class, $object->getDefaultSearchContext());
60
    }
61
62
    public function testCanView()
63
    {
64
        $object = $this->objFromFixture(CatalogProduct::class, 'one');
65
66
        $admin = $this->objFromFixture(Member::class, 'admin');
67
        $this->assertTrue($object->canView($admin));
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'admin') on line 66 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canView() does only seem to accept object<SilverStripe\Security\Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
68
69
        $member = $this->objFromFixture(Member::class, 'default');
70
        $this->assertTrue($object->canView($member));
0 ignored issues
show
Bug introduced by
It seems like $member defined by $this->objFromFixture(\S...mber::class, 'default') on line 69 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canView() does only seem to accept object<SilverStripe\Security\Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
71
    }
72
73
    public function testCanEdit()
74
    {
75
        $object = $this->objFromFixture(CatalogProduct::class, 'one');
76
77
        $admin = $this->objFromFixture(Member::class, 'admin');
78
        $this->assertTrue($object->canEdit($admin));
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'admin') on line 77 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canEdit() does only seem to accept object<SilverStripe\Security\Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
79
80
        $member = $this->objFromFixture(Member::class, 'default');
81
        $this->assertFalse($object->canEdit($member));
0 ignored issues
show
Bug introduced by
It seems like $member defined by $this->objFromFixture(\S...mber::class, 'default') on line 80 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canEdit() does only seem to accept object<SilverStripe\Security\Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
82
    }
83
84
    public function testCanDelete()
85
    {
86
        $object = $this->objFromFixture(CatalogProduct::class, 'one');
87
88
        $admin = $this->objFromFixture(Member::class, 'admin');
89
        $this->assertTrue($object->canDelete($admin));
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'admin') on line 88 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canDelete() does only seem to accept object<SilverStripe\Security\Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
90
91
        $member = $this->objFromFixture(Member::class, 'default');
92
        $this->assertFalse($object->canDelete($member));
0 ignored issues
show
Bug introduced by
It seems like $member defined by $this->objFromFixture(\S...mber::class, 'default') on line 91 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canDelete() does only seem to accept object<SilverStripe\Security\Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
93
    }
94
95
    public function testCanCreate()
96
    {
97
        $object = $this->objFromFixture(CatalogProduct::class, 'one');
98
99
        $admin = $this->objFromFixture(Member::class, 'admin');
100
        $this->assertTrue($object->canCreate($admin));
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->objFromFixture(\S...Member::class, 'admin') on line 99 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canCreate() does only seem to accept object<SilverStripe\Security\Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
101
102
        $member = $this->objFromFixture(Member::class, 'default');
103
        $this->assertFalse($object->canCreate($member));
0 ignored issues
show
Bug introduced by
It seems like $member defined by $this->objFromFixture(\S...mber::class, 'default') on line 102 can also be of type object<SilverStripe\ORM\DataObject>; however, SilverStripe\ORM\DataObject::canCreate() does only seem to accept object<SilverStripe\Security\Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
104
    }
105
106
    public function testProvidePermissions()
107
    {
108
        $object = $this->objFromFixture(CatalogProduct::class, 'one');
109
        $expected = array(
110
            'Product_EDIT' => 'Edit a Product',
111
            'Product_DELETE' => 'Delete a Product',
112
            'Product_CREATE' => 'Create a Product',
113
        );
114
        $this->assertEquals($expected, $object->providePermissions());
115
    }
116
}