Completed
Push — master ( a9e1ce...416b3d )
by Sam
22s
created

ManyManyThroughListTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\ORM\ManyManyThroughList;
8
use InvalidArgumentException;
9
10
class ManyManyThroughListTest extends SapphireTest
11
{
12
    protected static $fixture_file = 'ManyManyThroughListTest.yml';
13
14
    protected $extraDataObjects = [
15
        ManyManyThroughListTest\Item::class,
16
        ManyManyThroughListTest\JoinObject::class,
17
        ManyManyThroughListTest\TestObject::class
18
    ];
19
20
    protected function setUp()
21
    {
22
        parent::setUp();
23
        DataObject::reset();
24
    }
25
26
    protected function tearDown()
27
    {
28
        DataObject::reset();
29
        parent::tearDown();
30
    }
31
32
    public function testSelectJoin()
33
    {
34
        /** @var ManyManyThroughListTest\TestObject $parent */
35
        $parent = $this->objFromFixture(ManyManyThroughListTest\TestObject::class, 'parent1');
36
        $this->assertDOSEquals(
37
            [
38
                ['Title' => 'item 1'],
39
                ['Title' => 'item 2']
40
            ],
41
            $parent->Items()
42
        );
43
        // Check filters on list work
44
        $item1 = $parent->Items()->filter('Title', 'item 1')->first();
45
        $this->assertNotNull($item1);
46
        $this->assertNotNull($item1->getJoin());
47
        $this->assertEquals('join 1', $item1->getJoin()->Title);
48
        $this->assertInstanceOf(
49
            ManyManyThroughListTest\JoinObject::class,
50
            $item1->ManyManyThroughListTest_JoinObject
51
        );
52
        $this->assertEquals('join 1', $item1->ManyManyThroughListTest_JoinObject->Title);
53
54
        // Check filters on list work
55
        $item2 = $parent->Items()->filter('Title', 'item 2')->first();
56
        $this->assertNotNull($item2);
57
        $this->assertNotNull($item2->getJoin());
58
        $this->assertEquals('join 2', $item2->getJoin()->Title);
59
        $this->assertEquals('join 2', $item2->ManyManyThroughListTest_JoinObject->Title);
60
61
        // To filter on join table need to use some raw sql
62
        $item2 = $parent->Items()->where(['"ManyManyThroughListTest_JoinObject"."Title"' => 'join 2'])->first();
63
        $this->assertNotNull($item2);
64
        $this->assertEquals('item 2', $item2->Title);
65
        $this->assertNotNull($item2->getJoin());
66
        $this->assertEquals('join 2', $item2->getJoin()->Title);
67
        $this->assertEquals('join 2', $item2->ManyManyThroughListTest_JoinObject->Title);
68
69
        // Test sorting on join table
70
        $items = $parent->Items()->sort('"ManyManyThroughListTest_JoinObject"."Sort"');
71
        $this->assertDOSEquals(
72
            [
73
                ['Title' => 'item 2'],
74
                ['Title' => 'item 1'],
75
            ],
76
            $items
77
        );
78
79
        $items = $parent->Items()->sort('"ManyManyThroughListTest_JoinObject"."Sort" ASC');
80
        $this->assertDOSEquals(
81
            [
82
                ['Title' => 'item 1'],
83
                ['Title' => 'item 2'],
84
            ],
85
            $items
86
        );
87
        $items = $parent->Items()->sort('"ManyManyThroughListTest_JoinObject"."Title" DESC');
88
        $this->assertDOSEquals(
89
            [
90
                ['Title' => 'item 2'],
91
                ['Title' => 'item 1'],
92
            ],
93
            $items
94
        );
95
    }
96
97
    public function testAdd()
98
    {
99
        /** @var ManyManyThroughListTest\TestObject $parent */
100
        $parent = $this->objFromFixture(ManyManyThroughListTest\TestObject::class, 'parent1');
101
        $newItem = new ManyManyThroughListTest\Item();
102
        $newItem->Title = 'my new item';
103
        $newItem->write();
104
        $parent->Items()->add($newItem, ['Title' => 'new join record']);
105
106
        // Check select
107
        $newItem = $parent->Items()->filter(['Title' => 'my new item'])->first();
108
        $this->assertNotNull($newItem);
109
        $this->assertEquals('my new item', $newItem->Title);
110
        $this->assertInstanceOf(
111
            ManyManyThroughListTest\JoinObject::class,
112
            $newItem->getJoin()
113
        );
114
        $this->assertInstanceOf(
115
            ManyManyThroughListTest\JoinObject::class,
116
            $newItem->ManyManyThroughListTest_JoinObject
117
        );
118
        $this->assertEquals('new join record', $newItem->ManyManyThroughListTest_JoinObject->Title);
119
    }
120
121
    public function testRemove()
122
    {
123
        /** @var ManyManyThroughListTest\TestObject $parent */
124
        $parent = $this->objFromFixture(ManyManyThroughListTest\TestObject::class, 'parent1');
125
        $this->assertDOSEquals(
126
            [
127
                ['Title' => 'item 1'],
128
                ['Title' => 'item 2']
129
            ],
130
            $parent->Items()
131
        );
132
        $item1 = $parent->Items()->filter(['Title' => 'item 1'])->first();
133
        $parent->Items()->remove($item1);
0 ignored issues
show
Bug introduced by
It seems like $item1 defined by $parent->Items()->filter... => 'item 1'))->first() on line 132 can be null; however, SilverStripe\ORM\ManyManyThroughList::remove() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
134
        $this->assertDOSEquals(
135
            [['Title' => 'item 2']],
136
            $parent->Items()
137
        );
138
    }
139
140
    /**
141
     * Test validation
142
     */
143
    public function testValidateModelValidatesJoinType()
144
    {
145
        DataObject::reset();
146
        ManyManyThroughListTest\Item::config()->update(
147
            'db',
148
            [
149
            ManyManyThroughListTest\JoinObject::class => 'Text'
150
            ]
151
        );
152
        $this->setExpectedException(InvalidArgumentException::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
153
        DataObject::getSchema()->manyManyComponent(ManyManyThroughListTest\TestObject::class, 'Items');
154
    }
155
156
    public function testRelationParsing()
157
    {
158
        $schema = DataObject::getSchema();
159
160
        // Parent components
161
        $this->assertEquals(
162
            [
163
                ManyManyThroughList::class,
164
                ManyManyThroughListTest\TestObject::class,
165
                ManyManyThroughListTest\Item::class,
166
                'ParentID',
167
                'ChildID',
168
                ManyManyThroughListTest\JoinObject::class
169
            ],
170
            $schema->manyManyComponent(ManyManyThroughListTest\TestObject::class, 'Items')
171
        );
172
173
        // Belongs_many_many is the same, but with parent/child substituted
174
        $this->assertEquals(
175
            [
176
                ManyManyThroughList::class,
177
                ManyManyThroughListTest\Item::class,
178
                ManyManyThroughListTest\TestObject::class,
179
                'ChildID',
180
                'ParentID',
181
                ManyManyThroughListTest\JoinObject::class
182
            ],
183
            $schema->manyManyComponent(ManyManyThroughListTest\Item::class, 'Objects')
184
        );
185
    }
186
}
187