You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					96 lines
				
				2.1 KiB
			
		
		
			
		
	
	
					96 lines
				
				2.1 KiB
			| 
								 
											12 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace yiiunit\extensions\mongo;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @group mongo
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								class CollectionTest extends MongoTestCase
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
									protected function tearDown()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										$this->dropCollection('customer');
							 | 
						||
| 
								 | 
							
										parent::tearDown();
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// Tests :
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									public function testInsert()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$collection = $this->getConnection()->getCollection('customer');
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$data = [
							 | 
						||
| 
								 | 
							
											'name' => 'customer 1',
							 | 
						||
| 
								 | 
							
											'address' => 'customer 1 address',
							 | 
						||
| 
								 | 
							
										];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$id = $collection->insert($data);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$this->assertTrue($id instanceof \MongoId);
							 | 
						||
| 
								 | 
							
										$this->assertNotEmpty($id->__toString());
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @depends testInsert
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function testFindAll()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$collection = $this->getConnection()->getCollection('customer');
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$data = [
							 | 
						||
| 
								 | 
							
											'name' => 'customer 1',
							 | 
						||
| 
								 | 
							
											'address' => 'customer 1 address',
							 | 
						||
| 
								 | 
							
										];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$id = $collection->insert($data);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$rows = $collection->findAll();
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$this->assertEquals(1, count($rows));
							 | 
						||
| 
								 | 
							
										$this->assertEquals($id, $rows[0]['_id']);
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									public function testSave()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$collection = $this->getConnection()->getCollection('customer');
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$data = [
							 | 
						||
| 
								 | 
							
											'name' => 'customer 1',
							 | 
						||
| 
								 | 
							
											'address' => 'customer 1 address',
							 | 
						||
| 
								 | 
							
										];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$id = $collection->save($data);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$this->assertTrue($id instanceof \MongoId);
							 | 
						||
| 
								 | 
							
										$this->assertNotEmpty($id->__toString());
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @depends testSave
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function testUpdate()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$collection = $this->getConnection()->getCollection('customer');
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$data = [
							 | 
						||
| 
								 | 
							
											'name' => 'customer 1',
							 | 
						||
| 
								 | 
							
											'address' => 'customer 1 address',
							 | 
						||
| 
								 | 
							
										];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$newId = $collection->save($data);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$updatedId = $collection->save($data);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$this->assertEquals($newId, $updatedId, 'Unable to update data!');
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										$data['_id'] = $newId->__toString();
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$updatedId = $collection->save($data);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$this->assertEquals($newId, $updatedId, 'Unable to updated data by string id!');
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @depends testFindAll
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function testRemove()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$collection = $this->getConnection()->getCollection('customer');
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$data = [
							 | 
						||
| 
								 | 
							
											'name' => 'customer 1',
							 | 
						||
| 
								 | 
							
											'address' => 'customer 1 address',
							 | 
						||
| 
								 | 
							
										];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$id = $collection->insert($data);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$collection->remove(['_id' => $id]);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$rows = $collection->findAll();
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$this->assertEquals(0, count($rows));
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 |