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
11 years ago
|
<?php
|
||
|
|
||
|
namespace yiiunit\extensions\mongo;
|
||
|
|
||
|
/**
|
||
|
* @group mongo
|
||
|
*/
|
||
11 years ago
|
class CollectionTest extends MongoTestCase
|
||
11 years ago
|
{
|
||
|
protected function tearDown()
|
||
|
{
|
||
|
$this->dropCollection('customer');
|
||
|
parent::tearDown();
|
||
|
}
|
||
|
|
||
|
// Tests :
|
||
|
|
||
|
public function testInsert()
|
||
|
{
|
||
11 years ago
|
$collection = $this->getConnection()->getCollection('customer');
|
||
11 years ago
|
$data = [
|
||
|
'name' => 'customer 1',
|
||
|
'address' => 'customer 1 address',
|
||
|
];
|
||
11 years ago
|
$id = $collection->insert($data);
|
||
11 years ago
|
$this->assertTrue($id instanceof \MongoId);
|
||
|
$this->assertNotEmpty($id->__toString());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @depends testInsert
|
||
|
*/
|
||
|
public function testFindAll()
|
||
|
{
|
||
11 years ago
|
$collection = $this->getConnection()->getCollection('customer');
|
||
11 years ago
|
$data = [
|
||
|
'name' => 'customer 1',
|
||
|
'address' => 'customer 1 address',
|
||
|
];
|
||
11 years ago
|
$id = $collection->insert($data);
|
||
11 years ago
|
|
||
11 years ago
|
$rows = $collection->findAll();
|
||
11 years ago
|
$this->assertEquals(1, count($rows));
|
||
|
$this->assertEquals($id, $rows[0]['_id']);
|
||
|
}
|
||
|
|
||
|
public function testSave()
|
||
|
{
|
||
11 years ago
|
$collection = $this->getConnection()->getCollection('customer');
|
||
11 years ago
|
$data = [
|
||
|
'name' => 'customer 1',
|
||
|
'address' => 'customer 1 address',
|
||
|
];
|
||
11 years ago
|
$id = $collection->save($data);
|
||
11 years ago
|
$this->assertTrue($id instanceof \MongoId);
|
||
|
$this->assertNotEmpty($id->__toString());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @depends testSave
|
||
|
*/
|
||
|
public function testUpdate()
|
||
|
{
|
||
11 years ago
|
$collection = $this->getConnection()->getCollection('customer');
|
||
11 years ago
|
$data = [
|
||
|
'name' => 'customer 1',
|
||
|
'address' => 'customer 1 address',
|
||
|
];
|
||
11 years ago
|
$newId = $collection->save($data);
|
||
11 years ago
|
|
||
11 years ago
|
$updatedId = $collection->save($data);
|
||
11 years ago
|
$this->assertEquals($newId, $updatedId, 'Unable to update data!');
|
||
|
|
||
|
$data['_id'] = $newId->__toString();
|
||
11 years ago
|
$updatedId = $collection->save($data);
|
||
11 years ago
|
$this->assertEquals($newId, $updatedId, 'Unable to updated data by string id!');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @depends testFindAll
|
||
|
*/
|
||
|
public function testRemove()
|
||
|
{
|
||
11 years ago
|
$collection = $this->getConnection()->getCollection('customer');
|
||
11 years ago
|
$data = [
|
||
|
'name' => 'customer 1',
|
||
|
'address' => 'customer 1 address',
|
||
|
];
|
||
11 years ago
|
$id = $collection->insert($data);
|
||
11 years ago
|
|
||
11 years ago
|
$collection->remove(['_id' => $id]);
|
||
11 years ago
|
|
||
11 years ago
|
$rows = $collection->findAll();
|
||
11 years ago
|
$this->assertEquals(0, count($rows));
|
||
|
}
|
||
|
}
|