File: MongoObjDBTest.php

package info (click to toggle)
php-mongo 1.5.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,040 kB
  • ctags: 2,802
  • sloc: ansic: 17,632; xml: 2,195; php: 1,630; pascal: 330; makefile: 52; sh: 39
file content (150 lines) | stat: -rw-r--r-- 4,859 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
<?php
/**
 * Test class for Mongo.
 * Generated by PHPUnit on 2009-04-09 at 18:09:02.
 */
class MongoObjDBTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var    Mongo
     * @access protected
     */
    protected $object;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     *
     * @access protected
     */
    public function setUp() {
      ini_set('mongo.objects', 1);
      $m = new Mongo();
      $this->object = $m->selectDB("phpunit");
    }

    public function tearDown() {
      ini_set('mongo.objects', 0);
    }

    public function testDBDrop() {
        $r = $this->object->drop();
        $this->assertEquals(true, (bool)$r['ok'], json_encode($r));
    }

    public function testRepair() {
      $r = $this->object->repair();
      $this->assertEquals(true, (bool)$r['ok'], json_encode($r));
    }

    public function testCreateCollection() {
        $ns = $this->object->selectCollection('system.namespaces');
        $this->object->drop('z');
        $this->object->drop('zz');
        $this->object->drop('zzz');

        $this->object->createCollection('z');
        $obj = $ns->findOne((object)array('name' => 'phpunit.z'));
        $this->assertNotNull($obj);

        $c = $this->object->createCollection('zz', true, 100);
        $obj = $ns->findOne((object)array('name' => 'phpunit.zz'));
        $this->assertNotNull($obj);

        for($i=0;$i<100;$i++) {
            $c->insert((object)array('x' => $i));
        }
        $this->assertLessThan(100, $c->count());

        $c = $this->object->createCollection('zzz', true, 1000, 5);
        $obj = $ns->findOne((object)array('name' => 'phpunit.zzz'));
        $this->assertNotNull($obj);

        for($i=0;$i<10;$i++) {
            $c->insert((object)array('x' => $i));
        }
        $this->assertEquals(5, $c->count());
    }

    public function testListCollections() {
        $ns = $this->object->selectCollection('system.namespaces');

        for($i=0;$i<10;$i++) {
            $c = $this->object->selectCollection("x$i");
            $c->insert((object)array("foo" => "bar"), array("safe" => true));
        }

        $list = $this->object->listCollections();
        for($i=0;$i<10;$i++) {
            $this->assertTrue($list[$i] instanceof MongoCollection);
            if (!preg_match("/5\.1\../", phpversion())) {
              $this->assertTrue(in_array("phpunit.x$i", $list));
            }
        }
    }


    public function testCreateDBRef() {
        $arr = (object)array('_id' => new MongoId());
        $ref = $this->object->createDBRef('foo.bar', $arr);
        $this->assertNotNull($ref);
        $this->assertTrue(is_array($ref));

        $arr = (object)array('_id' => 1);
        $ref = $this->object->createDBRef('foo.bar', $arr);
        $this->assertNotNull($ref);
        $this->assertTrue(is_array($ref));

        $ref = $this->object->createDBRef('foo.bar', new MongoId());
        $this->assertNotNull($ref);
        $this->assertTrue(is_array($ref));

        $id = new MongoId();
        $ref = $this->object->createDBRef('foo.bar', (object)array('_id' => $id, 'y' => 3));
        $this->assertNotNull($ref);
        $this->assertEquals((string)$id, (string)$ref['$id']);
    }

    public function testGetDBRef() {
        $c = $this->object->selectCollection('foo');
        $c->drop();
        for($i=0;$i<50;$i++) {
            $c->insert((object)array('x' => rand()), array("safe" => true));
        }
        $obj = $c->findOne();

        $ref = $this->object->createDBRef('foo', $obj);
        $obj2 = $this->object->getDBRef($ref);

        $this->assertNotNull($obj2);
        $this->assertEquals($obj['x'], $obj2['x']);
    }

    public function testDBCommand() {
        $x = $this->object->command(array());
        $this->assertEquals(0, strpos($x['errmsg'], "no such cmd"), json_encode($x));
        $this->assertEquals((bool)$x['ok'], false);

        $created = $this->object->createCollection("system.profile", true, 5000);

        $this->object->command(array('profile' => 0));
        $x = $this->object->command(array('profile' => 1));
        $this->assertEquals($x['was'], 0, json_encode($x));
        $this->assertEquals((bool)$x['ok'], true, json_encode($x));
    }

    public function testCreateRef() {
        $ref = MongoDBRef::create("x", "y");
        $this->assertEquals('x', $ref['$ref']);
        $this->assertEquals('y', $ref['$id']);
    }

    public function testIsRef() {
        $this->assertFalse(MongoDBRef::isRef((object)array()));
        $this->assertFalse(MongoDBRef::isRef((object)array('$ns' => 'foo', '$id' => 'bar')));

        $ref = (object)array('$ref' => 'blog.posts', '$id' => new MongoId('cb37544b9dc71e4ac3116c00'));
        $this->assertTrue(MongoDBRef::isRef($ref));
    }
}
?>