yiiapi/tests/framework/web/helpers/CJSONTest.php

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

<?php
Yii::import('system.db.CDbConnection');
Yii::import('system.db.ar.CActiveRecord');

require_once(dirname(__FILE__).'/../../db/data/models.php');

/**
 * CJSON Test
 */
class CJSONTest extends CTestCase {
	private $db;

	protected function setUp()
	{
		if(!extension_loaded('pdo') || !extension_loaded('pdo_sqlite'))
			$this->markTestSkipped('PDO and SQLite extensions are required.');

		$this->db=new CDbConnection('sqlite::memory:');
		$this->db->active=true;
		$this->db->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/../../db/data/sqlite.sql'));
		CActiveRecord::$db=$this->db;
	}

	protected function tearDown()
	{
		$this->db->active=false;
	}

	/**
	 * native json_encode can't do it
	 * @return void
	 */
	public function testEncodeSingleAR(){
		$post = Post::model()->findByPk(1);
		$this->assertEquals(
			'{"id":"1","title":"post 1","create_time":"100000","author_id":"1","content":"content 1"}',
			CJSON::encode($post)
		);
	}

	/**
	 * native json_encode can't do it
	 * @return void
	 */
	public function testEncodeMultipleARs(){
		$posts=Post::model()->findAllByPk(array(1, 2));
		$this->assertEquals(
			'[{"id":"1","title":"post 1","create_time":"100000","author_id":"1","content":"content 1"},{"id":"2","title":"post 2","create_time":"100001","author_id":"2","content":"content 2"}]',
			CJSON::encode($posts)
		);
	}

	public function testEncodeSimple(){
		$this->assertEquals("true", CJSON::encode(true));
		$this->assertEquals("false", CJSON::encode(false));
		$this->assertEquals("null", CJSON::encode(null));
		$this->assertEquals("123", CJSON::encode(123));
		$this->assertEquals("123.12", CJSON::encode(123.12));
		$this->assertEquals('"test\\\\me"', CJSON::encode('test\me'));
	}
后面还有 40 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。