Passed
Push — fix-1489 ( c32af1...76255c )
by Sam
08:47
created

CheckboxSetFieldMulitEnumTest::tearDown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms\Tests;
4
5
use SilverStripe\Forms\Tests\CheckboxSetFieldTest\MultiEnumArticle;
6
use SilverStripe\ORM\DB;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Forms\CheckboxSetField;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\Form;
12
use SilverStripe\ORM\Connect\MySQLDatabase;
13
14
class CheckboxSetFieldMulitEnumTest extends SapphireTest
15
{
16
17
    protected $usesDatabase = true;
18
19
    public static function getExtraDataObjects()
20
    {
21
        // Don't add this for other database
22
        if (DB::get_conn() instanceof MySQLDatabase) {
23
            return [
24
                MultiEnumArticle::class,
25
            ];
26
        }
27
    }
28
29
    public function setUp()
30
    {
31
        if (!(DB::get_conn() instanceof MySQLDatabase)) {
32
            $this->markTestSkipped('DBMultiEnum only supported by MySQL');
33
            return;
34
        }
35
        parent::setUp();
36
    }
37
38
    public function tearDown()
39
    {
40
        if (!(DB::get_conn() instanceof MySQLDatabase)) {
41
            return;
42
        }
43
        parent::tearDown();
44
    }
45
46
    public function testLoadDataFromMultiEnum()
47
    {
48
        $article = new MultiEnumArticle();
49
        $article->Colours = 'Red,Green';
0 ignored issues
show
Bug Best Practice introduced by
The property Colours does not exist on SilverStripe\Forms\Tests...ldTest\MultiEnumArticle. Since you implemented __set, consider adding a @property annotation.
Loading history...
50
51
        $field = new CheckboxSetField(
52
            'Colours',
53
            'Colours',
54
            [
55
                'Red' => 'Red',
56
                'Blue' => 'Blue',
57
                'Green' => 'Green',
58
            ]
59
        );
60
61
        $form = new Form(
62
            Controller::curr(),
63
            'Form',
64
            new FieldList($field),
65
            new FieldList()
66
        );
67
        $form->loadDataFrom($article);
68
        $value = $field->Value();
69
        $this->assertEquals(['Red', 'Green'], $value);
70
    }
71
72
    public function testSavingIntoMultiEnum()
73
    {
74
        $field = new CheckboxSetField(
75
            'Colours',
76
            'Colours',
77
            [
78
                'Red' => 'Red',
79
                'Blue' => 'Blue',
80
                'Green' => 'Green',
81
            ]
82
        );
83
        $article = new MultiEnumArticle();
84
        $field->setValue(array('Red' => 'Red', 'Blue' => 'Blue'));
85
        $field->saveInto($article);
86
        $article->write();
87
88
        $dbValue = DB::query(
89
            sprintf(
90
                'SELECT "Colours" FROM "CheckboxSetFieldTest_MultiEnumArticle" WHERE "ID" = %s',
91
                $article->ID
92
            )
93
        )->value();
94
95
        // JSON encoded values
96
        $this->assertEquals('Red,Blue', $dbValue);
97
    }
98
}
99