Esempio n. 1
0
	/**
	 * Tests handling type values based on specified schema settings.
	 *
	 * @return void
	 */
	public function testTypeCasting() {
		$data = array(
			'_id' => '4c8f86167675abfabd970300',
			'title' => 'Foo',
			'tags' => 'test',
			'comments' => array(
				"4c8f86167675abfabdbe0300", "4c8f86167675abfabdbf0300", "4c8f86167675abfabdc00300"
			),
			'authors' => '4c8f86167675abfabdb00300',
			'created' => time(),
			'modified' => date('Y-m-d H:i:s'),
			'rank_count' => '45',
			'rank' => '3.45688'
		);
		$time = time();
		$model = $this->_model;
		$handlers = $this->_handlers;
		$options = compact('model', 'handlers');
		$result = Exporter::cast($data, $this->_schema, $model::connection(), $options);

		$this->assertEqual(array_keys($data), array_keys($result));
		$this->assertTrue($result['_id'] instanceof MongoId);
		$this->assertEqual('4c8f86167675abfabd970300', (string) $result['_id']);

		$this->assertTrue($result['comments'] instanceof DocumentArray);
		$this->assertEqual(3, count($result['comments']));

		$this->assertTrue($result['comments'][0] instanceof MongoId);
		$this->assertTrue($result['comments'][1] instanceof MongoId);
		$this->assertTrue($result['comments'][2] instanceof MongoId);
		$this->assertEqual('4c8f86167675abfabdbe0300', (string) $result['comments'][0]);
		$this->assertEqual('4c8f86167675abfabdbf0300', (string) $result['comments'][1]);
		$this->assertEqual('4c8f86167675abfabdc00300', (string) $result['comments'][2]);

		$this->assertEqual($data['comments'], $result['comments']->data());
		$this->assertEqual(array('test'), $result['tags']->data());
		$this->assertEqual(array('4c8f86167675abfabdb00300'), $result['authors']->data());
		$this->assertTrue($result['authors'][0] instanceof MongoId);

		$this->assertTrue($result['modified'] instanceof MongoDate);
		$this->assertTrue($result['created'] instanceof MongoDate);
		$this->assertTrue($result['created']->sec > 0);

		$this->assertEqual($time, $result['modified']->sec);
		$this->assertEqual($time, $result['created']->sec);

		$this->assertIdentical(45, $result['rank_count']);
		$this->assertIdentical(3.45688, $result['rank']);
	}