<commit_before><?php

/*
  +------------------------------------------------------------------------+
  | Phalcon Framework                                                      |
  +------------------------------------------------------------------------+
  | Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com)       |
  +------------------------------------------------------------------------+
  | This source file is subject to the New BSD License that is bundled     |
  | with this package in the file docs/LICENSE.txt.                        |
  |                                                                        |
  | If you did not receive a copy of the license and are unable to         |
  | obtain it through the world-wide-web, please send an email             |
  | to <EMAIL> so we can send you a copy immediately.       |
  +------------------------------------------------------------------------+
  | Authors: <NAME> <<EMAIL>>                      |
  |          <NAME> <<EMAIL>>                         |
  +------------------------------------------------------------------------+
*/

use Phalcon\Mvc\Model\Message as ModelMessage;

class Issue_1534 extends \Phalcon\Mvc\Model
{
}

class ModelsTest extends PHPUnit_Framework_TestCase
{

	public function __construct()
	{
		spl_autoload_register(array($this, 'modelsAutoloader'));
	}

	public function __destruct()
	{
		spl_autoload_unregister(array($this, 'modelsAutoloader'));
	}

	public function modelsAutoloader($className)
	{
		if (file_exists('unit-tests/models/'.$className.'.php')) {
			require 'unit-tests/models/'.$className.'.php';
		}
	}

	protected function _prepareDb($db){
		$db->delete("personas", "estado='X'");
		$db->delete("personas", "cedula LIKE 'CELL%'");
	}

	protected function _getDI($dbService)
	{

		Phalcon\DI::reset();

		$di = new Phalcon\DI();

		$di->set('modelsManager', function(){
			return new Phalcon\Mvc\Model\Manager();
		});

		$di->set('modelsMetadata', function(){
			return new Phalcon\Mvc\Model\Metadata\Memory();
		});

		$di->set('db', $dbService);

		return $di;
	}

	public function testModelsMysql()
	{
		require 'unit-tests/config.db.php';
		if (empty($configMysql)) {
			$this->markTestSkipped("Skipped");
			return;
		}

		$di = $this->_getDI(function(){
			require 'unit-tests/config.db.php';
			$db = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql);
		/*
			$em = new \Phalcon\Events\Manager();
			$em->attach('db', function($event, $connection) {
				if ($event->getType() == 'beforeQuery') {
					echo $connection->getSQLStatement(), PHP_EOL;
					print_r($connection->getSQLVariables());
				}
			});

			$db->setEventsManager($em);
		*/
			return $db;
		});

		$this->_executeTestsNormal($di);
		$this->_executeTestsRenamed($di);

		$this->issue1534($di);
	}

	public function testModelsPostgresql()
	{
		require 'unit-tests/config.db.php';
		if (empty($configPostgresql)) {
			$this->markTestSkipped("Skipped");
			return;
		}

		$di = $this->_getDI(function(){
			require 'unit-tests/config.db.php';
			return new Phalcon\Db\Adapter\Pdo\Postgresql($configPostgresql);
		});

		$this->_executeTestsNormal($di);
		$this->_executeTestsRenamed($di);
	}

	public function testModelsSqlite()
	{
		require 'unit-tests/config.db.php';
		if (empty($configSqlite)) {
			$this->markTestSkipped("Skipped");
			return;
		}

		$di = $this->_getDI(function(){
			require 'unit-tests/config.db.php';
			return new Phalcon\Db\Adapter\Pdo\Sqlite($configSqlite);
		});

		$this->_executeTestsNormal($di);
		$this->_executeTestsRenamed($di);
	}

	protected function issue1534($di)
	{
		$db = $di->getShared('db');
		$this->_prepareDb($di->getShared('db'));

		$this->assertTrue($db->delete('issue_1534'));

		$product = new Issue_1534();
		$product->language = new \Phalcon\Db\RawValue('default(language)');
		$product->name     = 'foo';
		$product->slug     = 'bar';
		$product->brand    = new \Phalcon\Db\RawValue('default');
		$product->sort     = new \Phalcon\Db\RawValue('default');
		$this->assertTrue($product->save());
		$this->assertEquals(1, Issue_1534::count());

		$entry = Issue_1534::findFirst();
		$this->assertEquals('bb', $entry->language);
		$this->assertEquals('0', $entry->sort);
		$this->assertTrue($entry->brand === NULL);

		$this->assertTrue($entry->delete());

		$product = new Issue_1534();
		$product->language = 'en';
		$product->name     = 'foo';
		$product->slug     = 'bar';
		$product->brand    = 'brand';
		$product->sort     = 1;
		$this->assertTrue($product->save());
		$this->assertEquals(1, Issue_1534::count());

		$entry = Issue_1534::findFirst();
		$entry->brand    = new \Phalcon\Db\RawValue('default');
		$entry->sort     = new \Phalcon\Db\RawValue('default');
		$this->assertTrue($entry->save());
		$this->assertEquals(1, Issue_1534::count());

		$entry = Issue_1534::findFirst();
		$this->assertEquals('0', $entry->sort);
		$this->assertTrue($entry->brand === NULL);

/* FIXME: this does not work yet
		$entry->language = new \Phalcon\Db\RawValue('default(language)');
		$entry->language = 'es';
		$this->assertTrue($entry->save());
		$this->assertEquals(1, Issue_1534::count());

		$entry = Issue_1534::findFirst();
		$this->assertEquals('es', $entry->language);
		$this->assertEquals('0', $entry->sort);
		$this->assertTrue($entry->brand === NULL);
*/

		$this->assertTrue($db->delete('issue_1534'));
	}

	protected function _executeTestsNormal($di){

		$this->_prepareDb($di->getShared('db'));

		//Count tests
		$this->assertEquals(People::count(), Personas::count());

		$params = array();
		$this->assertEquals(People::count($params), Personas::count($params));

		$params = array("estado='I'");
		$this->assertEquals(People::count($params), Personas::count($params));

		$params = "estado='I'";
		$this->assertEquals(People::count($params), Personas::count($params));

		$params = array("conditions" => "estado='I'");
		$this->assertEquals(People::count($params), Personas::count($params));

		//Find first
		$people = People::findFirst();
		$this->assertTrue(is_object($people));
		$this->assertEquals(get_class($people), 'People');

		$persona = Personas::findFirst();
		$this->assertEquals($people->nombres, $persona->nombres);
		$this->assertEquals($people->estado, $persona->estado);<commit_msg>Test case<commit_after><?php

/*
  +------------------------------------------------------------------------+
  | Phalcon Framework                                                      |
  +------------------------------------------------------------------------+
  | Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com)       |
  +------------------------------------------------------------------------+
  | This source file is subject to the New BSD License that is bundled     |
  | with this package in the file docs/LICENSE.txt.                        |
  |                                                                        |
  | If you did not receive a copy of the license and are unable to         |
  | obtain it through the world-wide-web, please send an email             |
  | to <EMAIL>.com so we can send you a copy immediately.       |
  +------------------------------------------------------------------------+
  | Authors: <NAME> <<EMAIL>>                      |
  |          <NAME> <<EMAIL>>                         |
  +------------------------------------------------------------------------+
*/

use Phalcon\Mvc\Model\Message as ModelMessage;

class Issue_1534 extends \Phalcon\Mvc\Model
{
}

class ModelsTest extends PHPUnit_Framework_TestCase
{

	public function __construct()
	{
		spl_autoload_register(array($this, 'modelsAutoloader'));
	}

	public function __destruct()
	{
		spl_autoload_unregister(array($this, 'modelsAutoloader'));
	}

	public function tearDown()
	{
		Phalcon\Mvc\Model::setup(array(
			'phqlLiterals' => true,
		));
	}

	public function modelsAutoloader($className)
	{
		if (file_exists('unit-tests/models/'.$className.'.php')) {
			require 'unit-tests/models/'.$className.'.php';
		}
	}

	protected function _prepareDb($db){
		$db->delete("personas", "estado='X'");
		$db->delete("personas", "cedula LIKE 'CELL%'");
	}

	protected function _getDI($dbService)
	{

		Phalcon\DI::reset();

		$di = new Phalcon\DI();

		$di->set('modelsManager', function(){
			return new Phalcon\Mvc\Model\Manager();
		});

		$di->set('modelsMetadata', function(){
			return new Phalcon\Mvc\Model\Metadata\Memory();
		});

		$di->set('db', $dbService);

		return $di;
	}

	public function testModelsMysql()
	{
		require 'unit-tests/config.db.php';
		if (empty($configMysql)) {
			$this->markTestSkipped("Skipped");
			return;
		}

		$di = $this->_getDI(function(){
			require 'unit-tests/config.db.php';
			$db = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql);
		/*
			$em = new \Phalcon\Events\Manager();
			$em->attach('db', function($event, $connection) {
				if ($event->getType() == 'beforeQuery') {
					echo $connection->getSQLStatement(), PHP_EOL;
					print_r($connection->getSQLVariables());
				}
			});

			$db->setEventsManager($em);
		*/
			return $db;
		});

		$this->_executeTestsNormal($di);
		$this->_executeTestsRenamed($di);

		$this->issue1534($di);
		$this->issue886($di);
	}

	public function testModelsPostgresql()
	{
		require 'unit-tests/config.db.php';
		if (empty($configPostgresql)) {
			$this->markTestSkipped("Skipped");
			return;
		}

		$di = $this->_getDI(function(){
			require 'unit-tests/config.db.php';
			return new Phalcon\Db\Adapter\Pdo\Postgresql($configPostgresql);
		});

		$this->_executeTestsNormal($di);
		$this->_executeTestsRenamed($di);

		$this->issue886($di);
	}

	public function testModelsSqlite()
	{
		require 'unit-tests/config.db.php';
		if (empty($configSqlite)) {
			$this->markTestSkipped("Skipped");
			return;
		}

		$di = $this->_getDI(function(){
			require 'unit-tests/config.db.php';
			return new Phalcon\Db\Adapter\Pdo\Sqlite($configSqlite);
		});

		$this->_executeTestsNormal($di);
		$this->_executeTestsRenamed($di);

		$this->issue886($di);
	}

	protected function issue1534($di)
	{
		$db = $di->getShared('db');
		$this->_prepareDb($di->getShared('db'));

		$this->assertTrue($db->delete('issue_1534'));

		$product = new Issue_1534();
		$product->language = new \Phalcon\Db\RawValue('default(language)');
		$product->name     = 'foo';
		$product->slug     = 'bar';
		$product->brand    = new \Phalcon\Db\RawValue('default');
		$product->sort     = new \Phalcon\Db\RawValue('default');
		$this->assertTrue($product->save());
		$this->assertEquals(1, Issue_1534::count());

		$entry = Issue_1534::findFirst();
		$this->assertEquals('bb', $entry->language);
		$this->assertEquals('0', $entry->sort);
		$this->assertTrue($entry->brand === NULL);

		$this->assertTrue($entry->delete());

		$product = new Issue_1534();
		$product->language = 'en';
		$product->name     = 'foo';
		$product->slug     = 'bar';
		$product->brand    = 'brand';
		$product->sort     = 1;
		$this->assertTrue($product->save());
		$this->assertEquals(1, Issue_1534::count());

		$entry = Issue_1534::findFirst();
		$entry->brand    = new \Phalcon\Db\RawValue('default');
		$entry->sort     = new \Phalcon\Db\RawValue('default');
		$this->assertTrue($entry->save());
		$this->assertEquals(1, Issue_1534::count());

		$entry = Issue_1534::findFirst();
		$this->assertEquals('0', $entry->sort);
		$this->assertTrue($entry->brand === NULL);

/* FIXME: this does not work yet
		$entry->language = new \Phalcon\Db\RawValue('default(language)');
		$entry->language = 'es';
		$this->assertTrue($entry->save());
		$this->assertEquals(1, Issue_1534::count());

		$entry = Issue_1534::findFirst();
		$this->assertEquals('es', $entry->language);
		$this->assertEquals('0', $entry->sort);
		$this->assertTrue($entry->brand === NULL);
*/

		$this->assertTrue($db->delete('issue_1534'));
	}

	protected function issue886($di)
	{
		$this->_prepareDb($di->getShared('db'));

		Phalcon\Mvc\Model::setup(array(
			'phqlLiterals' => false,
		));

		$people = People::findFirst();
		$this->assertTrue(is_object($people));
		$this->assertEquals(get_class($people), 'People');
	}

	protected function _executeTestsNormal($di){

		$this->_prepareDb($di->getShared('db'));

		//Count tests
		$this->assertEquals(People::count(), Personas::count());

		$params = array();
		$this->assertEquals(People::count($params), Personas::count($params));

		$params = array("estado='I'");
		$this->assertEquals(People::count($params), Personas::count($params));

		$params = "estado='I'";
		$this->assertEquals(People::count($params), Personas::count($params));

		$params = array("conditions" => "estado='I'");
		$this->assertEquals(People::count($params), Personas::count($params));

		//Find first
		$people = People::findFirst();
		$this->assertTrue(is_object($people));
		$this->assertEquals(get_class($people), 'People');

		$persona = Personas::findFirst();
		$this->assertEquals($people->nombres, $persona->nombres);
		$this->assertEquals($people->estado, $persona->estado);
{'fraction_non_alphanumeric': 0.2387646923254206, 'fraction_numerical': 0.012598909118844587, 'mean_word_length': 2.8836515513126493, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 206, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3918678', 'n_tokens_mistral': 4580, 'n_tokens_neox': 4113, 'n_words': 673}