|
1 | 1 | <?php
|
2 |
| -class CollectionTest extends PHPUnit_Framework_TestCase |
3 |
| -{ |
4 |
| - public function testPushAndPop() |
5 |
| - { |
6 |
| - $stack = array(); |
7 |
| - $this->assertEquals(0, count($stack)); |
8 |
| - |
9 |
| - array_push($stack, 'foo'); |
10 |
| - $this->assertEquals('foo', $stack[count($stack)-1]); |
11 |
| - $this->assertEquals(1, count($stack)); |
12 |
| - |
13 |
| - $this->assertEquals('foo', array_pop($stack)); |
14 |
| - $this->assertEquals(0, count($stack)); |
| 2 | +class CollectionTest extends PHPUnit_Framework_TestCase { |
| 3 | + |
| 4 | + function setUp() { |
| 5 | + require __DIR__ . "/" . "utils.inc"; |
| 6 | + $this->faker = Faker\Factory::create(); |
| 7 | + $this->faker->seed(1234); |
| 8 | + |
| 9 | + $this->manager = new MongoDB\Manager("mongodb://localhost"); |
| 10 | + $this->collection = new MongoDB\Collection($this->manager, "test.case"); |
| 11 | + $this->collection->deleteMany(array()); |
| 12 | + } |
| 13 | + |
| 14 | + function testInsertAndRetrieve() { |
| 15 | + $collection = $this->collection; |
| 16 | + |
| 17 | + for($i=0; $i<10;$i++) { |
| 18 | + $user = createUser($this->faker); |
| 19 | + $result = $collection->insertOne($user); |
| 20 | + $this->assertInstanceOf("MongoDB\\InsertResult", $result); |
| 21 | + $this->assertInstanceOf("BSON\ObjectId", $result->getInsertedId()); |
| 22 | + $this->assertEquals(24, strlen($result->getInsertedId())); |
| 23 | + |
| 24 | + $user["_id"] = $result->getInsertedId(); |
| 25 | + $document = $collection->findOne(array("_id" => $result->getInsertedId())); |
| 26 | + $this->assertEquals($document, $user, "The inserted and returned objects are the same"); |
| 27 | + } |
| 28 | + |
| 29 | + $this->assertEquals(10, $i); |
| 30 | + |
| 31 | + |
| 32 | + $query = array("firstName" => "Ransom"); |
| 33 | + $count = $collection->count($query); |
| 34 | + $this->assertEquals(1, $count); |
| 35 | + $cursor = $collection->find($query); |
| 36 | + $this->assertInstanceOf("MongoDB\\QueryResult", $cursor); |
| 37 | + |
| 38 | + foreach($cursor as $n => $person) { |
| 39 | + $this->assertInternalType("array", $person); |
| 40 | + } |
| 41 | + $this->assertEquals(0, $n); |
15 | 42 | }
|
16 | 43 | }
|
17 | 44 |
|
0 commit comments