Completed
Pull Request — master (#24)
by Jason
13:57
created

CareCleaningDocTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 78
loc 78
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetCMSFields() 12 12 1
A testCanView() 10 10 1
A testCanEdit() 10 10 1
A testCanDelete() 10 10 1
A testCanCreate() 10 10 1
A testProvidePermissions() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Dynamic\ProductCatalog\Test;
4
5
use Dynamic\ProductCatalog\Docs\CareCleaningDoc;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Security\Member;
9
10 View Code Duplication
class CareCleaningDocTest extends SapphireTest
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
{
12
    /**
13
     * @var string
14
     */
15
    protected static $fixture_file = 'fixtures.yml';
16
17
    /**
18
     *
19
     */
20
    public function testGetCMSFields()
21
    {
22
        $object = new CareCleaningDoc();
23
        $fields = $object->getCMSFields();
24
        $this->assertInstanceOf(FieldList::class, $fields);
25
        $this->assertNull($fields->dataFieldByName('Products'));
26
27
        $object = $this->objFromFixture(CareCleaningDoc::class, 'one');
28
        $fields = $object->getCMSFields();
29
        $this->assertInstanceOf(FieldList::class, $fields);
30
        $this->assertNotNull($fields->dataFieldByName('Products'));
31
    }
32
33
    public function testCanView()
34
    {
35
        $object = $this->objFromFixture(CareCleaningDoc::class, 'one');
36
37
        $admin = $this->objFromFixture(Member::class, 'admin');
38
        $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 37 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...
39
40
        $member = $this->objFromFixture(Member::class, 'default');
41
        $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 40 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...
42
    }
43
44
    public function testCanEdit()
45
    {
46
        $object = $this->objFromFixture(CareCleaningDoc::class, 'one');
47
48
        $admin = $this->objFromFixture(Member::class, 'admin');
49
        $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 48 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...
50
51
        $member = $this->objFromFixture(Member::class, 'default');
52
        $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 51 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...
53
    }
54
55
    public function testCanDelete()
56
    {
57
        $object = $this->objFromFixture(CareCleaningDoc::class, 'one');
58
59
        $admin = $this->objFromFixture(Member::class, 'admin');
60
        $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 59 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...
61
62
        $member = $this->objFromFixture(Member::class, 'default');
63
        $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 62 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...
64
    }
65
66
    public function testCanCreate()
67
    {
68
        $object = $this->objFromFixture(CareCleaningDoc::class, 'one');
69
70
        $admin = $this->objFromFixture(Member::class, 'admin');
71
        $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 70 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...
72
73
        $member = $this->objFromFixture(Member::class, 'default');
74
        $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 73 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...
75
    }
76
77
    public function testProvidePermissions()
78
    {
79
        $object = $this->objFromFixture(CareCleaningDoc::class, 'one');
80
        $expected = array(
81
            'Care_EDIT' => 'Edit Care and Cleaning Docs',
82
            'Care_DELETE' => 'Delete Care and Cleaning Docs',
83
            'Care_CREATE' => 'Create Care and Cleaning Docs',
84
        );
85
        $this->assertEquals($expected, $object->providePermissions());
86
    }
87
}
88