Passed
Push — zero-is-false ( 5bf3f2...d7e1e1 )
by Sam
08:16
created

DBEnumTest::testObsoleteValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 35
nc 1
nop 0
dl 0
loc 59
rs 9.36
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\ORM\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\ORM\FieldType\DBEnum;
7
use SilverStripe\ORM\FieldType\DBField;
8
use SilverStripe\ORM\DB;
9
10
class DBEnumTest extends SapphireTest
11
{
12
13
    protected $extraDataObjects = [
14
        FieldType\DBEnumTestObject::class,
15
    ];
16
17
    protected $usesDatabase = true;
18
19
    public function testDefault()
20
    {
21
        /** @var DBEnum $enum1 */
22
        $enum1 = DBField::create_field('Enum("A, B, C, D")', null);
23
        /** @var DBEnum $enum2 */
24
        $enum2 = DBField::create_field('Enum("A, B, C, D", "")', null);
25
        /** @var DBEnum $enum3 */
26
        $enum3 = DBField::create_field('Enum("A, B, C, D", null)', null);
27
        /** @var DBEnum $enum4 */
28
        $enum4 = DBField::create_field('Enum("A, B, C, D", 1)', null);
29
30
        $this->assertEquals('A', $enum1->getDefaultValue());
31
        $this->assertEquals('A', $enum1->getDefault());
32
        $this->assertEquals(null, $enum2->getDefaultValue());
33
        $this->assertEquals(null, $enum2->getDefault());
34
        $this->assertEquals(null, $enum3->getDefaultValue());
35
        $this->assertEquals(null, $enum3->getDefault());
36
        $this->assertEquals('B', $enum4->getDefaultValue());
37
        $this->assertEquals('B', $enum4->getDefault());
38
    }
39
40
    public function testObsoleteValues()
41
    {
42
        $obj = new FieldType\DBEnumTestObject();
43
        $colourField = $obj->obj('Colour');
44
        $colourField->setTable('FieldType_DBEnumTestObject');
45
46
        // Test values prior to any database content
47
        $this->assertEquals(
48
            ['Red', 'Blue', 'Green'],
49
            $colourField->getEnumObsolete()
0 ignored issues
show
Bug introduced by
The method getEnumObsolete() does not exist on SilverStripe\ORM\FieldType\DBField. 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

49
            $colourField->/** @scrutinizer ignore-call */ 
50
                          getEnumObsolete()
Loading history...
50
        );
51
52
        // Test values with a record
53
        $obj->Colour = 'Red';
0 ignored issues
show
Bug Best Practice introduced by
The property Colour does not exist on SilverStripe\ORM\Tests\FieldType\DBEnumTestObject. Since you implemented __set, consider adding a @property annotation.
Loading history...
54
        $obj->write();
55
        DBEnum::flushCache();
56
57
        $this->assertEquals(
58
            ['Red', 'Blue', 'Green'],
59
            $colourField->getEnumObsolete()
60
        );
61
62
        // If the value is removed from the enum, obsolete content is still retained
63
        $colourField->setEnum(['Blue', 'Green', 'Purple']);
0 ignored issues
show
Bug introduced by
The method setEnum() does not exist on SilverStripe\ORM\FieldType\DBField. 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

63
        $colourField->/** @scrutinizer ignore-call */ 
64
                      setEnum(['Blue', 'Green', 'Purple']);
Loading history...
64
        DBEnum::flushCache();
65
66
        $this->assertEquals(
67
            ['Blue', 'Green', 'Purple', 'Red'], // Red on the end now, because it's obsolete
68
            $colourField->getEnumObsolete()
69
        );
70
71
        // Check that old and new data is preserved after a schema update
72
        DB::get_schema()->schemaUpdate(function () use ($colourField) {
73
            $colourField->requireField();
74
        });
75
76
        $obj2 = new FieldType\DBEnumTestObject();
77
        $obj2->Colour = 'Purple';
78
        $obj2->write();
79
80
        $this->assertEquals(
81
            ['Purple', 'Red'],
82
            FieldType\DBEnumTestObject::get()->sort('Colour')->column('Colour')
83
        );
84
85
        // Ensure that enum columns are retained
86
        $colourField->setEnum(['Blue', 'Green']);
87
        $this->assertEquals(
88
            ['Blue', 'Green', 'Purple', 'Red'],
89
            $colourField->getEnumObsolete()
90
        );
91
92
        // If obsolete records are deleted, the extra values go away
93
        $obj->delete();
94
        $obj2->delete();
95
        DBEnum::flushCache();
96
        $this->assertEquals(
97
            ['Blue', 'Green'],
98
            $colourField->getEnumObsolete()
99
        );
100
    }
101
}
102