Completed
Push — 3.7 ( b58809...42ab51 )
by
unknown
05:48
created

ManyManyListTest::testWriteManipulationWithNonScalarValuesAllowed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package framework
5
 * @subpackage tests
6
 */
7
class ManyManyListTest extends SapphireTest {
8
9
	protected static $fixture_file = 'DataObjectTest.yml';
10
11
	protected $extraDataObjects = array(
12
		'DataObjectTest_Team',
13
		'DataObjectTest_SubTeam',
14
		'DataObjectTest_Fan',
15
		'DataObjectTest_Sortable',
16
		'DataObjectTest_EquipmentCompany',
17
		'DataObjectTest_SubEquipmentCompany',
18
		'DataObjectTest_Player',
19
		'DataObjectTest_Company',
20
		'DataObjectTest_TeamComment',
21
		'ManyManyListTest_ExtraFields',
22
		'ManyManyListTest_Product',
23
		'ManyManyListTest_Category',
24
	);
25
26
27
	public function testAddCompositedExtraFields() {
28
		$obj = new ManyManyListTest_ExtraFields();
29
		$obj->write();
30
31
		$money = new Money();
32
		$money->setAmount(100);
33
		$money->setCurrency('USD');
34
35
		// the actual test is that this does not generate an error in the sql.
36
		$obj->Clients()->add($obj, array(
37
			'Worth' => $money,
38
			'Reference' => 'Foo'
39
		));
40
41
		$check = $obj->Clients()->First();
42
43
		$this->assertEquals('Foo', $check->Reference, 'Basic scalar fields should exist');
44
		$this->assertInstanceOf('Money', $check->Worth, 'Composite fields should exist on the record');
45
		$this->assertEquals(100, $check->Worth->getAmount());
46
	}
47
48
	/**
49
	 * This test targets a bug where appending many_many_extraFields to a query would
50
	 * result in erroneous queries for sort orders that rely on _SortColumn0
51
	 */
52
	public function testAddCompositedExtraFieldsWithSortColumn0() {
53
		$obj = new ManyManyListTest_ExtraFields();
54
		$obj->write();
55
56
		$product = new ManyManyListTest_Product();
57
		$product->Title = 'Test Product';
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<ManyManyListTest_Product>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
58
		$product->write();
59
60
		// the actual test is that this does not generate an error in the sql.
61
		$obj->Products()->add($product, array(
62
			'Reference' => 'Foo'
63
		));
64
65
		$result = $obj->Products()->First();
66
		$this->assertEquals('Foo', $result->Reference, 'Basic scalar fields should exist');
67
		$this->assertEquals('Test Product', $result->Title);
68
	}
69
70
	public function testCreateList() {
71
		$list = ManyManyList::create('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID',
72
			'DataObjectTest_PlayerID');
73
		$this->assertEquals(2, $list->count());
74
	}
75
76
77
	public function testRelationshipEmptyOnNewRecords() {
78
		// Relies on the fact that (unrelated) teams exist in the fixture file already
79
		$newPlayer = new DataObjectTest_Player(); // many_many Teams
80
		$this->assertEquals(array(), $newPlayer->Teams()->column('ID'));
81
	}
82
83
	public function testAddingSingleDataObjectByReference() {
84
		$player1 = $this->objFromFixture('DataObjectTest_Player', 'player1');
85
		$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
86
		$player1->Teams()->add($team1);
87
		$player1->flushCache();
88
89
		$compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID',
90
			'DataObjectTest_PlayerID');
91
		$compareTeams = $compareTeams->forForeignID($player1->ID);
92
		$this->assertEquals($player1->Teams()->column('ID'),$compareTeams->column('ID'),
93
			"Adding single record as DataObject to many_many");
94
	}
95
96
	public function testRemovingSingleDataObjectByReference() {
97
		$player1 = $this->objFromFixture('DataObjectTest_Player', 'player1');
98
		$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
99
		$player1->Teams()->remove($team1);
100
		$player1->flushCache();
101
		$compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID',
102
			'DataObjectTest_PlayerID');
103
		$compareTeams = $compareTeams->forForeignID($player1->ID);
104
		$this->assertEquals($player1->Teams()->column('ID'),$compareTeams->column('ID'),
105
			"Removing single record as DataObject from many_many");
106
	}
107
108
	public function testAddingSingleDataObjectByID() {
109
		$player1 = $this->objFromFixture('DataObjectTest_Player', 'player1');
110
		$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
111
		$player1->Teams()->add($team1->ID);
112
		$player1->flushCache();
113
		$compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID',
114
			'DataObjectTest_PlayerID');
115
		$compareTeams = $compareTeams->forForeignID($player1->ID);
116
		$this->assertEquals($player1->Teams()->column('ID'), $compareTeams->column('ID'),
117
			"Adding single record as ID to many_many");
118
	}
119
120
	public function testRemoveByID() {
121
		$player1 = $this->objFromFixture('DataObjectTest_Player', 'player1');
122
		$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
123
		$player1->Teams()->removeByID($team1->ID);
124
		$player1->flushCache();
125
		$compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID',
126
			'DataObjectTest_PlayerID');
127
		$compareTeams = $compareTeams->forForeignID($player1->ID);
128
		$this->assertEquals($player1->Teams()->column('ID'), $compareTeams->column('ID'),
129
			"Removing single record as ID from many_many");
130
	}
131
132
	public function testSetByIdList() {
133
		$player1 = $this->objFromFixture('DataObjectTest_Player', 'player1');
134
		$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
135
		$team2 = $this->objFromFixture('DataObjectTest_Team', 'team2');
136
		$player1->Teams()->setByIdList(array($team1->ID, $team2->ID));
137
		$this->assertEquals(array($team1->ID, $team2->ID), $player1->Teams()->sort('Title')->column());
138
		$player1->Teams()->setByIdList(array($team1->ID));
139
		$this->assertEquals(array($team1->ID), $player1->Teams()->sort('Title')->column());
140
		$player1->Teams()->setByIdList(array($team2->ID));
141
		$this->assertEquals(array($team2->ID), $player1->Teams()->sort('Title')->column());
142
	}
143
144
	public function testAddingWithMultipleForeignKeys() {
145
		$newPlayer = new DataObjectTest_Player();
146
		$newPlayer->write();
147
		$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
148
		$team2 = $this->objFromFixture('DataObjectTest_Team', 'team2');
149
150
		$playersTeam1Team2 = DataObjectTest_Team::get()->relation('Players')
151
			->forForeignID(array($team1->ID, $team2->ID));
152
		$playersTeam1Team2->add($newPlayer);
153
		$this->assertEquals(
154
			array($team1->ID, $team2->ID),
155
			$newPlayer->Teams()->sort('Title')->column('ID')
156
		);
157
	}
158
159
	public function testAddingExistingDoesntRemoveExtraFields() {
160
		$player = new DataObjectTest_Player();
161
		$player->write();
162
		$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
163
164
		$team1->Players()->add($player, array('Position' => 'Captain'));
165
		$this->assertEquals(
166
			array('Position' => 'Captain'),
167
			$team1->Players()->getExtraData('Teams', $player->ID),
168
			'Writes extrafields'
169
		);
170
171
		$team1->Players()->add($player);
172
		$this->assertEquals(
173
			array('Position' => 'Captain'),
174
			$team1->Players()->getExtraData('Teams', $player->ID),
175
			'Retains extrafields on subsequent adds with NULL fields'
176
		);
177
178
		$team1->Players()->add($player, array('Position' => 'Defense'));
179
		$this->assertEquals(
180
			array('Position' => 'Defense'),
181
			$team1->Players()->getExtraData('Teams', $player->ID),
182
			'Updates extrafields on subsequent adds with fields'
183
		);
184
185
		$team1->Players()->add($player, array('Position' => null));
186
		$this->assertEquals(
187
			array('Position' => null),
188
			$team1->Players()->getExtraData('Teams', $player->ID),
189
			'Allows clearing of extrafields on subsequent adds'
190
		);
191
	}
192
193
	public function testSubtractOnAManyManyList() {
194
		$allList = ManyManyList::create('DataObjectTest_Player', 'DataObjectTest_Team_Players',
195
			'DataObjectTest_PlayerID', 'DataObjectTest_TeamID');
196
		$this->assertEquals(3, $allList->count(),
197
			'Precondition; we have all 3 players connected to a team in the list');
198
199
		$teamOneID = $this->idFromFixture('DataObjectTest_Team', 'team1');
200
		$teamTwoID = $this->idFromFixture('DataObjectTest_Team', 'team2');
201
202
		// Captain 1 belongs to one team; team1
203
		$captain1 = $this->objFromFixture('DataObjectTest_Player', 'captain1');
204
		$this->assertEquals(array($teamOneID),$captain1->Teams()->column("ID"),
205
			'Precondition; player2 belongs to team1');
206
207
		// Player 2 belongs to both teams: team1, team2
208
		$player2 = $this->objFromFixture('DataObjectTest_Player', 'player2');
209
		$this->assertEquals(array($teamOneID,$teamTwoID), $player2->Teams()->sort('Title')->column('ID'),
210
			'Precondition; player2 belongs to team1 and team2');
211
212
		// We want to find the teams for player2 where the captain does not belong to
213
		$teamsWithoutTheCaptain = $player2->Teams()->subtract($captain1->Teams());
214
215
		// Assertions
216
		$this->assertEquals(1,$teamsWithoutTheCaptain->count(),
217
			'The ManyManyList should onlu contain one team');
218
		$this->assertEquals($teamTwoID, $teamsWithoutTheCaptain->first()->ID,
219
			'The ManyManyList contains the wrong team');
220
	}
221
222
	public function testRemoveAll() {
223
		$first = new DataObjectTest_Team();
224
		$first->write();
225
226
		$second = new DataObjectTest_Team();
227
		$second->write();
228
229
		$firstPlayers = $first->Players();
230
		$secondPlayers = $second->Players();
231
232
		$a = new DataObjectTest_Player();
233
		$a->ShirtNumber = 'a';
0 ignored issues
show
Documentation introduced by
The property ShirtNumber does not exist on object<DataObjectTest_Player>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
234
		$a->write();
235
236
		$b = new DataObjectTest_Player();
237
		$b->ShirtNumber = 'b';
0 ignored issues
show
Documentation introduced by
The property ShirtNumber does not exist on object<DataObjectTest_Player>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
238
		$b->write();
239
240
		$firstPlayers->add($a);
241
		$firstPlayers->add($b);
242
243
		$secondPlayers->add($a);
244
		$secondPlayers->add($b);
245
246
		$this->assertEquals(array('a', 'b'), $firstPlayers->sort('ShirtNumber')->column('ShirtNumber'));
247
		$this->assertEquals(array('a', 'b'), $secondPlayers->sort('ShirtNumber')->column('ShirtNumber'));
248
249
		$firstPlayers->removeAll();
250
251
		$this->assertEquals(0, count($firstPlayers));
252
		$this->assertEquals(2, count($secondPlayers));
253
254
		$firstPlayers->removeAll();
255
256
		$firstPlayers->add($a);
257
		$firstPlayers->add($b);
258
259
		$this->assertEquals(array('a', 'b'), $firstPlayers->sort('ShirtNumber')->column('ShirtNumber'));
260
261
		$firstPlayers->filter('ShirtNumber', 'b')->removeAll();
262
263
		$this->assertEquals(array('a'), $firstPlayers->column('ShirtNumber'));
264
		$this->assertEquals(array('a', 'b'), $secondPlayers->sort('ShirtNumber')->column('ShirtNumber'));
265
266
		$this->assertNotNull(DataObjectTest_Player::get()->byID($a->ID));
267
		$this->assertNotNull(DataObjectTest_Player::get()->byID($b->ID));
268
	}
269
270
	public function testAppendExtraFieldsToQuery() {
271
		$list = new ManyManyList(
272
			'ManyManyListTest_ExtraFields',
273
			'ManyManyListTest_ExtraFields_Clients',
274
			'ManyManyListTest_ExtraFieldsID',
275
			'ChildID', array(
276
				'Worth' => 'Money',
277
				'Reference' => 'Varchar'
278
			)
279
		);
280
281
		// ensure that ManyManyListTest_ExtraFields_Clients.ValueCurrency is
282
		// selected.
283
		$db = DB::get_conn();
284
		$expected = 'SELECT DISTINCT "ManyManyListTest_ExtraFields_Clients"."WorthCurrency",'
285
			.' "ManyManyListTest_ExtraFields_Clients"."WorthAmount", "ManyManyListTest_ExtraFields_Clients"."Reference",'
286
			.' "ManyManyListTest_ExtraFields"."ClassName", "ManyManyListTest_ExtraFields"."LastEdited",'
287
			.' "ManyManyListTest_ExtraFields"."Created", "ManyManyListTest_ExtraFields"."ID",'
288
			.' CASE WHEN "ManyManyListTest_ExtraFields"."ClassName" IS NOT NULL THEN'
289
			.' "ManyManyListTest_ExtraFields"."ClassName" ELSE '. Convert::raw2sql('ManyManyListTest_ExtraFields', true)
290
			.' END AS "RecordClassName" FROM "ManyManyListTest_ExtraFields" INNER JOIN'
291
			.' "ManyManyListTest_ExtraFields_Clients" ON'
292
			.' "ManyManyListTest_ExtraFields_Clients"."ManyManyListTest_ExtraFieldsID" ='
293
			.' "ManyManyListTest_ExtraFields"."ID"';
294
295
		$this->assertSQLEquals($expected, $list->sql($parameters));
296
	}
297
298
	public function testFilteringOnPreviouslyJoinedTable() {
299
300
		/** @var ManyManyListTest_Category $category */
301
		$category = $this->objFromFixture('ManyManyListTest_Category', 'categorya');
302
303
		/** @var ManyManyList $productsRelatedToProductB */
304
		$productsRelatedToProductB = $category->Products()->filter('RelatedProducts.Title', 'Product B');
305
306
		$this->assertEquals(1, $productsRelatedToProductB->count());
307
	}
308
309
310
}
311
312
/**
313
 * @package framework
314
 * @subpackage tests
315
 */
316
class ManyManyListTest_ExtraFields extends DataObject implements TestOnly {
317
318
	private static $many_many = array(
319
		'Clients' => 'ManyManyListTest_ExtraFields',
320
		'Products' => 'ManyManyListTest_Product'
321
	);
322
323
	private static $belongs_many_many = array(
324
		'WorksWith' => 'ManyManyListTest_ExtraFields'
325
	);
326
327
	private static $many_many_extraFields = array(
328
		'Clients' => array(
329
			'Reference' => 'Varchar',
330
			'Worth' => 'Money'
331
		),
332
		'Products' => array(
333
			'Reference' => 'Varchar'
334
		)
335
	);
336
}
337
338
class ManyManyListTest_Product extends DataObject implements TestOnly {
339
340
	private static $db = array(
341
		'Title' => 'Varchar'
342
	);
343
344
	private static $many_many = array(
345
		'RelatedProducts' => 'ManyManyListTest_Product'
346
	);
347
348
	private static $belongs_many_many = array(
349
		'RelatedTo' => 'ManyManyListTest_Product',
350
		'Categories' => 'ManyManyListTest_Category'
351
	);
352
353
	private static $default_sort = '"Title" IS NOT NULL ASC, "Title" ASC';
354
355
}
356
357
class ManyManyListTest_Category extends DataObject implements TestOnly {
358
359
	private static $db = array(
360
		'Title' => 'Varchar'
361
	);
362
363
	private static $many_many = array(
364
		'Products' => 'ManyManyListTest_Product'
365
	);
366
367
}
368
369