Location: PHPKode > projects > MongoDB PHP Driver > mongodb-mongo-php-driver-a593f23/tests/MongoCodeTest.php
<?php
require_once 'PHPUnit/Framework.php';

/**
 * Test class for MongoCode
 * Generated by PHPUnit on 2009-04-10 at 13:30:28.
 */
class MongoCodeTest extends PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
        $m = new Mongo();
        $this->object = $m->selectCollection("phpunit", "bindata");
        $this->object->drop();
    }

    public function testWeird() {
        $c = new MongoCode(3);
        $this->assertEquals($c->code, "3");
        //        $this->assertEquals(0, count($c->scope));

        $c = new MongoCode(NULL, array("x"=>1));
        $this->assertEquals($c->code, "");
        $this->assertEquals(count($c->scope), 1);
        $this->assertEquals($c->scope['x'], 1);
    }

    public function testBasic() {
      $code_str = "if(x<5){ return true; } else { return false;}";

      $c = $this->object;
      $c->drop();

      $code = new MongoCode($code_str, array());
      $c->insert(array("something" => $code));
      $obj = $c->findOne();
      $this->assertEquals($obj['something']->code, $code_str);
      $this->assertEquals(0, count($obj['something']->scope));

      $c->remove();
      $code = new MongoCode($code_str, array("x" => 2));
      $c->insert(array("something" => $code));
      $obj = $c->findOne();
      $this->assertEquals($code_str, $obj['something']->code);
      $this->assertEquals(1, count($obj['something']->scope));
      $this->assertEquals(2, $obj['something']->scope['x']);
      
      $c->remove();
      $code = new MongoCode($code_str);
      $c->insert(array("something" => $code));
      $obj = $c->findOne();
      $this->assertEquals($obj['something']->code, $code_str);
      $this->assertEquals(0, count($obj['something']->scope));
    }
}

?>
Return current item: MongoDB PHP Driver