Passed
Push — fix-1489 ( 4d9384...c32af1 )
by Sam
07:17
created

getExtraDataObjects()   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
        }
34
        parent::setUp();
35
    }
36
37
    public function testLoadDataFromMultiEnum()
38
    {
39
        $article = new MultiEnumArticle();
40
        $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...
41
42
        $field = new CheckboxSetField(
43
            'Colours',
44
            'Colours',
45
            [
46
                'Red' => 'Red',
47
                'Blue' => 'Blue',
48
                'Green' => 'Green',
49
            ]
50
        );
51
52
        $form = new Form(
53
            Controller::curr(),
54
            'Form',
55
            new FieldList($field),
56
            new FieldList()
57
        );
58
        $form->loadDataFrom($article);
59
        $value = $field->Value();
60
        $this->assertEquals(['Red', 'Green'], $value);
61
    }
62
63
    public function testSavingIntoMultiEnum()
64
    {
65
        $field = new CheckboxSetField(
66
            'Colours',
67
            'Colours',
68
            [
69
                'Red' => 'Red',
70
                'Blue' => 'Blue',
71
                'Green' => 'Green',
72
            ]
73
        );
74
        $article = new MultiEnumArticle();
75
        $field->setValue(array('Red' => 'Red', 'Blue' => 'Blue'));
76
        $field->saveInto($article);
77
        $article->write();
78
79
        $dbValue = DB::query(
80
            sprintf(
81
                'SELECT "Colours" FROM "CheckboxSetFieldTest_MultiEnumArticle" WHERE "ID" = %s',
82
                $article->ID
83
            )
84
        )->value();
85
86
        // JSON encoded values
87
        $this->assertEquals('Red,Blue', $dbValue);
88
    }
89
}
90