QueryBuilderTest.php 19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

use Illuminate\Support\Facades\DB;

class QueryBuilderTest extends PHPUnit_Framework_TestCase {

	public function setUp() {}

	public function tearDown()
	{
		DB::collection('users')->truncate();
		DB::collection('items')->truncate();
	}

	public function testCollection()
	{
Jens Segers's avatar
Jens Segers committed
17
		$this->assertInstanceOf('Jenssegers\Mongodb\Query\Builder', DB::collection('users'));
18 19 20 21 22 23 24 25 26 27 28 29 30
	}

	public function testGet()
	{
		$users = DB::collection('users')->get();
		$this->assertEquals(0, count($users));

		DB::collection('users')->insert(array('name' => 'John Doe'));

		$users = DB::collection('users')->get();
		$this->assertEquals(1, count($users));
	}

Jens Segers's avatar
Jens Segers committed
31 32 33 34 35 36 37 38 39 40 41 42
	public function testNoDocument()
	{
		$items = DB::collection('items')->where('name', 'nothing')->get();
		$this->assertEquals(array(), $items);

		$item = DB::collection('items')->where('name', 'nothing')->first();
		$this->assertEquals(null, $item);

		$item = DB::collection('items')->where('_id', '51c33d8981fec6813e00000a')->first();
		$this->assertEquals(null, $item);
	}

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	public function testInsert()
	{
		DB::collection('users')->insert(array(
			'tags' => array('tag1', 'tag2'),
			'name' => 'John Doe',
		));

		$users = DB::collection('users')->get();
		$this->assertEquals(1, count($users));

		$user = $users[0];
		$this->assertEquals('John Doe', $user['name']);
		$this->assertTrue(is_array($user['tags']));
	}

58
	public function testInsertGetId()
59 60 61 62 63 64 65 66 67 68 69 70
	{
		$id = DB::collection('users')->insertGetId(array('name' => 'John Doe'));

		$this->assertTrue(is_string($id));
		$this->assertEquals(24, strlen($id));
	}

	public function testBatchInsert()
	{
		DB::collection('users')->insert(array(
			array(
				'tags' => array('tag1', 'tag2'),
71
				'name' => 'Jane Doe',
72 73 74 75 76 77 78 79 80
			),
			array(
				'tags' => array('tag3'),
				'name' => 'John Doe',
			),
		));

		$users = DB::collection('users')->get();
		$this->assertEquals(2, count($users));
81
		$this->assertTrue(is_array($users[0]['tags']));
82 83 84 85 86 87 88 89 90 91
	}

	public function testFind()
	{
		$id = DB::collection('users')->insertGetId(array('name' => 'John Doe'));

		$user = DB::collection('users')->find($id);
		$this->assertEquals('John Doe', $user['name']);
	}

92 93 94 95 96 97
	public function testFindNull()
	{
		$user = DB::collection('users')->find(null);
		$this->assertEquals(null, $user);
	}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	public function testCount()
	{
		DB::collection('users')->insert(array(
			array('name' => 'Jane Doe'),
			array('name' => 'John Doe')
		));

		$this->assertEquals(2, DB::collection('users')->count());
	}

	public function testUpdate()
	{
		DB::collection('users')->insert(array(
			array('name' => 'Jane Doe', 'age' => 20),
			array('name' => 'John Doe', 'age' => 21)
		));

		DB::collection('users')->where('name', 'John Doe')->update(array('age' => 100));
		$users = DB::collection('users')->get();

118 119 120 121
		$john = DB::collection('users')->where('name', 'John Doe')->first();
		$jane = DB::collection('users')->where('name', 'Jane Doe')->first();
		$this->assertEquals(100, $john['age']);
		$this->assertEquals(20, $jane['age']);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	}

	public function testDelete()
	{
		DB::collection('users')->insert(array(
			array('name' => 'Jane Doe', 'age' => 20),
			array('name' => 'John Doe', 'age' => 25)
		));

		DB::collection('users')->where('age', '<', 10)->delete();
		$this->assertEquals(2, DB::collection('users')->count());

		DB::collection('users')->where('age', '<', 25)->delete();
		$this->assertEquals(1, DB::collection('users')->count());
	}

	public function testTruncate()
	{
		DB::collection('users')->insert(array('name' => 'John Doe'));
		DB::collection('users')->truncate();
		$this->assertEquals(0, DB::collection('users')->count());
	}

	public function testSubKey()
	{
		DB::collection('users')->insert(array(
			array(
				'name' => 'John Doe',
				'address' => array('country' => 'Belgium', 'city' => 'Ghent')
			),
			array(
				'name' => 'Jane Doe',
				'address' => array('country' => 'France', 'city' => 'Paris')
			)
		));

		$users = DB::collection('users')->where('address.country', 'Belgium')->get();
		$this->assertEquals(1, count($users));
		$this->assertEquals('John Doe', $users[0]['name']);
	}

	public function testInArray()
	{
		DB::collection('items')->insert(array(
			array(
				'tags' => array('tag1', 'tag2', 'tag3', 'tag4')
			),
			array(
				'tags' => array('tag2')
			)
		));

		$items = DB::collection('items')->where('tags', 'tag2')->get();
		$this->assertEquals(2, count($items));

		$items = DB::collection('items')->where('tags', 'tag1')->get();
		$this->assertEquals(1, count($items));
	}

	public function testRaw()
	{
		DB::collection('users')->insert(array(
			array('name' => 'Jane Doe', 'age' => 20),
			array('name' => 'John Doe', 'age' => 25)
		));

		$cursor = DB::collection('users')->raw(function($collection) {
			return $collection->find(array('age' => 20));
		});

		$this->assertInstanceOf('MongoCursor', $cursor);
		$this->assertEquals(1, $cursor->count());

		$collection = DB::collection('users')->raw();
		$this->assertInstanceOf('MongoCollection', $collection);
Jens Segers's avatar
Jens Segers committed
197

198 199 200
		$collection = User::raw();
		$this->assertInstanceOf('MongoCollection', $collection);

Jens Segers's avatar
Jens Segers committed
201 202 203
		$results = DB::collection('users')->whereRaw(array('age' => 20))->get();
		$this->assertEquals(1, count($results));
		$this->assertEquals('Jane Doe', $results[0]['name']);
204 205 206 207 208 209
	}

	public function testPush()
	{
		$id = DB::collection('users')->insertGetId(array(
			'name' => 'John Doe',
210 211
			'tags' => array(),
			'messages' => array(),
212 213 214
		));

		DB::collection('users')->where('_id', $id)->push('tags', 'tag1');
215

216 217 218 219 220 221 222 223 224 225
		$user = DB::collection('users')->find($id);
		$this->assertTrue(is_array($user['tags']));
		$this->assertEquals(1, count($user['tags']));
		$this->assertEquals('tag1', $user['tags'][0]);

		DB::collection('users')->where('_id', $id)->push('tags', 'tag2');
		$user = DB::collection('users')->find($id);
		$this->assertEquals(2, count($user['tags']));
		$this->assertEquals('tag2', $user['tags'][1]);

226 227 228 229 230 231 232 233 234 235
		// Add duplicate
		DB::collection('users')->where('_id', $id)->push('tags', 'tag2');
		$user = DB::collection('users')->find($id);
		$this->assertEquals(3, count($user['tags']));

		// Add unique
		DB::collection('users')->where('_id', $id)->push('tags', 'tag1', true);
		$user = DB::collection('users')->find($id);
		$this->assertEquals(3, count($user['tags']));

236 237
		$message = array('from' => 'Jane', 'body' => 'Hi John');
		DB::collection('users')->where('_id', $id)->push('messages', $message);
238
		$user = DB::collection('users')->find($id);
239 240
		$this->assertTrue(is_array($user['messages']));
		$this->assertEquals($message, $user['messages'][0]);
241 242 243 244
	}

	public function testPull()
	{
245 246
		$message = array('from' => 'Jane', 'body' => 'Hi John');

247 248
		$id = DB::collection('users')->insertGetId(array(
			'name' => 'John Doe',
249 250
			'tags' => array('tag1', 'tag2', 'tag3', 'tag4'),
			'messages' => array($message)
251 252 253
		));

		DB::collection('users')->where('_id', $id)->pull('tags', 'tag3');
254

255 256 257 258 259
		$user = DB::collection('users')->find($id);
		$this->assertTrue(is_array($user['tags']));
		$this->assertEquals(3, count($user['tags']));
		$this->assertEquals('tag4', $user['tags'][2]);

260 261
		DB::collection('users')->where('_id', $id)->pull('messages', $message);

262
		$user = DB::collection('users')->find($id);
263 264
		$this->assertTrue(is_array($user['messages']));
		$this->assertEquals(0, count($user['messages']));
265 266 267 268 269
	}

	public function testDistinct()
	{
		DB::collection('items')->insert(array(
Jens Segers's avatar
Jens Segers committed
270 271 272 273
			array('name' => 'knife', 'type' => 'sharp',),
			array('name' => 'fork',  'type' => 'sharp'),
			array('name' => 'spoon', 'type' => 'round'),
			array('name' => 'spoon', 'type' => 'round')
274 275
		));

276 277 278
		$items = DB::collection('items')->distinct('name')->get(); sort($items);
		$this->assertEquals(3, count($items));
		$this->assertEquals(array('fork', 'knife', 'spoon'), $items);
279

280 281 282
		$types = DB::collection('items')->distinct('type')->get(); sort($types);
		$this->assertEquals(2, count($types));
		$this->assertEquals(array('round', 'sharp'), $types);
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	}

	public function testCustomId()
	{
		DB::collection('items')->insert(array(
			array('_id' => 'knife', 'type' => 'sharp', 'amount' => 34),
			array('_id' => 'fork',  'type' => 'sharp', 'amount' => 20),
			array('_id' => 'spoon', 'type' => 'round', 'amount' => 3)
		));

		$item = DB::collection('items')->find('knife');
		$this->assertEquals('knife', $item['_id']);

		$item = DB::collection('items')->where('_id', 'fork')->first();
		$this->assertEquals('fork', $item['_id']);
Jens Segers's avatar
Jens Segers committed
298 299 300 301 302 303 304 305

		DB::collection('users')->insert(array(
			array('_id' => 1, 'name' => 'Jane Doe'),
			array('_id' => 2, 'name' => 'John Doe')
		));

		$item = DB::collection('users')->find(1);
		$this->assertEquals(1, $item['_id']);
306 307 308 309 310 311 312 313 314 315 316
	}

	public function testTake()
	{
		DB::collection('items')->insert(array(
			array('name' => 'knife', 'type' => 'sharp', 'amount' => 34),
			array('name' => 'fork',  'type' => 'sharp', 'amount' => 20),
			array('name' => 'spoon', 'type' => 'round', 'amount' => 3),
			array('name' => 'spoon', 'type' => 'round', 'amount' => 14)
		));

317
		$items = DB::collection('items')->orderBy('name')->take(2)->get();
318
		$this->assertEquals(2, count($items));
319
		$this->assertEquals('fork', $items[0]['name']);
320 321 322 323 324 325 326 327 328 329 330
	}

	public function testSkip()
	{
		DB::collection('items')->insert(array(
			array('name' => 'knife', 'type' => 'sharp', 'amount' => 34),
			array('name' => 'fork',  'type' => 'sharp', 'amount' => 20),
			array('name' => 'spoon', 'type' => 'round', 'amount' => 3),
			array('name' => 'spoon', 'type' => 'round', 'amount' => 14)
		));

331
		$items = DB::collection('items')->orderBy('name')->skip(2)->get();
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
		$this->assertEquals(2, count($items));
		$this->assertEquals('spoon', $items[0]['name']);
	}

	public function testPluck()
	{
		DB::collection('users')->insert(array(
			array('name' => 'Jane Doe', 'age' => 20),
			array('name' => 'John Doe', 'age' => 25)
		));

		$age = DB::collection('users')->where('name', 'John Doe')->pluck('age');
		$this->assertEquals(25, $age);
	}

	public function testList()
	{
		DB::collection('items')->insert(array(
			array('name' => 'knife', 'type' => 'sharp', 'amount' => 34),
			array('name' => 'fork',  'type' => 'sharp', 'amount' => 20),
			array('name' => 'spoon', 'type' => 'round', 'amount' => 3),
			array('name' => 'spoon', 'type' => 'round', 'amount' => 14)
		));

		$list = DB::collection('items')->lists('name');
357
		sort($list);
358
		$this->assertEquals(4, count($list));
359
		$this->assertEquals(array('fork', 'knife', 'spoon', 'spoon'), $list);
360 361

		$list = DB::collection('items')->lists('type', 'name');
362
		$this->assertEquals(3, count($list));
363 364 365
		$this->assertEquals(array('knife' => 'sharp', 'fork' => 'sharp', 'spoon' => 'round'), $list);
	}

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
	public function testAggregate()
	{
		DB::collection('items')->insert(array(
			array('name' => 'knife', 'type' => 'sharp', 'amount' => 34),
			array('name' => 'fork',  'type' => 'sharp', 'amount' => 20),
			array('name' => 'spoon', 'type' => 'round', 'amount' => 3),
			array('name' => 'spoon', 'type' => 'round', 'amount' => 14)
		));

		$this->assertEquals(71, DB::collection('items')->sum('amount'));
		$this->assertEquals(4, DB::collection('items')->count('amount'));
		$this->assertEquals(3, DB::collection('items')->min('amount'));
		$this->assertEquals(34, DB::collection('items')->max('amount'));
		$this->assertEquals(17.75, DB::collection('items')->avg('amount'));

		$this->assertEquals(2, DB::collection('items')->where('name', 'spoon')->count('amount'));
		$this->assertEquals(14, DB::collection('items')->where('name', 'spoon')->max('amount'));
	}

	public function testSubdocumentAggregate()
	{
		DB::collection('items')->insert(array(
			array('name' => 'knife', 'amount' => array('hidden' => 10, 'found' => 3)),
			array('name' => 'fork',  'amount' => array('hidden' => 35, 'found' => 12)),
			array('name' => 'spoon', 'amount' => array('hidden' => 14, 'found' => 21)),
			array('name' => 'spoon', 'amount' => array('hidden' => 6, 'found' => 4))
		));

		$this->assertEquals(65, DB::collection('items')->sum('amount.hidden'));
		$this->assertEquals(4, DB::collection('items')->count('amount.hidden'));
		$this->assertEquals(6, DB::collection('items')->min('amount.hidden'));
		$this->assertEquals(35, DB::collection('items')->max('amount.hidden'));
		$this->assertEquals(16.25, DB::collection('items')->avg('amount.hidden'));
	}

401 402 403 404 405 406 407 408 409 410 411
	public function testUpsert()
	{
		DB::collection('items')->where('name', 'knife')
							   ->update(
							   		array('amount' => 1),
							   		array('upsert' => true)
							   	);

		$this->assertEquals(1, DB::collection('items')->count());
	}

Jens Segers's avatar
Jens Segers committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	public function testUnset()
	{
		$id1 = DB::collection('users')->insertGetId(array('name' => 'John Doe', 'note1' => 'ABC', 'note2' => 'DEF'));
		$id2 = DB::collection('users')->insertGetId(array('name' => 'Jane Doe', 'note1' => 'ABC', 'note2' => 'DEF'));

		DB::collection('users')->where('name', 'John Doe')->unset('note1');

		$user1 = DB::collection('users')->find($id1);
		$user2 = DB::collection('users')->find($id2);

		$this->assertFalse(isset($user1['note1']));
		$this->assertTrue(isset($user1['note2']));
		$this->assertTrue(isset($user2['note1']));
		$this->assertTrue(isset($user2['note2']));

		DB::collection('users')->where('name', 'Jane Doe')->unset(array('note1', 'note2'));

		$user2 = DB::collection('users')->find($id2);
		$this->assertFalse(isset($user2['note1']));
		$this->assertFalse(isset($user2['note2']));
	}

434 435
	public function testUpdateSubdocument()
	{
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
		$id = DB::collection('users')->insertGetId(array('name' => 'John Doe', 'address' => array('country' => 'Belgium')));

		DB::collection('users')->where('_id', $id)->update(array('address.country' => 'England'));

		$check = DB::collection('users')->find($id);
		$this->assertEquals('England', $check['address']['country']);
	}

	public function testDates()
	{
		DB::collection('users')->insert(array(
			array('name' => 'John Doe', 'birthday' => new MongoDate(strtotime("1980-01-01 00:00:00"))),
			array('name' => 'Jane Doe', 'birthday' => new MongoDate(strtotime("1981-01-01 00:00:00"))),
			array('name' => 'Robert Roe', 'birthday' => new MongoDate(strtotime("1982-01-01 00:00:00"))),
			array('name' => 'Mark Moe', 'birthday' => new MongoDate(strtotime("1983-01-01 00:00:00"))),
451 452
		));

453 454
		$user = DB::collection('users')->where('birthday', new MongoDate(strtotime("1980-01-01 00:00:00")))->first();
		$this->assertEquals('John Doe', $user['name']);
455

456 457 458
		$user = DB::collection('users')->where('birthday', '=', new DateTime("1980-01-01 00:00:00"))->first();
		$this->assertEquals('John Doe', $user['name']);

459 460
		$start = new MongoDate(strtotime("1981-01-01 00:00:00"));
		$stop = new MongoDate(strtotime("1982-01-01 00:00:00"));
461

462 463
		$users = DB::collection('users')->whereBetween('birthday', array($start, $stop))->get();
		$this->assertEquals(2, count($users));
464 465
	}

466 467 468 469 470 471 472 473 474 475
	public function testOperators()
	{
		DB::collection('users')->insert(array(
			array('name' => 'John Doe', 'age' => 30),
			array('name' => 'Jane Doe'),
			array('name' => 'Robert Roe', 'age' => 'thirty-one'),
		));

		$results = DB::collection('users')->where('age', 'exists', true)->get();
		$this->assertEquals(2, count($results));
476 477 478
		$resultsNames = array($results[0]['name'], $results[1]['name']);
		$this->assertContains('John Doe', $resultsNames);
		$this->assertContains('Robert Roe', $resultsNames);
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

		$results = DB::collection('users')->where('age', 'exists', false)->get();
		$this->assertEquals(1, count($results));
		$this->assertEquals('Jane Doe', $results[0]['name']);

		$results = DB::collection('users')->where('age', 'type', 2)->get();
		$this->assertEquals(1, count($results));
		$this->assertEquals('Robert Roe', $results[0]['name']);

		$results = DB::collection('users')->where('age', 'mod', array(15, 0))->get();
		$this->assertEquals(1, count($results));
		$this->assertEquals('John Doe', $results[0]['name']);

		$results = DB::collection('users')->where('age', 'mod', array(29, 1))->get();
		$this->assertEquals(1, count($results));
		$this->assertEquals('John Doe', $results[0]['name']);

		$results = DB::collection('users')->where('age', 'mod', array(14, 0))->get();
		$this->assertEquals(0, count($results));

		DB::collection('items')->insert(array(
			array('name' => 'fork',  'tags' => array('sharp', 'pointy')),
			array('name' => 'spork', 'tags' => array('sharp', 'pointy', 'round', 'bowl')),
			array('name' => 'spoon', 'tags' => array('round', 'bowl')),
		));

		$results = DB::collection('items')->where('tags', 'all', array('sharp', 'pointy'))->get();
		$this->assertEquals(2, count($results));

		$results = DB::collection('items')->where('tags', 'all', array('sharp', 'round'))->get();
		$this->assertEquals(1, count($results));

		$results = DB::collection('items')->where('tags', 'size', 2)->get();
		$this->assertEquals(2, count($results));

		$results = DB::collection('items')->where('tags', 'size', 3)->get();
		$this->assertEquals(0, count($results));

		$results = DB::collection('items')->where('tags', 'size', 4)->get();
		$this->assertEquals(1, count($results));
Jens Segers's avatar
Jens Segers committed
519 520 521 522

		$regex = new MongoRegex("/.*doe/i");
		$results = DB::collection('users')->where('name', 'regex', $regex)->get();
		$this->assertEquals(2, count($results));
523 524 525

		$results = DB::collection('users')->where('name', 'REGEX', $regex)->get();
		$this->assertEquals(2, count($results));
526 527
	}

528 529 530 531 532
	public function testIncrement()
	{
		DB::collection('users')->insert(array(
			array('name' => 'John Doe', 'age' => 30, 'note' => 'adult'),
			array('name' => 'Jane Doe', 'age' => 10, 'note' => 'minor'),
533
			array('name' => 'Robert Roe', 'age' => null),
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
			array('name' => 'Mark Moe'),
		));

		$user = DB::collection('users')->where('name', 'John Doe')->first();
		$this->assertEquals(30, $user['age']);

		DB::collection('users')->where('name', 'John Doe')->increment('age');
		$user = DB::collection('users')->where('name', 'John Doe')->first();
		$this->assertEquals(31, $user['age']);

		DB::collection('users')->where('name', 'John Doe')->decrement('age');
		$user = DB::collection('users')->where('name', 'John Doe')->first();
		$this->assertEquals(30, $user['age']);

		DB::collection('users')->where('name', 'John Doe')->increment('age', 5);
		$user = DB::collection('users')->where('name', 'John Doe')->first();
		$this->assertEquals(35, $user['age']);

		DB::collection('users')->where('name', 'John Doe')->decrement('age', 5);
		$user = DB::collection('users')->where('name', 'John Doe')->first();
		$this->assertEquals(30, $user['age']);

		DB::collection('users')->where('name', 'Jane Doe')->increment('age', 10, array('note' => 'adult'));
		$user = DB::collection('users')->where('name', 'Jane Doe')->first();
		$this->assertEquals(20, $user['age']);
		$this->assertEquals('adult', $user['note']);

		DB::collection('users')->where('name', 'John Doe')->decrement('age', 20, array('note' => 'minor'));
		$user = DB::collection('users')->where('name', 'John Doe')->first();
		$this->assertEquals(10, $user['age']);
		$this->assertEquals('minor', $user['note']);

		DB::collection('users')->increment('age');
		$user = DB::collection('users')->where('name', 'John Doe')->first();
		$this->assertEquals(11, $user['age']);
		$user = DB::collection('users')->where('name', 'Jane Doe')->first();
		$this->assertEquals(21, $user['age']);
571 572
		$user = DB::collection('users')->where('name', 'Robert Roe')->first();
		$this->assertEquals(null, $user['age']);
573
		$user = DB::collection('users')->where('name', 'Mark Moe')->first();
574
		$this->assertEquals(1, $user['age']);
575 576
	}

577
}