├── .gitattributes ├── .gitignore ├── README.md ├── phpunit.xml ├── schemas └── phalcon_test.sql ├── test ├── .htaccess ├── app │ ├── cache │ │ ├── _home_fish_web_mssql_test_app_views_index.volt.php │ │ └── _home_fish_web_mssql_test_app_views_index_index.volt.php │ ├── config │ │ ├── config.php │ │ ├── loader.php │ │ └── services.php │ ├── controllers │ │ ├── ControllerBase.php │ │ └── IndexController.php │ ├── library │ │ └── db │ │ │ ├── adapter │ │ │ └── Mssql.php │ │ │ ├── dialect │ │ │ └── Mssql.php │ │ │ └── test.php │ ├── models │ │ └── Personas.php │ └── views │ │ ├── index.volt │ │ └── index │ │ └── index.volt ├── index.html └── public │ ├── .htaccess │ └── index.php └── unit-tests ├── DbBindTest.php ├── DbDescribeTest.php ├── DbDialectTest.php ├── DbProfilerTest.php ├── DbTest.php ├── ModelsBehaviorsTest.php ├── ModelsCalculationsTest.php ├── ModelsCriteriaTest.php ├── ModelsDynamicOperationsTest.php ├── ModelsEventsTest.php ├── ModelsFindersTest.php ├── ModelsForeignKeysTest.php ├── ModelsHydrationTest.php ├── ModelsMassAssigmentTest.php ├── ModelsMetadataAdaptersTest.php ├── ModelsMetadataManualTest.php ├── ModelsMetadataStrategyTest.php ├── ModelsMetadataTest.php ├── ModelsMultipleSourcesTest.php ├── ModelsQueryBuilderTest.php ├── ModelsQueryExecuteTest.php ├── ModelsQueryParsingTest.php ├── ModelsRelationsMagicTest.php ├── ModelsRelationsTest.php ├── ModelsResultsetCacheStaticTest.php ├── ModelsResultsetCacheTest.php ├── ModelsResultsetTest.php ├── ModelsSerializeTest.php ├── ModelsSnapshotsTest.php ├── ModelsTest.php ├── ModelsTransactionsTest.php ├── ModelsValidatorsTest.php ├── PaginatorTest.php ├── PlaygroundTest.php ├── cache ├── map-robots.php └── meta-robots-robots.php ├── config.db.local.php ├── config.db.php ├── db ├── adapter │ └── Mssql.php ├── dialect │ └── Mssql.php └── test.php ├── helpers └── xcache.php ├── loader.php └── models ├── Abonnes.php ├── AlbumORama ├── Albums.php ├── Artists.php └── Songs.php ├── Boutique.php ├── Boutique ├── Robots.php └── Robotters.php ├── Cacheable ├── Model.php ├── Parts.php ├── Robots.php └── RobotsParts.php ├── Deles.php ├── Dynamic ├── Personas.php └── Personers.php ├── GossipRobots.php ├── News └── Subscribers.php ├── Parts.php ├── People.php ├── Personas.php ├── Personers.php ├── Personnes.php ├── Pessoas.php ├── Products.php ├── Prueba.php ├── Relations ├── Deles.php ├── M2MParts.php ├── M2MRobots.php ├── M2MRobotsParts.php ├── RelationsParts.php ├── RelationsRobots.php ├── RelationsRobotsParts.php ├── Robotters.php ├── RobottersDeles.php └── Some │ ├── Deles.php │ ├── Parts.php │ ├── Products.php │ ├── Robots.php │ ├── RobotsParts.php │ ├── Robotters.php │ └── RobottersDeles.php ├── Robots.php ├── RobotsParts.php ├── Robotters.php ├── RobottersDeles.php ├── Robotto.php ├── Snapshot ├── Parts.php ├── Robots.php ├── RobotsParts.php └── Robotters.php ├── Some ├── Deles.php ├── Parts.php ├── Products.php ├── Robots.php ├── RobotsParts.php ├── Robotters.php └── RobottersDeles.php ├── Store ├── Parts.php ├── Robots.php └── RobotsParts.php ├── Subscribers.php └── Subscriptores.php /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set default behaviour, in case users don't have core.autocrlf set. 2 | * text=auto 3 | 4 | # Explicitly declare text files we want to always be normalized and converted 5 | # to native line endings on checkout. 6 | *.c text 7 | *.h text 8 | 9 | # Declare files that will always have CRLF line endings on checkout. 10 | *.sln text eol=crlf 11 | 12 | # Denote all files that are truly binary and should not be modified. 13 | *.png binary 14 | *.jpg binary 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.swo 3 | .~* 4 | *.bak 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | phalcon-mssql 2 | ============= 3 | 4 | ## Description : 5 | 6 | A Phalcon PHP Framework MSSQL pdo db adapter. 7 | 8 | ## Requirements : 9 | - Phalcon >= 1.2.0 and <=1.3.x 10 | 11 | ## Installation Instructions : 12 | 1. copy "test/app/library/db" folder to your library folder 13 | 2. Add the namespace to "folder path" setting 14 | --// Register some namespaces 15 | --array( 16 | -- "Twm\Db\Adapter\Pdo" => "library/db/adapter/", 17 | -- "Twm\Db\Dialect" => "library/db/dialect/" 18 | --); 19 | 3. Change the following : 20 | --$descriptor['pdoType']}:host={$descriptor['host']};dbname={$descriptor['dbname']} 21 | to : 22 | --"{$descriptor['pdoType']}:server={$descriptor['host']};database={$descriptor['dbname']}" 23 | 24 | Also you need to make sure 'sqlsrv' is set for $config->database->pdoType. 25 | 26 | ## Upcoming Release : 27 | - Phalcon 2.0 support 28 | 29 | --- 30 | 31 | Please Note : 32 | The adapter works but a few problems still exist. 33 | 34 | Issues: 35 | 1. scaling? 36 | 2. transaction 37 | can only run single transaction 38 | 3.about nolock hint 39 | I have no idea how PDO using nolock hint, so I add a trigger while ordering with id, it will add nolock inside the sql statement. You may change the token at [dialect/Mssql.php](https://github.com/fishjerky/phalcon-mssql/blob/master/test/app/library/db/dialect/Mssql.php#L273) 40 | --$nolockTokens = array('id'); //token to trigger nolock hint` 41 | 42 | --- 43 | Unit Test: 44 | Some test cases did not pass 45 | -ModelsQueryExecuteTest.php 46 | -group by 1 47 | -table Abonnes does not exist - Line 781 48 | 49 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 12 | 13 | 14 | unit-tests/PlaygroundTest.php 15 | 25 | 26 | 33 | 34 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /schemas/phalcon_test.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE [prueba] 2 | ( 3 | id int IDENTITY(1,1) PRIMARY KEY, 4 | [nombre] varchar(255) NOT NULL, 5 | [estado] varchar(255) 6 | ) 7 | 8 | -------------------------------------------------------------------------------- /test/.htaccess: -------------------------------------------------------------------------------- 1 | 2 | RewriteBase /fish/mssql/test 3 | RewriteEngine on 4 | RewriteRule ^$ public/ [L] 5 | RewriteRule (.*) public/$1 [L] 6 | 7 | -------------------------------------------------------------------------------- /test/app/cache/_home_fish_web_mssql_test_app_views_index.volt.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Phalcon PHP Framework 5 | 6 | 7 | getContent(); ?> 8 | 9 | -------------------------------------------------------------------------------- /test/app/cache/_home_fish_web_mssql_test_app_views_index_index.volt.php: -------------------------------------------------------------------------------- 1 |

Congratulations!

2 | 3 |

You're now flying with Phalcon. Great things are about to happen!

-------------------------------------------------------------------------------- /test/app/config/config.php: -------------------------------------------------------------------------------- 1 | array( 5 | 'adapter' => 'Twm\Db\Adapter\Pdo\Mssql', 6 | 'host' => 'McDev', 7 | 'username' => 'apnewmc', 8 | 'password' => 'Cm!2212@12', 9 | 'dbname' => 'CCCMCDEV1', 10 | 'pdoType' => 'dblib', 11 | 'dialectClass' => 'Twm\Db\Dialect\Mssql' 12 | ), 13 | 'application' => array( 14 | 'controllersDir' => __DIR__ . '/../../app/controllers/', 15 | 'modelsDir' => __DIR__ . '/../../app/models/', 16 | 'viewsDir' => __DIR__ . '/../../app/views/', 17 | 'pluginsDir' => __DIR__ . '/../../app/plugins/', 18 | 'libraryDir' => __DIR__ . '/../../app/library/', 19 | 'cacheDir' => __DIR__ . '/../../app/cache/', 20 | 'baseUri' => '/test/', 21 | ) 22 | )); 23 | -------------------------------------------------------------------------------- /test/app/config/loader.php: -------------------------------------------------------------------------------- 1 | registerNamespaces( 9 | array( 10 | "Twm\Db\Adapter\Pdo" => "../app/library/db/adapter/", 11 | "Twm\Db\Dialect" => "../app/library/db/dialect/" 12 | ) 13 | )->register(); 14 | 15 | $loader->registerDirs( 16 | array( 17 | $config->application->controllersDir, 18 | $config->application->modelsDir 19 | ) 20 | )->register(); 21 | -------------------------------------------------------------------------------- /test/app/config/services.php: -------------------------------------------------------------------------------- 1 | set('url', function() use ($config) { 20 | $url = new UrlResolver(); 21 | $url->setBaseUri($config->application->baseUri); 22 | return $url; 23 | }, true); 24 | 25 | /** 26 | * Setting up the view component 27 | */ 28 | $di->set('view', function() use ($config) { 29 | 30 | $view = new View(); 31 | 32 | $view->setViewsDir($config->application->viewsDir); 33 | 34 | $view->registerEngines(array( 35 | '.volt' => function($view, $di) use ($config) { 36 | 37 | $volt = new VoltEngine($view, $di); 38 | 39 | $volt->setOptions(array( 40 | 'compiledPath' => $config->application->cacheDir, 41 | 'compiledSeparator' => '_' 42 | )); 43 | 44 | return $volt; 45 | }, 46 | '.phtml' => 'Phalcon\Mvc\View\Engine\Php' 47 | )); 48 | 49 | return $view; 50 | }, true); 51 | 52 | /** 53 | * Database connection is created based in the parameters defined in the configuration file 54 | */ 55 | $di->set('db', function() use ($config) { 56 | return new Twm\Db\Adapter\Pdo\Mssql(array( 57 | 'host' => $config->database->host, 58 | 'username' => $config->database->username, 59 | 'password' => $config->database->password, 60 | 'dbname' => $config->database->dbname, 61 | 'pdoType' => $config->database->pdoType, 62 | 'dialectClass' => $config->database->dialectClass 63 | )); 64 | }); 65 | 66 | /** 67 | * If the configuration specify the use of metadata adapter use it or use memory otherwise 68 | */ 69 | $di->set('modelsMetadata', function() { 70 | return new MetaDataAdapter(); 71 | }); 72 | 73 | /** 74 | * Start the session the first time some component request the session service 75 | */ 76 | $di->set('session', function() { 77 | $session = new SessionAdapter(); 78 | $session->start(); 79 | return $session; 80 | }); 81 | -------------------------------------------------------------------------------- /test/app/controllers/ControllerBase.php: -------------------------------------------------------------------------------- 1 | nombres); 10 | $this->view->disable(); 11 | return; 12 | 13 | //Store and check for errors 14 | $success = $test->save($this->request->getPost(), array('name', 'email')); 15 | 16 | if ($success) { 17 | echo "Thanks for register!"; 18 | } else { 19 | echo "Sorry, the following problems were generated: "; 20 | foreach ($user->getMessages() as $message) { 21 | echo $message->getMessage(), "
"; 22 | } 23 | } 24 | 25 | 26 | $this->view->disable(); 27 | } 28 | 29 | } 30 | 31 | -------------------------------------------------------------------------------- /test/app/library/db/test.php: -------------------------------------------------------------------------------- 1 | registerDirs( 7 | array('models/') 8 | ); 9 | 10 | //Register some namespaces 11 | $loader->registerNamespaces( 12 | array( 13 | "Twm\Db\Adapter\Pdo" => "adapter/", 14 | "Twm\Db\Dialect" => "dialect/" 15 | ) 16 | ); 17 | 18 | // register autoloader 19 | $loader->register(); 20 | 21 | echo '

connect

'; 22 | $mc = array( 23 | 'host' => 'McDev', 24 | 'username' => 'apnewmc', 25 | 'password' => 'Cm!2212@12', 26 | 'dbname' => 'CCCMCDEV1', 27 | 'dialectClass' => '\Twm\Db\Dialect\Mssql' 28 | 29 | ); 30 | $ec = array( 31 | 'host' => 'MSsql', 32 | 'username' => 'apedtuser2', 33 | 'password' => 'Ecg01dedt', 34 | 'dbname' => 'CCCECST2', 35 | 'dialectClass' => '\Twm\Db\Dialect\Mssql' 36 | ); 37 | $db = new \Twm\Db\Adapter\Pdo\Mssql($mc); 38 | if (!$db->connect()){ 39 | $db->close(); 40 | die('connection failed'); 41 | } 42 | 43 | //testModel($db); 44 | testQueryBinding($db); 45 | 46 | function testModel($db){ 47 | $boutique = new Boutique(); 48 | } 49 | 50 | function testQueryBinding($db){ 51 | echo '

execute query

'; 52 | $sqlStatement = "select * from tb_a_boutique_data where 1=':aaa' and 2=':bbb'"; 53 | $bindParams = array(':aaa'=>'1',':bbb'=>'2'); 54 | 55 | var_dump($db->query($sqlStatement, $bindParams)); 56 | } 57 | 58 | function testDescribeColumns(){ 59 | var_dump($db->describeColumns('tb_a_frist_data')); 60 | } 61 | -------------------------------------------------------------------------------- /test/app/models/Personas.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Phalcon PHP Framework 5 | 6 | 7 | {{ content() }} 8 | 9 | -------------------------------------------------------------------------------- /test/app/views/index/index.volt: -------------------------------------------------------------------------------- 1 |

Congratulations!

2 | 3 |

You're now flying with Phalcon. Great things are about to happen!

-------------------------------------------------------------------------------- /test/index.html: -------------------------------------------------------------------------------- 1 |

Mod-Rewrite is not enabled

Please enable rewrite module on your web server to continue -------------------------------------------------------------------------------- /test/public/.htaccess: -------------------------------------------------------------------------------- 1 | AddDefaultCharset UTF-8 2 | 3 | 4 | RewriteBase /fish/mssql/test/public 5 | RewriteEngine On 6 | RewriteCond %{REQUEST_FILENAME} !-d 7 | RewriteCond %{REQUEST_FILENAME} !-f 8 | RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L] 9 | 10 | -------------------------------------------------------------------------------- /test/public/index.php: -------------------------------------------------------------------------------- 1 | handle()->getContent(); 28 | 29 | } catch (\Exception $e) { 30 | echo $e->getMessage(); 31 | } -------------------------------------------------------------------------------- /unit-tests/DbBindTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | | Rack Lin | 19 | +------------------------------------------------------------------------+ 20 | */ 21 | 22 | use Phalcon\Db\Column as DbColumn; 23 | // Creates the autoloader 24 | require "loader.php"; 25 | class DbBindTest extends PHPUnit_Framework_TestCase 26 | { 27 | public function testDbMssql() 28 | { 29 | require 'unit-tests/config.db.php'; 30 | if (empty($configMssql)) { 31 | $this->markTestSkipped("Skipped"); 32 | return; 33 | } 34 | $connection = new Twm\Db\Adapter\Pdo\Mssql($configMssql); 35 | 36 | $this->_executeConvertBindTests($connection); 37 | $this->_executeBindByTypeTests($connection); 38 | } 39 | 40 | public function testDbBindMysql() 41 | { 42 | return; 43 | require 'unit-tests/config.db.php'; 44 | if (empty($configMysql)) { 45 | $this->markTestSkipped("Skipped"); 46 | return; 47 | } 48 | 49 | $connection = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 50 | 51 | //$this->_executeRawBindTests($connection); 52 | //$this->_executeRawBindTestsMysql($connection); 53 | $this->_executeConvertBindTests($connection); 54 | $this->_executeBindByTypeTests($connection); 55 | } 56 | 57 | 58 | 59 | /* 60 | protected function _executeRawBindTests($connection) 61 | { 62 | 63 | $conditions = $connection->bindParams("a=?0", array(0 => 100)); 64 | $this->assertEquals($conditions, "a=100"); 65 | 66 | $conditions = $connection->bindParams("a=?0", array(0 => 100, 1 => 50)); 67 | $this->assertEquals($conditions, "a=100"); 68 | 69 | $conditions = $connection->bindParams("a=?0", array(1 => 50)); 70 | $this->assertEquals($conditions, "a=?0"); 71 | 72 | $conditions = $connection->bindParams("a=?1 AND b = ?0", array(0 => 25, 1 => 50)); 73 | $this->assertEquals($conditions, "a=50 AND b = 25"); 74 | 75 | $conditions = $connection->bindParams("a=?1 AND b = ?0", array(0 => '25', 1 => '50')); 76 | $this->assertEquals($conditions, "a=50 AND b = 25"); 77 | 78 | $conditions = $connection->bindParams("a=?1 AND b = ?0", array(0 => '25.10', 1 => 25.10)); 79 | $this->assertEquals($conditions, "a=25.1 AND b = 25.10"); 80 | 81 | $conditions = $connection->bindParams("a=?1 AND b = ?0 AND c<>?2", array(0 => 25, 1 => 50, 2 => 15)); 82 | $this->assertEquals($conditions, "a=50 AND b = 25 AND c<>15"); 83 | 84 | $conditions = $connection->bindParams("a=:a:", array('a' => 'no-suprises')); 85 | $this->assertEquals($conditions, "a='no-suprises'"); 86 | 87 | $conditions = $connection->bindParams("column1 = :column1: AND column2=:column2:", array('column1' => 'hello', 'column2' => 'lol')); 88 | $this->assertEquals($conditions, "column1 = 'hello' AND column2='lol'"); 89 | } 90 | 91 | protected function _executeRawBindTestsMysql($connection) 92 | { 93 | 94 | $conditions = $connection->bindParams("column3 IN (:val1:, :val2:, :val3:)", array('val1' => 'hello', 'val2' => 100, 'val3' => "'hahaha'")); 95 | $this->assertEquals($conditions, "column3 IN ('hello', 100, '\'hahaha\'')"); 96 | 97 | $conditions = $connection->bindParams("column3 IN (:val1:, :val2:, :val3:) AND column4 > ?2", array('val1' => 'hello', 'val2' => 100, 'val3' => "'hahaha'", 2 => 'le-nice')); 98 | $this->assertEquals($conditions, "column3 IN ('hello', 100, '\'hahaha\'') AND column4 > 'le-nice'"); 99 | } 100 | 101 | protected function _executeRawBindTestsPostgresql($connection) 102 | { 103 | $conditions = $connection->bindParams("column3 IN (:val1:, :val2:, :val3:)", array('val1' => 'hello', 'val2' => 100, 'val3' => "'hahaha'")); 104 | $this->assertEquals($conditions, "column3 IN ('hello', 100, '''hahaha''')"); 105 | 106 | $conditions = $connection->bindParams("column3 IN (:val1:, :val2:, :val3:) AND column4 > ?2", array('val1' => 'hello', 'val2' => 100, 'val3' => "'hahaha'", 2 => 'le-nice')); 107 | $this->assertEquals($conditions, "column3 IN ('hello', 100, '''hahaha''') AND column4 > 'le-nice'"); 108 | } 109 | 110 | protected function _executeRawBindTestsSqlite($connection) 111 | { 112 | $conditions = $connection->bindParams("column3 IN (:val1:, :val2:, :val3:)", array('val1' => 'hello', 'val2' => 100, 'val3' => "'hahaha'")); 113 | $this->assertEquals($conditions, "column3 IN ('hello', 100, '''hahaha''')"); 114 | 115 | $conditions = $connection->bindParams("column3 IN (:val1:, :val2:, :val3:) AND column4 > ?2", array('val1' => 'hello', 'val2' => 100, 'val3' => "'hahaha'", 2 => 'le-nice')); 116 | $this->assertEquals($conditions, "column3 IN ('hello', 100, '''hahaha''') AND column4 > 'le-nice'"); 117 | }*/ 118 | 119 | protected function _executeConvertBindTests($connection) 120 | { 121 | 122 | $params = $connection->convertBoundParams("a=?0", array(0 => 100)); 123 | $this->assertEquals($params, array('sql' => 'a=?', 'params' => array(0 => 100))); 124 | 125 | $params = $connection->convertBoundParams("a=?0", array(0 => 100, 1 => 50)); 126 | $this->assertEquals($params, array('sql' => 'a=?', 'params' => array(0 => 100))); 127 | 128 | $params = $connection->convertBoundParams("a=?1 AND b = ?0", array(1 => 50, 0 => 25)); 129 | $this->assertEquals($params, array('sql' => "a=? AND b = ?", 'params' => array(0 => 50, 1 => 25))); 130 | 131 | $params = $connection->convertBoundParams("a=?1 AND b = ?0", array(1 => 25.10, 0 => '25.10')); 132 | $this->assertEquals($params, array('sql' => "a=? AND b = ?", 'params' => array(0 => '25.10', 1 => 25.10))); 133 | 134 | $params = $connection->convertBoundParams("a=?1 AND b = ?0 AND c > :c: AND d = ?3", array('c' => 1000, 1 => 'some-name', 0 => 15, 3 => 400)); 135 | $this->assertEquals($params, array('sql' => "a=? AND b = ? AND c > ? AND d = ?", 'params' => array(0 => 'some-name', 1 => 15, 2 => 1000, 3 => 400))); 136 | } 137 | 138 | protected function _executeBindByTypeTests($connection) 139 | { 140 | 141 | $success = $connection->execute( 142 | 'INSERT INTO prueba( nombre, estado) VALUES ( ?, ?)', 143 | array("LOL 1", "A"), 144 | array(DbColumn::BIND_PARAM_STR, DbColumn::BIND_PARAM_STR) 145 | ); 146 | $this->assertTrue($success); 147 | 148 | $success = $connection->execute( 149 | 'UPDATE prueba SET nombre = ?, estado = ?', 150 | array("LOL 11", "R"), 151 | array(DbColumn::BIND_PARAM_STR, DbColumn::BIND_PARAM_STR) 152 | ); 153 | $this->assertTrue($success); 154 | 155 | $success = $connection->execute( 156 | 'DELETE FROM prueba WHERE estado = ?', 157 | array("R"), 158 | array(DbColumn::BIND_PARAM_STR) 159 | ); 160 | $this->assertTrue($success); 161 | 162 | /* ISUEE 163 | $success = $connection->insert( 164 | 'prueba', 165 | array($connection->getDefaultIdValue(), "LOL 1", "A"), 166 | null, 167 | array(DbColumn::BIND_SKIP, DbColumn::BIND_PARAM_STR, DbColumn::BIND_PARAM_STR) 168 | ); 169 | */ 170 | $success = $connection->insert( 171 | 'prueba', 172 | array( "LOL 1", "A"), 173 | null, 174 | array( DbColumn::BIND_PARAM_STR, DbColumn::BIND_PARAM_STR) 175 | ); 176 | 177 | $this->assertTrue($success); 178 | 179 | $success = $connection->insert( 180 | 'prueba', 181 | array("LOL 2", "E"), 182 | array('nombre', 'estado'), 183 | array(DbColumn::BIND_PARAM_STR, DbColumn::BIND_PARAM_STR) 184 | ); 185 | $this->assertTrue($success); 186 | 187 | $success = $connection->insert( 188 | 'prueba', 189 | array("LOL 3", "I"), 190 | array('nombre', 'estado'), 191 | array(DbColumn::BIND_PARAM_STR, DbColumn::BIND_PARAM_STR) 192 | ); 193 | $this->assertTrue($success); 194 | 195 | $success = $connection->insert( 196 | 'prueba', 197 | array(new Phalcon\Db\RawValue('GETDATE()'), "A"), 198 | array('nombre', 'estado'), 199 | array(DbColumn::BIND_PARAM_STR, DbColumn::BIND_PARAM_STR) 200 | ); 201 | $this->assertTrue($success); 202 | 203 | $success = $connection->update( 204 | 'prueba', 205 | array("nombre", "estado"), 206 | array("LOL 1000", "X"), 207 | "estado='E'", 208 | array(DbColumn::BIND_PARAM_STR, DbColumn::BIND_PARAM_STR) 209 | ); 210 | $this->assertTrue($success); 211 | 212 | $success = $connection->update( 213 | 'prueba', 214 | array("nombre"), array("LOL 3000"), 215 | "estado='X'", 216 | array(DbColumn::BIND_PARAM_STR) 217 | ); 218 | $this->assertTrue($success); 219 | 220 | $success = $connection->update( 221 | 'prueba', 222 | array("nombre"), array(new Phalcon\Db\RawValue('GETDATE()')), 223 | "estado='X'", 224 | array(DbColumn::BIND_PARAM_STR) 225 | ); 226 | $this->assertTrue($success); 227 | 228 | } 229 | 230 | } 231 | -------------------------------------------------------------------------------- /unit-tests/DbProfilerTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | class DbProfiler extends Phalcon\Db\Profiler 22 | { 23 | 24 | private $_points = 0; 25 | 26 | public function beforeStartProfile($profile) 27 | { 28 | $this->_points++; 29 | } 30 | 31 | public function afterEndProfile($profile) 32 | { 33 | $this->_points--; 34 | } 35 | 36 | public function getPoints() 37 | { 38 | return $this->_points; 39 | } 40 | 41 | } 42 | 43 | class DbProfilerListener 44 | { 45 | 46 | protected $_profiler; 47 | 48 | public function __construct(){ 49 | $this->_profiler = new DbProfiler(); 50 | } 51 | 52 | public function beforeQuery($event, $connection) 53 | { 54 | $this->_profiler->startProfile($connection->getSQLStatement()); 55 | } 56 | 57 | public function afterQuery($event, $connection) 58 | { 59 | $this->_profiler->stopProfile(); 60 | } 61 | 62 | public function getProfiler(){ 63 | return $this->_profiler; 64 | } 65 | 66 | } 67 | 68 | class DbProfilerTest extends PHPUnit_Framework_TestCase 69 | { 70 | public function testDbMssql() 71 | { 72 | include "loader.php"; 73 | require 'unit-tests/config.db.php'; 74 | if (empty($configMssql)) { 75 | $this->markTestSkipped("Skipped"); 76 | return; 77 | } 78 | $connection = new Twm\Db\Adapter\Pdo\Mssql($configMssql); 79 | 80 | $this->_executeTests($connection); 81 | } 82 | 83 | public function estDbMysql() 84 | { 85 | 86 | require 'unit-tests/config.db.php'; 87 | if (empty($configMysql)) { 88 | $this->markTestSkipped("Skipped"); 89 | return; 90 | } 91 | 92 | $connection = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 93 | 94 | $this->_executeTests($connection); 95 | } 96 | 97 | 98 | public function _executeTests($connection) 99 | { 100 | 101 | $eventsManager = new Phalcon\Events\Manager(); 102 | 103 | $listener = new DbProfilerListener(); 104 | 105 | $eventsManager->attach('db', $listener); 106 | 107 | $connection->setEventsManager($eventsManager); 108 | 109 | $connection->query("SELECT TOP 3 * FROM personas"); 110 | 111 | $profiler = $listener->getProfiler(); 112 | 113 | $this->assertEquals($profiler->getNumberTotalStatements(), 1); 114 | 115 | $profile = $profiler->getLastProfile(); 116 | $this->assertEquals(get_class($profile), 'Phalcon\Db\Profiler\Item'); 117 | 118 | $this->assertEquals("SELECT TOP 3 * FROM personas",$profile->getSQLStatement()); 119 | $this->assertEquals(gettype($profile->getInitialTime()), "double"); 120 | $this->assertEquals(gettype($profile->getFinalTime()), "double"); 121 | $this->assertEquals(gettype($profile->getTotalElapsedSeconds()), "double"); 122 | $this->assertTrue($profile->getFinalTime()>$profile->getInitialTime()); 123 | 124 | $connection->query("SELECT TOP 100 * FROM personas"); 125 | 126 | $this->assertEquals($profiler->getNumberTotalStatements(), 2); 127 | 128 | $profile = $profiler->getLastProfile(); 129 | $this->assertEquals(get_class($profile), 'Phalcon\Db\Profiler\Item'); 130 | 131 | $this->assertEquals($profile->getSQLStatement(), "SELECT TOP 100 * FROM personas"); 132 | $this->assertTrue($profile->getFinalTime() > $profile->getInitialTime()); 133 | 134 | $connection->query("SELECT TOP 5 * FROM personas"); 135 | $connection->query("SELECT TOP 10 * FROM personas"); 136 | $connection->query("SELECT TOP 15 * FROM personas"); 137 | 138 | $this->assertEquals(count($profiler->getProfiles()), 5); 139 | $this->assertEquals($profiler->getNumberTotalStatements(), 5); 140 | $this->assertEquals(gettype($profiler->getTotalElapsedSeconds()), "double"); 141 | $this->assertEquals($profiler->getPoints(), 0); 142 | 143 | $profiler->reset(); 144 | 145 | $this->assertEquals(count($profiler->getProfiles()), 0); 146 | $this->assertEquals($profiler->getNumberTotalStatements(), 0); 147 | } 148 | 149 | } 150 | -------------------------------------------------------------------------------- /unit-tests/DbTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | | Rack Lin | 19 | +------------------------------------------------------------------------+ 20 | */ 21 | 22 | include "loader.php"; 23 | class DbTest extends PHPUnit_Framework_TestCase 24 | { 25 | 26 | public function estDbMysql() 27 | { 28 | require 'config.db.php'; 29 | 30 | if (!empty($configMysql)) { 31 | $connection = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 32 | } else { 33 | $this->markTestSkipped("Skipped"); 34 | } 35 | } 36 | public function testDbMssql() 37 | { 38 | require 'config.db.php'; 39 | if (!empty($configMssql)) { 40 | $connection = new Twm\Db\Adapter\Pdo\Mssql($configMssql); 41 | $this->_executeTests($connection); 42 | } else { 43 | $this->markTestSkipped("Skipped"); 44 | } 45 | } 46 | function log($message){ 47 | $debug = true; 48 | if(!$debug) 49 | return; 50 | echo '

';
 51 | 		var_dump($message);
 52 | 		echo '
'; 53 | } 54 | 55 | protected function _executeTests($connection) 56 | { 57 | 58 | $result = $connection->query("SELECT TOP 3 * FROM personas "); 59 | 60 | $this->assertTrue(is_object($result)); 61 | $this->assertEquals(get_class($result), 'Phalcon\Db\Result\Pdo'); 62 | 63 | for ($i = 0; $i < 3; $i++) { 64 | $row = $result->fetch(); 65 | $this->assertEquals(count($row), 22); 66 | } 67 | $row = $result->fetch(); 68 | $this->assertEquals($row, false); 69 | $this->assertEquals($result->numRows(), 3); 70 | 71 | $number = 0; 72 | $result = $connection->query("SELECT TOP 5 * FROM personas"); 73 | $this->assertTrue(is_object($result)); 74 | 75 | while ($row = $result->fetch()) { 76 | $number++; 77 | } 78 | $this->assertEquals($number, 5); 79 | 80 | $result = $connection->query("SELECT TOP 5 * FROM personas"); 81 | $result->setFetchMode(Phalcon\Db::FETCH_NUM); 82 | $row = $result->fetch(); 83 | $this->assertTrue(is_array($row)); 84 | $this->assertEquals(count($row), 11); 85 | $this->assertTrue(isset($row[0])); 86 | $this->assertFalse(isset($row['cedula'])); 87 | $this->assertFalse(isset($row->cedula)); 88 | 89 | $result = $connection->query("SELECT TOP 5 * FROM personas"); 90 | $result->setFetchMode(Phalcon\Db::FETCH_ASSOC); 91 | $row = $result->fetch(); 92 | $this->assertTrue(is_array($row)); 93 | $this->assertEquals(count($row), 11); 94 | $this->assertFalse(isset($row[0])); 95 | $this->assertTrue(isset($row['cedula'])); 96 | $result = $connection->query("SELECT TOP 5 * FROM personas"); 97 | $result->setFetchMode(Phalcon\Db::FETCH_OBJ); 98 | $row = $result->fetch(); 99 | $this->assertTrue(is_object($row)); 100 | $this->assertTrue(isset($row->cedula)); 101 | 102 | $result = $connection->query("SELECT TOP 5 * FROM personas"); 103 | $result->setFetchMode(Phalcon\Db::FETCH_BOTH); 104 | $result->dataSeek(4); 105 | $row = $result->fetch(); 106 | $row = $result->fetch(); 107 | $this->assertEquals($row, false); 108 | 109 | $result = $connection->execute("DELETE FROM prueba"); 110 | $this->assertTrue($result); 111 | 112 | //$success = $connection->execute('INSERT INTO prueba(id, nombre, estado) VALUES ('.$connection->getDefaultIdValue().', ?, ?)', array("LOL 1", "A")); 113 | $success = $connection->execute('INSERT INTO prueba( nombre, estado) VALUES ( ?, ?)', array("LOL 1", "A")); 114 | $this->assertTrue($success); 115 | 116 | $success = $connection->execute('UPDATE prueba SET nombre = ?, estado = ?', array("LOL 11", "R")); 117 | $this->assertTrue($success); 118 | 119 | $success = $connection->execute('DELETE FROM prueba WHERE estado = ?', array("R")); 120 | $this->assertTrue($success); 121 | 122 | //$success = $connection->insert('prueba', array($connection->getDefaultIdValue(), "LOL 1", "A")); 123 | $success = $connection->insert('prueba', array("LOL 1", "A")); 124 | $this->assertTrue($success); 125 | 126 | $success = $connection->insert('prueba', array("LOL 2", "E"), array('nombre', 'estado')); 127 | $this->assertTrue($success); 128 | 129 | $success = $connection->insert('prueba', array("LOL 3", "I"), array('nombre', 'estado')); 130 | $this->assertTrue($success); 131 | 132 | $success = $connection->insert('prueba', array(new Phalcon\Db\RawValue('GETDATE()'), "A"), array('nombre', 'estado')); 133 | $this->assertTrue($success); 134 | 135 | for ($i=0; $i<50; $i++) { 136 | $success = $connection->insert('prueba', array("LOL ".$i, "F"), array('nombre', 'estado')); 137 | $this->assertTrue($success); 138 | } 139 | 140 | $success = $connection->update('prueba', array("nombre", "estado"), array("LOL 1000", "X"), "estado='F'"); 141 | $this->assertTrue($success); 142 | 143 | $success = $connection->update('prueba', array("nombre"), array("LOL 3000"), "estado='X'"); 144 | $this->assertTrue($success); 145 | 146 | $success = $connection->update('prueba', array("nombre"), array(new Phalcon\Db\RawValue('GETDATE()')), "estado='X'"); 147 | $this->assertTrue($success); 148 | 149 | $connection->delete("prueba", "estado='X'"); 150 | $this->assertTrue($success); 151 | 152 | $connection->delete("prueba"); 153 | $this->assertEquals(4, $connection->affectedRows()); 154 | 155 | $row = $connection->fetchOne("SELECT * FROM prueba"); 156 | $this->assertEquals(count($row), 1); 157 | 158 | $row = $connection->fetchOne("SELECT * FROM personas", Phalcon\Db::FETCH_NUM); 159 | $this->assertEquals(count($row), 11); 160 | 161 | $rows = $connection->fetchAll("SELECT top 10 * FROM personas "); 162 | $this->assertEquals(count($rows), 10); 163 | 164 | $rows = $connection->fetchAll("SELECT top 10 * FROM personas ", Phalcon\Db::FETCH_NUM); 165 | $this->assertEquals(count($rows), 10); 166 | $this->assertEquals(count($rows[0]), 11); 167 | 168 | //Auto-Increment/Serial Columns 169 | 170 | //$sql = 'INSERT INTO subscriptores(id, email, created_at, status) VALUES ('.$connection->getDefaultIdValue().', ?, ?, ?)'; 171 | $sql = 'INSERT INTO subscriptores( email, created_at, status) VALUES ( ?, ?, ?)'; 172 | $success = $connection->execute($sql, array('shirley@garbage.com', "2011-01-01 12:59:13", "P")); 173 | $this->assertTrue($success); 174 | 175 | //Check for auto-increment column 176 | $this->assertTrue($connection->lastInsertId('subscriptores_id_seq') > 0); 177 | 178 | 179 | // Create View 180 | $success = $connection->createView('phalcon_test_view', array('sql' => 'SELECT 1 AS one, 2 AS two, 3 AS three')); 181 | $this->assertTrue($success); 182 | 183 | //Check view exists 184 | $success = $connection->viewExists('phalcon_test_view'); 185 | $this->assertTrue((bool) $success); 186 | 187 | //Gets the list of all views. 188 | $views = $connection->listViews(); 189 | $this->assertTrue(is_array($views)); 190 | $this->assertTrue(in_array('phalcon_test_view', $views)); 191 | 192 | //Execute created view 193 | $row = $connection->fetchOne("SELECT * FROM phalcon_test_view"); 194 | $this->assertEquals(count($row), 6); 195 | $this->assertTrue(array_key_exists('one', $row)); 196 | $this->assertEquals($row['two'], 2); 197 | 198 | //Drop view 199 | $success = $connection->dropView('phalcon_test_view'); 200 | $this->assertTrue($success); 201 | 202 | //Transactions without savepoints. 203 | /* $connection->setNestedTransactionsWithSavepoints(false); 204 | 205 | $success = $connection->begin(); // level 1 - real 206 | $this->assertTrue($success); 207 | 208 | $success = $connection->begin(); // level 2 - virtual 209 | $this->assertFalse($success); 210 | 211 | $success = $connection->begin(); // level 3 - virtual 212 | $this->assertFalse($success); 213 | 214 | $success = $connection->rollback(); // level 2 - virtual 215 | $this->assertFalse($success); 216 | 217 | $success = $connection->commit(); // level 1 - virtual 218 | $this->assertFalse($success); 219 | 220 | $success = $connection->commit(); // commit - real 221 | $this->assertTrue($success); 222 | 223 | $success = $connection->begin(); // level 1 - real 224 | $this->assertTrue($success); 225 | 226 | $success = $connection->begin(); // level 2 - virtual 227 | $this->assertFalse($success); 228 | 229 | $success = $connection->commit(); // level 1 - virtual 230 | $this->assertFalse($success); 231 | 232 | $success = $connection->rollback(); // rollback - real 233 | $this->assertTrue($success); 234 | 235 | //Transactions with savepoints. 236 | $connection->setNestedTransactionsWithSavepoints(true); 237 | 238 | $success = $connection->begin(); // level 1 - begin transaction 239 | $this->assertTrue($success); 240 | 241 | $success = $connection->begin(); // level 2 - uses savepoint_1 242 | $this->assertTrue($success); 243 | 244 | $success = $connection->begin(); // level 3 - uses savepoint_2 245 | $this->assertTrue($success); 246 | 247 | $success = $connection->rollback(); // level 2 - uses rollback savepoint_2 248 | $this->assertTrue($success); 249 | 250 | $success = $connection->commit(); // level 1 - uses release savepoint_1 251 | $this->assertTrue($success); 252 | 253 | $success = $connection->commit(); // commit - real commit 254 | $this->assertTrue($success); 255 | 256 | $success = $connection->begin(); // level 1 - real begin transaction 257 | $this->assertTrue($success); 258 | 259 | $success = $connection->begin(); // level 2 - uses savepoint_1 260 | $this->assertTrue($success); 261 | 262 | $success = $connection->commit(); // level 1 - uses release savepoint_1 263 | $this->assertTrue($success); 264 | 265 | $success = $connection->rollback(); // rollback - real rollback 266 | $this->assertTrue($success); 267 | */ 268 | } 269 | 270 | } 271 | -------------------------------------------------------------------------------- /unit-tests/ModelsBehaviorsTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | require "loader.php"; 22 | class ModelsBehaviorsTest extends PHPUnit_Framework_TestCase 23 | { 24 | 25 | public function __construct() 26 | { 27 | spl_autoload_register(array($this, 'modelsAutoloader')); 28 | } 29 | 30 | public function __destruct() 31 | { 32 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 33 | } 34 | 35 | public function modelsAutoloader($className) 36 | { 37 | $className = str_replace('\\', '/', $className); 38 | if (file_exists('unit-tests/models/' . $className . '.php')) { 39 | require 'unit-tests/models/' . $className . '.php'; 40 | } 41 | } 42 | 43 | protected function _prepareDI() 44 | { 45 | Phalcon\DI::reset(); 46 | 47 | $di = new Phalcon\DI(); 48 | 49 | $di->set('modelsManager', function() { 50 | return new Phalcon\Mvc\Model\Manager(); 51 | }, true); 52 | 53 | $di->set('modelsMetadata', function() { 54 | return new Phalcon\Mvc\Model\Metadata\Memory(); 55 | }, true); 56 | 57 | $di->set('db', function() { 58 | require 'unit-tests/config.db.php'; 59 | return new Twm\Db\Adapter\Pdo\Mssql($configMssql); 60 | }, true); 61 | 62 | } 63 | 64 | public function testBehaviorsTimestampable() 65 | { 66 | 67 | $this->_prepareDI(); 68 | 69 | $subscriber = new News\Subscribers(); 70 | $subscriber->email = 'some@some.com'; 71 | $subscriber->status = 'I'; 72 | $this->assertTrue($subscriber->save()); 73 | $this->assertEquals(preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $subscriber->created_at), 1); 74 | } 75 | 76 | public function testBehaviorsSoftDelete() 77 | { 78 | 79 | $this->_prepareDI(); 80 | 81 | $number = News\Subscribers::count(); 82 | 83 | $subscriber = News\Subscribers::findFirst(); 84 | $this->assertTrue($subscriber->delete()); 85 | $this->assertEquals($subscriber->status, 'D'); 86 | $this->assertEquals(News\Subscribers::count(), $number); 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /unit-tests/ModelsCriteriaTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | require "loader.php"; 21 | class ModelsCriteriaTest extends PHPUnit_Framework_TestCase 22 | { 23 | 24 | public function __construct() 25 | { 26 | spl_autoload_register(array($this, 'modelsAutoloader')); 27 | } 28 | 29 | public function __destruct() 30 | { 31 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 32 | } 33 | 34 | public function modelsAutoloader($className) 35 | { 36 | if (file_exists('unit-tests/models/'.$className.'.php')) { 37 | require 'unit-tests/models/'.$className.'.php'; 38 | } 39 | } 40 | 41 | protected function _getDI() 42 | { 43 | 44 | Phalcon\DI::reset(); 45 | 46 | $di = new Phalcon\DI(); 47 | 48 | $di->set('modelsManager', function(){ 49 | return new Phalcon\Mvc\Model\Manager(); 50 | }); 51 | 52 | $di->set('modelsMetadata', function(){ 53 | return new Phalcon\Mvc\Model\Metadata\Memory(); 54 | }); 55 | 56 | return $di; 57 | } 58 | public function testModelsMssql() 59 | { 60 | require 'unit-tests/config.db.php'; 61 | if (empty($configMysql)) { 62 | $this->markTestSkipped("Skipped"); 63 | return; 64 | } 65 | 66 | $di = $this->_getDI(); 67 | 68 | $di->set('db', function(){ 69 | require 'unit-tests/config.db.php'; 70 | return new Twm\Db\Adapter\Pdo\Mssql($configMssql); 71 | }); 72 | 73 | $this->_executeTestsNormal($di); 74 | $this->_executeTestsRenamed($di); 75 | $this->_executeTestsFromInput($di); 76 | } 77 | /* 78 | public function testModelsMysql() 79 | { 80 | require 'unit-tests/config.db.php'; 81 | if (empty($configMysql)) { 82 | $this->markTestSkipped("Skipped"); 83 | return; 84 | } 85 | 86 | $di = $this->_getDI(); 87 | 88 | $di->set('db', function(){ 89 | require 'unit-tests/config.db.php'; 90 | return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 91 | }); 92 | 93 | $this->_executeTestsNormal($di); 94 | $this->_executeTestsRenamed($di); 95 | $this->_executeTestsFromInput($di); 96 | } 97 | 98 | public function testModelsPostgresql() 99 | { 100 | require 'unit-tests/config.db.php'; 101 | if (empty($configPostgresql)) { 102 | $this->markTestSkipped("Skipped"); 103 | return; 104 | } 105 | 106 | $di = $this->_getDI(); 107 | 108 | $di->set('db', function(){ 109 | require 'unit-tests/config.db.php'; 110 | return new Phalcon\Db\Adapter\Pdo\Postgresql($configPostgresql); 111 | }); 112 | 113 | $this->_executeTestsNormal($di); 114 | $this->_executeTestsRenamed($di); 115 | $this->_executeTestsFromInput($di); 116 | } 117 | 118 | public function testModelsSQLite() 119 | { 120 | require 'unit-tests/config.db.php'; 121 | if (empty($configSqlite)) { 122 | $this->markTestSkipped("Skipped"); 123 | return; 124 | } 125 | 126 | $di = $this->_getDI(); 127 | 128 | $di->set('db', function(){ 129 | require 'unit-tests/config.db.php'; 130 | return new Phalcon\Db\Adapter\Pdo\SQLite($configSqlite); 131 | }); 132 | 133 | $this->_executeTestsNormal($di); 134 | $this->_executeTestsRenamed($di); 135 | $this->_executeTestsFromInput($di); 136 | } 137 | */ 138 | protected function _executeTestsNormal($di) 139 | { 140 | $personas = Personas::query() 141 | ->where("estado=?1") 142 | ->bind(array(1 => "A")) 143 | ->order("nombres") 144 | ->limit(100, 10) 145 | ->execute(); 146 | 147 | return; 148 | $people = People::find(array( 149 | "estado=?1", 150 | "bind" => array(1 => "A"), 151 | "order" => "nombres", 152 | "limit" => array('number' => 100, 'offset' => 10) 153 | )); 154 | $this->assertEquals(count($personas), count($people)); 155 | return; 156 | 157 | $personas = Personas::query()->where("estado='I'")->execute(); 158 | $people = People::find("estado='I'"); 159 | $this->assertEquals(count($personas), count($people)); 160 | 161 | $personas = Personas::query()->conditions("estado='I'")->execute(); 162 | $people = People::find("estado='I'"); 163 | $this->assertEquals(count($personas), count($people)); 164 | 165 | $personas = Personas::query() 166 | ->where("estado='A'") 167 | ->order("nombres") 168 | ->execute(); 169 | $people = People::find(array( 170 | "estado='A'", 171 | "order" => "nombres" 172 | )); 173 | $this->assertEquals(count($personas), count($people)); 174 | 175 | $somePersona = $personas->getFirst(); 176 | $somePeople = $people->getFirst(); 177 | $this->assertEquals($somePersona->cedula, $somePeople->cedula); 178 | 179 | //Order + limit 180 | $personas = Personas::query() 181 | ->where("estado='A'") 182 | ->order("nombres") 183 | ->limit(100) 184 | ->execute(); 185 | $people = People::find(array( 186 | "estado='A'", 187 | "order" => "nombres", 188 | "limit" => 100 189 | )); 190 | $this->assertEquals(count($personas), count($people)); 191 | 192 | $somePersona = $personas->getFirst(); 193 | $somePeople = $people->getFirst(); 194 | $this->assertEquals($somePersona->cedula, $somePeople->cedula); 195 | 196 | //Bind params + Limit 197 | $personas = Personas::query() 198 | ->where("estado=?1") 199 | ->bind(array(1 => "A")) 200 | ->order("nombres") 201 | ->limit(100) 202 | ->execute(); 203 | 204 | $people = People::find(array( 205 | "estado=?1", 206 | "bind" => array(1 => "A"), 207 | "order" => "nombres", 208 | "limit" => 100 209 | )); 210 | $this->assertEquals(count($personas), count($people)); 211 | 212 | $somePersona = $personas->getFirst(); 213 | $somePeople = $people->getFirst(); 214 | $this->assertEquals($somePersona->cedula, $somePeople->cedula); 215 | 216 | //Limit + Offset 217 | $personas = Personas::query() 218 | ->where("estado=?1") 219 | ->bind(array(1 => "A")) 220 | ->order("nombres") 221 | ->limit(100, 10) 222 | ->execute(); 223 | 224 | $people = People::find(array( 225 | "estado=?1", 226 | "bind" => array(1 => "A"), 227 | "order" => "nombres", 228 | "limit" => array('number' => 100, 'offset' => 10) 229 | )); 230 | $this->assertEquals(count($personas), count($people)); 231 | 232 | $somePersona = $personas->getFirst(); 233 | $somePeople = $people->getFirst(); 234 | $this->assertEquals($somePersona->cedula, $somePeople->cedula); 235 | 236 | $personas = Personas::query() 237 | ->where("estado=:estado:") 238 | ->bind(array("estado" => "A")) 239 | ->order("nombres") 240 | ->limit(100) 241 | ->execute(); 242 | 243 | $people = People::find(array( 244 | "estado=:estado:", 245 | "bind" => array("estado" => "A"), 246 | "order" => "nombres", 247 | "limit" => 100 248 | )); 249 | $this->assertEquals(count($personas), count($people)); 250 | 251 | $somePersona = $personas->getFirst(); 252 | $somePeople = $people->getFirst(); 253 | $this->assertEquals($somePersona->cedula, $somePeople->cedula); 254 | 255 | } 256 | 257 | protected function _executeTestsRenamed($di) 258 | { 259 | 260 | $personers = Personers::query() 261 | ->where("status='I'") 262 | ->execute(); 263 | $this->assertTrue(is_object($personers)); 264 | $this->assertEquals(get_class($personers), 'Phalcon\Mvc\Model\Resultset\Simple'); 265 | 266 | $personers = Personers::query() 267 | ->conditions("status='I'") 268 | ->execute(); 269 | $this->assertTrue(is_object($personers)); 270 | $this->assertEquals(get_class($personers), 'Phalcon\Mvc\Model\Resultset\Simple'); 271 | 272 | $personers = Personers::query() 273 | ->where("status='A'") 274 | ->order("navnes") 275 | ->execute(); 276 | $this->assertTrue(is_object($personers)); 277 | $this->assertEquals(get_class($personers), 'Phalcon\Mvc\Model\Resultset\Simple'); 278 | 279 | $somePersoner = $personers->getFirst(); 280 | $this->assertTrue(is_object($somePersoner)); 281 | $this->assertEquals(get_class($somePersoner), 'Personers'); 282 | 283 | $personers = Personers::query() 284 | ->where("status='A'") 285 | ->order("navnes") 286 | ->limit(100) 287 | ->execute(); 288 | $this->assertTrue(is_object($personers)); 289 | $this->assertEquals(get_class($personers), 'Phalcon\Mvc\Model\Resultset\Simple'); 290 | 291 | $somePersoner = $personers->getFirst(); 292 | $this->assertTrue(is_object($somePersoner)); 293 | $this->assertEquals(get_class($somePersoner), 'Personers'); 294 | 295 | $personers = Personers::query() 296 | ->where("status=?1") 297 | ->bind(array(1 => "A")) 298 | ->order("navnes") 299 | ->limit(100) 300 | ->execute(); 301 | $this->assertTrue(is_object($personers)); 302 | $this->assertEquals(get_class($personers), 'Phalcon\Mvc\Model\Resultset\Simple'); 303 | 304 | $somePersoner = $personers->getFirst(); 305 | $this->assertTrue(is_object($somePersoner)); 306 | $this->assertEquals(get_class($somePersoner), 'Personers'); 307 | 308 | $personers = Personers::query() 309 | ->where("status=:status:") 310 | ->bind(array("status" => "A")) 311 | ->order("navnes") 312 | ->limit(100)->execute(); 313 | $this->assertTrue(is_object($personers)); 314 | $this->assertEquals(get_class($personers), 'Phalcon\Mvc\Model\Resultset\Simple'); 315 | 316 | $somePersoner = $personers->getFirst(); 317 | $this->assertTrue(is_object($somePersoner)); 318 | $this->assertEquals(get_class($somePersoner), 'Personers'); 319 | } 320 | 321 | protected function _executeTestsFromInput($di) 322 | { 323 | 324 | $data = array(); 325 | $criteria = \Phalcon\Mvc\Model\Criteria::fromInput($di, "Robots", $data); 326 | $this->assertEquals($criteria->getParams(), NULL); 327 | $this->assertEquals($criteria->getModelName(), "Robots"); 328 | 329 | $data = array('id' => 1); 330 | $criteria = \Phalcon\Mvc\Model\Criteria::fromInput($di, "Robots", $data); 331 | $this->assertEquals($criteria->getParams(), array( 332 | 'conditions' => 'id=:id:', 333 | 'bind' => array( 334 | 'id' => 1, 335 | ), 336 | )); 337 | 338 | $data = array('name' => 'ol'); 339 | $criteria = \Phalcon\Mvc\Model\Criteria::fromInput($di, "Robots", $data); 340 | $this->assertEquals($criteria->getParams(), array( 341 | 'conditions' => 'name LIKE :name:', 342 | 'bind' => array( 343 | 'name' => '%ol%', 344 | ), 345 | )); 346 | 347 | $data = array('id' => 1, 'name' => 'ol'); 348 | $criteria = \Phalcon\Mvc\Model\Criteria::fromInput($di, "Robots", $data); 349 | $this->assertEquals($criteria->getParams(), array( 350 | 'conditions' => 'id=:id: AND name LIKE :name:', 351 | 'bind' => array( 352 | 'id' => 1, 353 | 'name' => '%ol%', 354 | ) 355 | )); 356 | 357 | $data = array('id' => 1, 'name' => 'ol', 'other' => true); 358 | $criteria = \Phalcon\Mvc\Model\Criteria::fromInput($di, "Robots", $data); 359 | $this->assertEquals($criteria->getParams(), array( 360 | 'conditions' => 'id=:id: AND name LIKE :name:', 361 | 'bind' => array( 362 | 'id' => 1, 363 | 'name' => '%ol%', 364 | ) 365 | )); 366 | } 367 | 368 | } 369 | -------------------------------------------------------------------------------- /unit-tests/ModelsDynamicOperationsTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | require "loader.php"; 22 | class ModelsDynamicOperationsTest extends PHPUnit_Framework_TestCase 23 | { 24 | 25 | public function __construct() 26 | { 27 | spl_autoload_register(array($this, 'modelsAutoloader')); 28 | } 29 | 30 | public function __destruct() 31 | { 32 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 33 | } 34 | 35 | public function modelsAutoloader($className) 36 | { 37 | $className = str_replace('\\', '/', $className); 38 | if (file_exists('unit-tests/models/' . $className . '.php')) { 39 | require 'unit-tests/models/' . $className . '.php'; 40 | } 41 | } 42 | 43 | protected function _getDI() 44 | { 45 | 46 | Phalcon\DI::reset(); 47 | 48 | $di = new Phalcon\DI(); 49 | 50 | $di->set('modelsManager', function(){ 51 | return new Phalcon\Mvc\Model\Manager(); 52 | }, true); 53 | 54 | $di->set('modelsMetadata', function(){ 55 | return new Phalcon\Mvc\Model\Metadata\Memory(); 56 | }, true); 57 | 58 | return $di; 59 | } 60 | 61 | public function testModelsMysql() 62 | { 63 | 64 | $di = $this->_getDI(); 65 | 66 | $tracer = array(); 67 | 68 | $di->set('db', function() use (&$tracer) { 69 | 70 | require 'unit-tests/config.db.php'; 71 | 72 | $eventsManager = new Phalcon\Events\Manager(); 73 | 74 | $connection = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 75 | 76 | $eventsManager->attach('db', function($event, $connection) use (&$tracer) { 77 | if ($event->getType() == 'beforeQuery') { 78 | $tracer[] = $connection->getSqlStatement(); 79 | } 80 | }); 81 | 82 | $connection->setEventsManager($eventsManager); 83 | 84 | return $connection; 85 | }); 86 | 87 | $this->_executeTestsNormal($di, $tracer); 88 | 89 | $tracer = array(); 90 | $this->_executeTestsRenamed($di, $tracer); 91 | } 92 | 93 | protected function _executeTestsNormal($di, &$tracer) 94 | { 95 | 96 | $persona = Dynamic\Personas::findFirst(); 97 | $this->assertTrue($persona->save()); 98 | 99 | $this->assertEquals(count($tracer), 3); 100 | 101 | $persona->nombres = 'Other Name '.mt_rand(0, 150000); 102 | $this->assertTrue($persona->save()); 103 | 104 | $this->assertEquals('UPDATE `personas` SET `nombres` = ? WHERE `cedula` = ?', $tracer[3]); 105 | $this->assertEquals($persona->getChangedFields(), array('nombres')); 106 | 107 | $persona->nombres = 'Other Name '.mt_rand(0, 150000); 108 | $persona->direccion = 'Address '.mt_rand(0, 150000); 109 | $this->assertTrue($persona->save()); 110 | 111 | $this->assertEquals('UPDATE `personas` SET `nombres` = ?, `direccion` = ? WHERE `cedula` = ?', $tracer[4]); 112 | $this->assertEquals($persona->getChangedFields(), array('nombres', 'direccion')); 113 | } 114 | 115 | protected function _executeTestsRenamed($di, &$tracer) 116 | { 117 | $personer = Dynamic\Personers::findFirst(); 118 | $this->assertTrue($personer->save()); 119 | 120 | $this->assertEquals(count($tracer), 3); 121 | 122 | $personer->navnes = 'Other Name '.mt_rand(0, 150000); 123 | $this->assertTrue($personer->save()); 124 | 125 | $this->assertEquals('UPDATE `personas` SET `nombres` = ? WHERE `cedula` = ?', $tracer[3]); 126 | $this->assertEquals($personer->getChangedFields(), array('navnes')); 127 | 128 | $personer->navnes = 'Other Name '.mt_rand(0, 150000); 129 | $personer->adresse = 'Address '.mt_rand(0, 150000); 130 | $this->assertTrue($personer->save()); 131 | 132 | $this->assertEquals('UPDATE `personas` SET `nombres` = ?, `direccion` = ? WHERE `cedula` = ?', $tracer[4]); 133 | $this->assertEquals($personer->getChangedFields(), array('navnes', 'adresse')); 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /unit-tests/ModelsEventsTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | include "loader.php"; 22 | class ModelsEventsTest extends PHPUnit_Framework_TestCase 23 | { 24 | 25 | public function __construct() 26 | { 27 | spl_autoload_register(array($this, 'modelsAutoloader')); 28 | } 29 | 30 | public function __destruct() 31 | { 32 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 33 | } 34 | 35 | public function modelsAutoloader($className) 36 | { 37 | if (file_exists('unit-tests/models/'.$className.'.php')) { 38 | require 'unit-tests/models/'.$className.'.php'; 39 | } 40 | } 41 | 42 | protected function _prepareDI(&$trace) 43 | { 44 | Phalcon\DI::reset(); 45 | 46 | $eventsManager = new Phalcon\Events\Manager(); 47 | 48 | $eventsManager->attach('model', function($event, $model) use (&$trace) { 49 | if (!isset($trace[$event->getType()][get_class($model)])) { 50 | $trace[$event->getType()][get_class($model)] = 1; 51 | } else { 52 | $trace[$event->getType()][get_class($model)]++; 53 | } 54 | }); 55 | 56 | $di = new Phalcon\DI(); 57 | 58 | $di->set('modelsManager', function() use ($eventsManager) { 59 | 60 | $modelsManager = new Phalcon\Mvc\Model\Manager(); 61 | 62 | $modelsManager->setEventsManager($eventsManager); 63 | 64 | return $modelsManager; 65 | }, true); 66 | 67 | $di->set('modelsMetadata', function(){ 68 | return new Phalcon\Mvc\Model\Metadata\Memory(); 69 | }, true); 70 | 71 | $di->set('db', function(){ 72 | require 'unit-tests/config.db.php'; 73 | //return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 74 | return new Twm\Db\Adapter\Pdo\Mssql($configMssql); 75 | }, true); 76 | } 77 | 78 | public function testEventsCreate() 79 | { 80 | 81 | $trace = array(); 82 | 83 | $this->_prepareDI($trace); 84 | 85 | $robot = new GossipRobots(); 86 | 87 | $robot->name = 'Test'; 88 | $robot->year = 2000; 89 | $robot->type = 'Some Type'; 90 | 91 | $robot->trace = &$trace; 92 | 93 | $robot->save(); 94 | 95 | $this->assertEquals($trace, array( 96 | 'beforeValidation' => array( 97 | 'GossipRobots' => 2, 98 | ), 99 | 'beforeValidationOnCreate' => array( 100 | 'GossipRobots' => 1, 101 | ), 102 | 'validation' => array( 103 | 'GossipRobots' => 2, 104 | ), 105 | 'afterValidationOnCreate' => array( 106 | 'GossipRobots' => 1, 107 | ), 108 | 'afterValidation' => array( 109 | 'GossipRobots' => 2, 110 | ), 111 | 'beforeSave' => array( 112 | 'GossipRobots' => 2, 113 | ), 114 | 'beforeCreate' => array( 115 | 'GossipRobots' => 1, 116 | ) 117 | )); 118 | 119 | } 120 | 121 | public function testEventsUpdate() 122 | { 123 | 124 | $trace = array(); 125 | 126 | $this->_prepareDI($trace); 127 | 128 | $robot = GossipRobots::findFirst(); 129 | 130 | $robot->trace = &$trace; 131 | 132 | $robot->save(); 133 | 134 | $this->assertEquals($trace, array( 135 | 'beforeValidation' => array( 136 | 'GossipRobots' => 2, 137 | ), 138 | 'beforeValidationOnUpdate' => array( 139 | 'GossipRobots' => 2, 140 | ), 141 | 'validation' => array( 142 | 'GossipRobots' => 2, 143 | ), 144 | 'afterValidationOnUpdate' => array( 145 | 'GossipRobots' => 2, 146 | ), 147 | 'afterValidation' => array( 148 | 'GossipRobots' => 2, 149 | ), 150 | 'beforeSave' => array( 151 | 'GossipRobots' => 2, 152 | ), 153 | 'beforeUpdate' => array( 154 | 'GossipRobots' => 2, 155 | ), 156 | 'afterUpdate' => array( 157 | 'GossipRobots' => 2, 158 | ), 159 | 'afterSave' => array( 160 | 'GossipRobots' => 2, 161 | ), 162 | )); 163 | 164 | } 165 | 166 | public function testEventsDelete() 167 | { 168 | 169 | $trace = array(); 170 | 171 | $this->_prepareDI($trace); 172 | 173 | $robot = GossipRobots::findFirst(); 174 | 175 | $robot->trace = &$trace; 176 | 177 | $robot->delete(); 178 | 179 | $this->assertEquals($trace, array( 180 | 'beforeDelete' => array( 181 | 'GossipRobots' => 1, 182 | ) 183 | )); 184 | 185 | } 186 | 187 | } 188 | -------------------------------------------------------------------------------- /unit-tests/ModelsFindersTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | require "loader.php"; 22 | class ModelsFindersTest extends PHPUnit_Framework_TestCase 23 | { 24 | 25 | public function __construct() 26 | { 27 | spl_autoload_register(array($this, 'modelsAutoloader')); 28 | } 29 | 30 | public function __destruct() 31 | { 32 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 33 | } 34 | 35 | public function modelsAutoloader($className) 36 | { 37 | if (file_exists('unit-tests/models/'.$className.'.php')) { 38 | require 'unit-tests/models/'.$className.'.php'; 39 | } 40 | } 41 | 42 | protected function _getDI() 43 | { 44 | 45 | Phalcon\DI::reset(); 46 | 47 | $di = new Phalcon\DI(); 48 | 49 | $di->set('modelsManager', function(){ 50 | return new Phalcon\Mvc\Model\Manager(); 51 | }, true); 52 | 53 | $di->set('modelsMetadata', function(){ 54 | return new Phalcon\Mvc\Model\Metadata\Memory(); 55 | }, true); 56 | 57 | return $di; 58 | } 59 | 60 | public function testModels() 61 | { 62 | 63 | $di = $this->_getDI(); 64 | 65 | $di->set('db', function(){ 66 | require 'unit-tests/config.db.php'; 67 | return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 68 | }); 69 | 70 | $this->_executeTestsNormal($di); 71 | $this->_executeTestsRenamed($di); 72 | } 73 | 74 | protected function _executeTestsNormal($di) 75 | { 76 | 77 | $robot = Robots::findFirstById(1); 78 | $this->assertEquals(get_class($robot), 'Robots'); 79 | $this->assertEquals($robot->id, 1); 80 | 81 | $robot = Robots::findFirstById(2); 82 | $this->assertEquals(get_class($robot), 'Robots'); 83 | $this->assertEquals($robot->id, 2); 84 | 85 | $robots = Robots::findByType('mechanical'); 86 | $this->assertEquals(count($robots), 2); 87 | $this->assertEquals($robots[0]->id, 1); 88 | 89 | $number = Robots::countByType('mechanical'); 90 | $this->assertEquals($number, 2); 91 | } 92 | 93 | protected function _executeTestsRenamed($di) 94 | { 95 | 96 | $robot = Robotters::findFirstByCode(1); 97 | $this->assertEquals(get_class($robot), 'Robotters'); 98 | $this->assertEquals($robot->code, 1); 99 | 100 | $robot = Robotters::findFirstByCode(2); 101 | $this->assertEquals(get_class($robot), 'Robotters'); 102 | $this->assertEquals($robot->code, 2); 103 | 104 | $robots = Robotters::findByTheType('mechanical'); 105 | $this->assertEquals(count($robots), 2); 106 | $this->assertEquals($robots[0]->code, 1); 107 | 108 | $number = Robotters::countByTheType('mechanical'); 109 | $this->assertEquals($number, 2); 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /unit-tests/ModelsForeignKeysTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | class ModelsForeignKeysTest extends PHPUnit_Framework_TestCase 22 | { 23 | 24 | public function __construct() 25 | { 26 | spl_autoload_register(array($this, 'modelsAutoloader')); 27 | } 28 | 29 | public function __destruct() 30 | { 31 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 32 | } 33 | 34 | public function modelsAutoloader($className) 35 | { 36 | if (file_exists('unit-tests/models/'.$className.'.php')) { 37 | require 'unit-tests/models/'.$className.'.php'; 38 | } 39 | } 40 | 41 | protected function _getDI() 42 | { 43 | 44 | Phalcon\DI::reset(); 45 | 46 | $di = new Phalcon\DI(); 47 | 48 | $di->set('modelsManager', function(){ 49 | return new Phalcon\Mvc\Model\Manager(); 50 | }, true); 51 | 52 | $di->set('modelsMetadata', function(){ 53 | return new Phalcon\Mvc\Model\Metadata\Memory(); 54 | }, true); 55 | 56 | return $di; 57 | } 58 | 59 | public function testForeignKeysMysql() 60 | { 61 | require 'unit-tests/config.db.php'; 62 | if (empty($configMysql)) { 63 | $this->markTestSkipped("Skipped"); 64 | return; 65 | } 66 | 67 | $di = $this->_getDI(); 68 | 69 | $di->set('db', function(){ 70 | require 'unit-tests/config.db.php'; 71 | return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 72 | }, true); 73 | 74 | $this->_executeTestsNormal($di); 75 | $this->_executeTestsRenamed($di); 76 | } 77 | 78 | public function testForeignKeysPostgresql() 79 | { 80 | require 'unit-tests/config.db.php'; 81 | if (empty($configPostgresql)) { 82 | $this->markTestSkipped("Skipped"); 83 | return; 84 | } 85 | 86 | $di = $this->_getDI(); 87 | 88 | $di->set('db', function(){ 89 | require 'unit-tests/config.db.php'; 90 | return new Phalcon\Db\Adapter\Pdo\Postgresql($configPostgresql); 91 | }, true); 92 | 93 | $this->_executeTestsNormal($di); 94 | $this->_executeTestsRenamed($di); 95 | } 96 | 97 | public function testForeignKeysSqlite() 98 | { 99 | require 'unit-tests/config.db.php'; 100 | if (empty($configSqlite)) { 101 | $this->markTestSkipped("Skipped"); 102 | return; 103 | } 104 | 105 | $di = $this->_getDI(); 106 | 107 | $di->set('db', function(){ 108 | require 'unit-tests/config.db.php'; 109 | return new Phalcon\Db\Adapter\Pdo\Sqlite($configSqlite); 110 | }, true); 111 | 112 | $this->_executeTestsNormal($di); 113 | $this->_executeTestsRenamed($di); 114 | } 115 | 116 | public function _executeTestsNormal($di) 117 | { 118 | 119 | //Normal foreign keys 120 | $robotsParts = new RobotsParts(); 121 | $robotsParts->robots_id = 1; 122 | $robotsParts->parts_id = 100; 123 | 124 | $this->assertFalse($robotsParts->save()); 125 | 126 | $messages = array( 127 | 0 => Phalcon\Mvc\Model\Message::__set_state(array( 128 | '_type' => 'ConstraintViolation', 129 | '_message' => 'Value of field "parts_id" does not exist on referenced table', 130 | '_field' => 'parts_id', 131 | )) 132 | ); 133 | 134 | $this->assertEquals($robotsParts->getMessages(), $messages); 135 | 136 | $robotsParts->robots_id = 100; 137 | $robotsParts->parts_id = 1; 138 | $this->assertFalse($robotsParts->save()); 139 | 140 | $messages = array( 141 | 0 => Phalcon\Mvc\Model\Message::__set_state(array( 142 | '_type' => 'ConstraintViolation', 143 | '_message' => 'The robot code does not exist', 144 | '_field' => 'robots_id', 145 | )) 146 | ); 147 | 148 | $this->assertEquals($robotsParts->getMessages(), $messages); 149 | 150 | //Reverse foreign keys 151 | 152 | $robot = Robots::findFirst(); 153 | $this->assertNotEquals($robot, false); 154 | 155 | $this->assertFalse($robot->delete()); 156 | 157 | $messages = array( 158 | 0 => Phalcon\Mvc\Model\Message::__set_state(array( 159 | '_type' => 'ConstraintViolation', 160 | '_message' => 'Record is referenced by model RobotsParts', 161 | '_field' => 'id', 162 | )) 163 | ); 164 | 165 | $this->assertEquals($robot->getMessages(), $messages); 166 | 167 | $part = Parts::findFirst(); 168 | $this->assertNotEquals($part, false); 169 | 170 | $this->assertFalse($part->delete()); 171 | 172 | $messages = array( 173 | 0 => Phalcon\Mvc\Model\Message::__set_state(array( 174 | '_type' => 'ConstraintViolation', 175 | '_message' => 'Parts cannot be deleted because is referenced by a Robot', 176 | '_field' => 'id', 177 | )) 178 | ); 179 | 180 | $this->assertEquals($part->getMessages(), $messages); 181 | 182 | } 183 | 184 | public function _executeTestsRenamed($di) 185 | { 186 | 187 | //Normal foreign keys with column renaming 188 | $robottersDeles = new RobottersDeles(); 189 | $robottersDeles->robottersCode = 1; 190 | $robottersDeles->delesCode = 100; 191 | $this->assertFalse($robottersDeles->save()); 192 | 193 | $messages = array( 194 | 0 => Phalcon\Mvc\Model\Message::__set_state(array( 195 | '_type' => 'ConstraintViolation', 196 | '_message' => 'Value of field "delesCode" does not exist on referenced table', 197 | '_field' => 'delesCode', 198 | )) 199 | ); 200 | 201 | $this->assertEquals($robottersDeles->getMessages(), $messages); 202 | 203 | $robottersDeles->robottersCode = 100; 204 | $robottersDeles->delesCode = 1; 205 | $this->assertFalse($robottersDeles->save()); 206 | 207 | $messages = array( 208 | 0 => Phalcon\Mvc\Model\Message::__set_state(array( 209 | '_type' => 'ConstraintViolation', 210 | '_message' => 'The robotters code does not exist', 211 | '_field' => 'robottersCode', 212 | )) 213 | ); 214 | 215 | $this->assertEquals($robottersDeles->getMessages(), $messages); 216 | 217 | //Reverse foreign keys with renaming 218 | $robotter = Robotters::findFirst(); 219 | $this->assertNotEquals($robotter, false); 220 | 221 | $this->assertFalse($robotter->delete()); 222 | 223 | $messages = array( 224 | 0 => Phalcon\Mvc\Model\Message::__set_state(array( 225 | '_type' => 'ConstraintViolation', 226 | '_message' => 'Record is referenced by model RobottersDeles', 227 | '_field' => 'code', 228 | )) 229 | ); 230 | 231 | $this->assertEquals($robotter->getMessages(), $messages); 232 | 233 | $dele = Deles::findFirst(); 234 | $this->assertNotEquals($dele, false); 235 | 236 | $this->assertFalse($dele->delete()); 237 | 238 | $messages = array( 239 | 0 => Phalcon\Mvc\Model\Message::__set_state(array( 240 | '_type' => 'ConstraintViolation', 241 | '_message' => 'Deles cannot be deleted because is referenced by a Robotter', 242 | '_field' => 'code', 243 | )) 244 | ); 245 | 246 | $this->assertEquals($dele->getMessages(), $messages); 247 | 248 | } 249 | 250 | } 251 | -------------------------------------------------------------------------------- /unit-tests/ModelsHydrationTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | require "loader.php"; 22 | class ModelsHydrationTest extends PHPUnit_Framework_TestCase 23 | { 24 | 25 | public function __construct() 26 | { 27 | spl_autoload_register(array($this, 'modelsAutoloader')); 28 | } 29 | 30 | public function __destruct() 31 | { 32 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 33 | } 34 | 35 | public function modelsAutoloader($className) 36 | { 37 | if (file_exists('unit-tests/models/'.$className.'.php')) { 38 | require 'unit-tests/models/'.$className.'.php'; 39 | } 40 | } 41 | 42 | protected function _getDI() 43 | { 44 | 45 | Phalcon\DI::reset(); 46 | 47 | $di = new Phalcon\DI(); 48 | 49 | $di->set('modelsManager', function(){ 50 | return new Phalcon\Mvc\Model\Manager(); 51 | }, true); 52 | 53 | $di->set('modelsMetadata', function(){ 54 | return new Phalcon\Mvc\Model\Metadata\Memory(); 55 | }, true); 56 | 57 | return $di; 58 | } 59 | 60 | public function testModelsMssql() 61 | { 62 | require 'unit-tests/config.db.php'; 63 | if (empty($configMssql)) { 64 | $this->markTestSkipped("Skipped"); 65 | return; 66 | } 67 | 68 | $di = $this->_getDI(); 69 | 70 | $di->set('db', function(){ 71 | require 'unit-tests/config.db.php'; 72 | return new Twm\Db\Adapter\Pdo\Mssql($configMssql); 73 | }); 74 | 75 | $this->_executeTestsNormal($di); 76 | $this->_executeTestsRenamed($di); 77 | $this->_executeTestsNormalComplex($di); 78 | } 79 | /* 80 | public function testModelsPostgresql() 81 | { 82 | require 'unit-tests/config.db.php'; 83 | if (empty($configPostgresql)) { 84 | $this->markTestSkipped("Skipped"); 85 | return; 86 | } 87 | 88 | $di = $this->_getDI(); 89 | 90 | $di->set('db', function(){ 91 | require 'unit-tests/config.db.php'; 92 | return new Phalcon\Db\Adapter\Pdo\Postgresql($configPostgresql); 93 | }); 94 | 95 | $this->_executeTestsNormal($di); 96 | $this->_executeTestsRenamed($di); 97 | $this->_executeTestsNormalComplex($di); 98 | } 99 | 100 | public function testModelsSQLite() 101 | { 102 | require 'unit-tests/config.db.php'; 103 | if (empty($configSqlite)) { 104 | $this->markTestSkipped("Skipped"); 105 | return; 106 | } 107 | 108 | $di = $this->_getDI(); 109 | 110 | $di->set('db', function(){ 111 | require 'unit-tests/config.db.php'; 112 | return new Phalcon\Db\Adapter\Pdo\SQLite($configSqlite); 113 | }); 114 | 115 | $this->_executeTestsNormal($di); 116 | $this->_executeTestsRenamed($di); 117 | $this->_executeTestsNormalComplex($di); 118 | } 119 | */ 120 | protected function _executeTestsNormal($di) 121 | { 122 | 123 | $number = 0; 124 | 125 | $robots = Robots::find(); 126 | 127 | foreach ($robots as $robot) { 128 | $this->assertTrue(is_object($robot)); 129 | $this->assertEquals(get_class($robot), 'Robots'); 130 | $number++; 131 | } 132 | 133 | $robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS); 134 | foreach ($robots as $robot) { 135 | $this->assertTrue(is_object($robot)); 136 | $this->assertEquals(get_class($robot), 'Robots'); 137 | $number++; 138 | } 139 | 140 | $robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS); 141 | foreach ($robots as $robot) { 142 | $this->assertTrue(is_array($robot)); 143 | $this->assertEquals(count($robot), 4); 144 | $number++; 145 | } 146 | 147 | $robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS); 148 | foreach ($robots as $robot) { 149 | $this->assertTrue(is_object($robot)); 150 | $this->assertEquals(get_class($robot), 'stdClass'); 151 | $number++; 152 | } 153 | 154 | $this->assertEquals($number, 12); 155 | 156 | $number = 0; 157 | 158 | $people = People::find(array('limit' => 33)); 159 | 160 | foreach ($people as $person) { 161 | $this->assertTrue(is_object($person)); 162 | $this->assertEquals(get_class($person), 'People'); 163 | $number++; 164 | } 165 | 166 | $people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS); 167 | foreach ($people as $person) { 168 | $this->assertTrue(is_object($person)); 169 | $this->assertEquals(get_class($person), 'People'); 170 | $number++; 171 | } 172 | 173 | $people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS); 174 | foreach ($people as $person) { 175 | $this->assertTrue(is_array($person)); 176 | $number++; 177 | } 178 | 179 | $people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS); 180 | foreach ($people as $person) { 181 | $this->assertTrue(is_object($person)); 182 | $this->assertEquals(get_class($person), 'stdClass'); 183 | $number++; 184 | } 185 | 186 | $this->assertEquals($number, 33 * 4); 187 | 188 | } 189 | 190 | protected function _executeTestsRenamed($di) 191 | { 192 | 193 | $number = 0; 194 | 195 | $robots = Robotters::find(); 196 | 197 | foreach ($robots as $robot) { 198 | $this->assertTrue(is_object($robot)); 199 | $this->assertEquals(get_class($robot), 'Robotters'); 200 | $number++; 201 | } 202 | 203 | $robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS); 204 | foreach ($robots as $robot) { 205 | $this->assertTrue(is_object($robot)); 206 | $this->assertEquals(get_class($robot), 'Robotters'); 207 | $number++; 208 | } 209 | 210 | $robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS); 211 | foreach ($robots as $robot) { 212 | $this->assertTrue(is_array($robot)); 213 | $this->assertEquals(count($robot), 4); 214 | $number++; 215 | } 216 | 217 | $robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS); 218 | foreach ($robots as $robot) { 219 | $this->assertTrue(is_object($robot)); 220 | $this->assertEquals(get_class($robot), 'stdClass'); 221 | $number++; 222 | } 223 | 224 | $this->assertEquals($number, 12); 225 | 226 | $number = 0; 227 | 228 | $people = Personers::find(array('limit' => 33)); 229 | 230 | foreach ($people as $person) { 231 | $this->assertTrue(is_object($person)); 232 | $this->assertEquals(get_class($person), 'Personers'); 233 | $number++; 234 | } 235 | 236 | $people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS); 237 | foreach ($people as $person) { 238 | $this->assertTrue(is_object($person)); 239 | $this->assertEquals(get_class($person), 'Personers'); 240 | $number++; 241 | } 242 | 243 | $people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS); 244 | 245 | foreach ($people as $person) { 246 | $this->assertTrue(is_array($person)); 247 | $this->assertTrue(isset($person['navnes'])); 248 | $number++; 249 | } 250 | 251 | $people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS); 252 | foreach ($people as $person) { 253 | $this->assertTrue(is_object($person)); 254 | $this->assertEquals(get_class($person), 'stdClass'); 255 | $this->assertTrue(isset($person->navnes)); 256 | $number++; 257 | } 258 | 259 | $this->assertEquals($number, 33 * 4); 260 | } 261 | 262 | protected function _executeTestsNormalComplex($di) 263 | { 264 | $result = $di->get('modelsManager')->executeQuery('SELECT id FROM Robots'); 265 | 266 | //Scalar complex query 267 | foreach ($result as $row) { 268 | $this->assertEquals(get_class($row), 'Phalcon\Mvc\Model\Row'); 269 | $this->assertTrue(is_numeric($row->id)); 270 | } 271 | 272 | $result->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS); 273 | foreach ($result as $row) { 274 | $this->assertEquals(get_class($row), 'Phalcon\Mvc\Model\Row'); 275 | $this->assertTrue(is_numeric($row->id)); 276 | } 277 | 278 | $result->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS); 279 | foreach ($result as $row) { 280 | $this->assertTrue(is_array($row)); 281 | $this->assertTrue(is_numeric($row['id'])); 282 | } 283 | 284 | $result->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS); 285 | foreach ($result as $row) { 286 | $this->assertEquals(get_class($row), 'stdClass'); 287 | $this->assertTrue(is_numeric($row->id)); 288 | } 289 | 290 | //Complex resultset including scalars and complete objects 291 | $result = $di->get('modelsManager')->executeQuery('SELECT Robots.id, Robots.*, RobotsParts.* FROM Robots JOIN RobotsParts'); 292 | foreach ($result as $row) { 293 | $this->assertEquals(get_class($row), 'Phalcon\Mvc\Model\Row'); 294 | $this->assertTrue(is_numeric($row->id)); 295 | $this->assertEquals(gettype($row->robots), 'object'); 296 | $this->assertEquals(get_class($row->robots), 'Robots'); 297 | $this->assertEquals(gettype($row->robotsParts), 'object'); 298 | $this->assertEquals(get_class($row->robotsParts), 'RobotsParts'); 299 | } 300 | 301 | $result->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS); 302 | foreach ($result as $row) { 303 | $this->assertEquals(get_class($row), 'Phalcon\Mvc\Model\Row'); 304 | $this->assertTrue(is_numeric($row->id)); 305 | $this->assertEquals(gettype($row->robots), 'object'); 306 | $this->assertEquals(get_class($row->robots), 'Robots'); 307 | $this->assertEquals(gettype($row->robotsParts), 'object'); 308 | $this->assertEquals(get_class($row->robotsParts), 'RobotsParts'); 309 | } 310 | 311 | $result->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS); 312 | foreach ($result as $row) { 313 | $this->assertTrue(is_array($row)); 314 | $this->assertTrue(is_numeric($row['id'])); 315 | $this->assertEquals(gettype($row['robots']), 'array'); 316 | $this->assertEquals(count($row['robots']), 4); 317 | $this->assertEquals(gettype($row['robotsParts']), 'array'); 318 | $this->assertEquals(count($row['robotsParts']), 3); 319 | } 320 | 321 | $result->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS); 322 | foreach ($result as $row) { 323 | $this->assertEquals(get_class($row), 'stdClass'); 324 | $this->assertTrue(is_numeric($row->id)); 325 | $this->assertEquals(gettype($row->robots), 'object'); 326 | $this->assertEquals(get_class($row->robots), 'stdClass'); 327 | $this->assertEquals(gettype($row->robotsParts), 'object'); 328 | $this->assertEquals(get_class($row->robotsParts), 'stdClass'); 329 | } 330 | 331 | } 332 | 333 | } 334 | -------------------------------------------------------------------------------- /unit-tests/ModelsMassAssigmentTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | require "loader.php"; 22 | class ModelsMassAssigmentTest extends PHPUnit_Framework_TestCase 23 | { 24 | 25 | public function __construct() 26 | { 27 | spl_autoload_register(array($this, 'modelsAutoloader')); 28 | } 29 | 30 | public function __destruct() 31 | { 32 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 33 | } 34 | 35 | public function modelsAutoloader($className) 36 | { 37 | if (file_exists('unit-tests/models/'.$className.'.php')) { 38 | require 'unit-tests/models/'.$className.'.php'; 39 | } 40 | } 41 | 42 | protected function _getDI() 43 | { 44 | 45 | Phalcon\DI::reset(); 46 | 47 | $di = new Phalcon\DI(); 48 | 49 | $di->set('modelsManager', function(){ 50 | return new Phalcon\Mvc\Model\Manager(); 51 | }, true); 52 | 53 | $di->set('modelsMetadata', function(){ 54 | return new Phalcon\Mvc\Model\Metadata\Memory(); 55 | }, true); 56 | 57 | return $di; 58 | } 59 | 60 | public function testModels() 61 | { 62 | 63 | $di = $this->_getDI(); 64 | 65 | $di->set('db', function(){ 66 | require 'unit-tests/config.db.php'; 67 | return new Twm\Db\Adapter\Pdo\Mssql($configMssql); 68 | }); 69 | 70 | $this->_executeTestsNormal($di); 71 | $this->_executeTestsRenamed($di); 72 | } 73 | 74 | protected function _executeTestsNormal($di) 75 | { 76 | $robot = new Robots(); 77 | 78 | $success = $robot->save(array( 79 | 'type' => 'mechanical', 80 | 'year' => 2018 81 | )); 82 | 83 | $this->assertEquals($success, false); 84 | $this->assertEquals($robot->type, 'mechanical'); 85 | $this->assertEquals($robot->year, 2018); 86 | 87 | $robot = new Robots(); 88 | 89 | $robot->assign(array( 90 | 'type' => 'mechanical', 91 | 'year' => 2018 92 | )); 93 | 94 | $this->assertEquals($robot->type, 'mechanical'); 95 | $this->assertEquals($robot->year, 2018); 96 | } 97 | 98 | protected function _executeTestsRenamed($di) 99 | { 100 | 101 | $robot = new Robotters(); 102 | 103 | $success = $robot->save(array( 104 | 'theType' => 'mechanical', 105 | 'theYear' => 2018 106 | )); 107 | 108 | $this->assertEquals($success, false); 109 | $this->assertEquals($robot->theType, 'mechanical'); 110 | $this->assertEquals($robot->theYear, 2018); 111 | 112 | $robot = new Robotters(); 113 | 114 | $robot->assign(array( 115 | 'theType' => 'mechanical', 116 | 'theYear' => 2018 117 | )); 118 | 119 | $this->assertEquals($robot->theType, 'mechanical'); 120 | $this->assertEquals($robot->theYear, 2018); 121 | } 122 | 123 | } 124 | -------------------------------------------------------------------------------- /unit-tests/ModelsMetadataAdaptersTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | require_once 'helpers/xcache.php'; 22 | 23 | include "loader.php"; 24 | class ModelsMetadataAdaptersTest extends PHPUnit_Framework_TestCase 25 | { 26 | 27 | private $_data = array( 28 | 'meta-robots-robots' => array( 29 | 0 => array( 30 | 0 => 'id', 31 | 1 => 'name', 32 | 2 => 'type', 33 | 3 => 'year', 34 | ), 35 | 1 => array( 36 | 0 => 'id', 37 | ), 38 | 2 => array( 39 | 0 => 'name', 40 | 1 => 'type', 41 | 2 => 'year', 42 | ), 43 | 3 => array( 44 | 0 => 'id', 45 | 1 => 'name', 46 | 2 => 'type', 47 | 3 => 'year', 48 | ), 49 | 4 => array( 50 | 'id' => 0, 51 | 'name' => 2, 52 | 'type' => 2, 53 | 'year' => 0, 54 | ), 55 | 5 => array( 56 | 'id' => true, 57 | 'year' => true, 58 | ), 59 | 8 => 'id', 60 | 9 => array( 61 | 'id' => 1, 62 | 'name' => 2, 63 | 'type' => 2, 64 | 'year' => 1, 65 | ), 66 | 10 => array(), 67 | 11 => array() 68 | ), 69 | 'map-robots' => array( 70 | 0 => null, 71 | 1 => null, 72 | ) 73 | ); 74 | 75 | public function __construct() 76 | { 77 | spl_autoload_register(array($this, 'modelsAutoloader')); 78 | } 79 | 80 | public function __destruct() 81 | { 82 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 83 | } 84 | 85 | public function modelsAutoloader($className) 86 | { 87 | if (file_exists('unit-tests/models/' . $className . '.php')) { 88 | require 'unit-tests/models/' . $className . '.php'; 89 | } 90 | } 91 | 92 | protected function _getDI() 93 | { 94 | 95 | Phalcon\DI::reset(); 96 | 97 | $di = new Phalcon\DI(); 98 | 99 | $di->set('modelsManager', function(){ 100 | return new Phalcon\Mvc\Model\Manager(); 101 | }); 102 | 103 | $di->set('db', function(){ 104 | require 'unit-tests/config.db.php'; 105 | return new Twm\Db\Adapter\Pdo\Mssql($configMssql); 106 | }); 107 | 108 | return $di; 109 | } 110 | 111 | public function testMetadataMemory() 112 | { 113 | 114 | $di = $this->_getDI(); 115 | 116 | $di->set('modelsMetadata', function(){ 117 | return new Phalcon\Mvc\Model\Metadata\Memory(); 118 | }); 119 | 120 | $metaData = $di->getShared('modelsMetadata'); 121 | 122 | $metaData->reset(); 123 | 124 | $this->assertTrue($metaData->isEmpty()); 125 | 126 | Robots::findFirst(); 127 | 128 | $this->assertFalse($metaData->isEmpty()); 129 | 130 | Robots::findFirst(); 131 | 132 | } 133 | 134 | public function testMetadataSession() 135 | { 136 | 137 | @session_start(); 138 | 139 | $di = $this->_getDI(); 140 | 141 | $di->set('modelsMetadata', function(){ 142 | return new Phalcon\Mvc\Model\Metadata\Session(array( 143 | 'prefix' => 'my-local-app' 144 | )); 145 | }); 146 | 147 | $metaData = $di->getShared('modelsMetadata'); 148 | 149 | $metaData->reset(); 150 | 151 | $this->assertTrue($metaData->isEmpty()); 152 | 153 | Robots::findFirst(); 154 | 155 | $expectedSession = array( 156 | '$PMM$my-local-app' => $this->_data 157 | ); 158 | 159 | $this->assertEquals($expectedSession, $_SESSION); 160 | 161 | $this->assertFalse($metaData->isEmpty()); 162 | 163 | Robots::findFirst(); 164 | 165 | } 166 | 167 | public function testMetadataApc() 168 | { 169 | 170 | if (!function_exists('apc_fetch')) { 171 | $this->markTestSkipped('apc extension is not loaded'); 172 | return false; 173 | } 174 | 175 | apc_delete('$PMM$my-local-app'); 176 | 177 | $di = $this->_getDI(); 178 | 179 | $di->set('modelsMetadata', function(){ 180 | return new Phalcon\Mvc\Model\Metadata\Apc(array( 181 | 'prefix' => 'my-local-app', 182 | 'lifetime' => 60 183 | )); 184 | }); 185 | 186 | $metaData = $di->getShared('modelsMetadata'); 187 | 188 | $metaData->reset(); 189 | 190 | $this->assertTrue($metaData->isEmpty()); 191 | 192 | Robots::findFirst(); 193 | 194 | $this->assertEquals(apc_fetch('$PMM$my-local-appmeta-robots-robots'), $this->_data['meta-robots-robots']); 195 | $this->assertEquals(apc_fetch('$PMM$my-local-appmap-robots'), $this->_data['map-robots']); 196 | 197 | $this->assertFalse($metaData->isEmpty()); 198 | 199 | Robots::findFirst(); 200 | 201 | } 202 | 203 | public function testMetadataXcache() 204 | { 205 | 206 | if (!function_exists('xcache_get')) { 207 | $this->markTestSkipped('xcache extension is not loaded'); 208 | return false; 209 | } 210 | 211 | xcache_unset('$PMM$my-local-app'); 212 | 213 | $di = $this->_getDI(); 214 | 215 | $di->set('modelsMetadata', function(){ 216 | return new Phalcon\Mvc\Model\Metadata\Xcache(array( 217 | 'prefix' => 'my-local-app', 218 | 'lifetime' => 60 219 | )); 220 | }); 221 | 222 | $metaData = $di->getShared('modelsMetadata'); 223 | 224 | $metaData->reset(); 225 | 226 | $this->assertTrue($metaData->isEmpty()); 227 | 228 | Robots::findFirst(); 229 | 230 | $this->assertEquals(apc_fetch('$PMM$my-local-appmeta-robots-robots'), $this->_data['meta-robots-robots']); 231 | $this->assertEquals(apc_fetch('$PMM$my-local-appmap-robots'), $this->_data['map-robots']); 232 | 233 | $this->assertFalse($metaData->isEmpty()); 234 | 235 | Robots::findFirst(); 236 | 237 | } 238 | 239 | public function testMetadataFiles() 240 | { 241 | 242 | $di = $this->_getDI(); 243 | 244 | $di->set('modelsMetadata', function(){ 245 | return new Phalcon\Mvc\Model\Metadata\Files(array( 246 | 'metaDataDir' => 'unit-tests/cache/', 247 | )); 248 | }); 249 | 250 | @unlink('unit-tests/cache/meta-robots-robots.php'); 251 | @unlink('unit-tests/cache/map-robots.php'); 252 | 253 | $metaData = $di->getShared('modelsMetadata'); 254 | 255 | $metaData->reset(); 256 | 257 | $this->assertTrue($metaData->isEmpty()); 258 | 259 | Robots::findFirst(); 260 | 261 | $this->assertEquals(require 'unit-tests/cache/meta-robots-robots.php', $this->_data['meta-robots-robots']); 262 | $this->assertEquals(require 'unit-tests/cache/map-robots.php', $this->_data['map-robots']); 263 | 264 | $this->assertFalse($metaData->isEmpty()); 265 | 266 | Robots::findFirst(); 267 | } 268 | 269 | } 270 | -------------------------------------------------------------------------------- /unit-tests/ModelsMetadataManualTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | class ModelsMetadataManualTest extends PHPUnit_Framework_TestCase 22 | { 23 | 24 | public function __construct() 25 | { 26 | spl_autoload_register(array($this, 'modelsAutoloader')); 27 | } 28 | 29 | public function __destruct() 30 | { 31 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 32 | } 33 | 34 | public function modelsAutoloader($className) 35 | { 36 | if (file_exists('unit-tests/models/'.$className.'.php')) { 37 | require 'unit-tests/models/'.$className.'.php'; 38 | } 39 | } 40 | 41 | public function testMetadataManual() 42 | { 43 | 44 | $di = new Phalcon\DI(); 45 | 46 | $di->set('modelsManager', function(){ 47 | return new Phalcon\Mvc\Model\Manager(); 48 | }); 49 | 50 | $di->set('modelsMetadata', function(){ 51 | return new Phalcon\Mvc\Model\Metadata\Memory(); 52 | }); 53 | 54 | $metaData = $di->getShared('modelsMetadata'); 55 | 56 | $robotto = new Robotto($di); 57 | 58 | //Robots 59 | $pAttributes = array( 60 | 0 => 'id', 61 | 1 => 'name', 62 | 2 => 'type', 63 | 3 => 'year' 64 | ); 65 | 66 | $attributes = $metaData->getAttributes($robotto); 67 | $this->assertEquals($attributes, $pAttributes); 68 | 69 | $ppkAttributes = array( 70 | 0 => 'id' 71 | ); 72 | 73 | $pkAttributes = $metaData->getPrimaryKeyAttributes($robotto); 74 | $this->assertEquals($ppkAttributes, $pkAttributes); 75 | 76 | $pnpkAttributes = array( 77 | 0 => 'name', 78 | 1 => 'type', 79 | 2 => 'year' 80 | ); 81 | 82 | $npkAttributes = $metaData->getNonPrimaryKeyAttributes($robotto); 83 | $this->assertEquals($pnpkAttributes, $npkAttributes); 84 | 85 | $this->assertEquals($metaData->getIdentityField($robotto), 'id'); 86 | 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /unit-tests/ModelsMetadataStrategyTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | include "loader.php"; 21 | class ModelsMetadataStrategyTest extends PHPUnit_Framework_TestCase 22 | { 23 | 24 | protected $_expectedMeta = array( 25 | 0 => array( 26 | 0 => 'id', 27 | 1 => 'name', 28 | 2 => 'type', 29 | 3 => 'year', 30 | ), 31 | 1 => array( 32 | 0 => 'id', 33 | ), 34 | 2 => array( 35 | 0 => 'name', 36 | 1 => 'type', 37 | 2 => 'year', 38 | ), 39 | 3 => array( 40 | 0 => 'id', 41 | 1 => 'name', 42 | 2 => 'type', 43 | 3 => 'year', 44 | ), 45 | 4 => array( 46 | 'id' => 0, 47 | 'name' => 2, 48 | 'type' => 2, 49 | 'year' => 0, 50 | ), 51 | 5 => array( 52 | 'id' => true, 53 | 'year' => true, 54 | ), 55 | 8 => 'id', 56 | 9 => array( 57 | 'id' => 1, 58 | 'name' => 2, 59 | 'type' => 2, 60 | 'year' => 1, 61 | ), 62 | 10 => array(), 63 | 11 => array(), 64 | ); 65 | 66 | public function __construct() 67 | { 68 | spl_autoload_register(array($this, 'modelsAutoloader')); 69 | } 70 | 71 | public function __destruct() 72 | { 73 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 74 | } 75 | 76 | public function modelsAutoloader($className) 77 | { 78 | $className = str_replace('\\', '/', $className); 79 | if (file_exists('unit-tests/models/'.$className.'.php')) { 80 | require 'unit-tests/models/'.$className.'.php'; 81 | } 82 | } 83 | 84 | protected function _getDI() 85 | { 86 | 87 | Phalcon\DI::reset(); 88 | 89 | $di = new Phalcon\DI(); 90 | 91 | $di['modelsManager'] = function() { 92 | return new Phalcon\Mvc\Model\Manager(); 93 | }; 94 | 95 | $di['db'] = function() { 96 | require 'unit-tests/config.db.php'; 97 | return new Twm\Db\Adapter\Pdo\Mssql($configMssql); 98 | //return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 99 | }; 100 | 101 | $di['annotations'] = function() { 102 | return new Phalcon\Annotations\Adapter\Memory(); 103 | }; 104 | 105 | return $di; 106 | } 107 | 108 | public function testMetadataDatabaseIntrospection() 109 | { 110 | 111 | $di = $this->_getDI(); 112 | 113 | $di['modelsMetadata'] = function() { 114 | $metaData = new Phalcon\Mvc\Model\Metadata\Memory(); 115 | $metaData->setStrategy(new Phalcon\Mvc\Model\MetaData\Strategy\Introspection()); 116 | return $metaData; 117 | }; 118 | 119 | $metaData = $di['modelsMetadata']; 120 | 121 | $robots = new Robots($di); 122 | 123 | $meta = $metaData->readMetaData($robots); 124 | $this->assertEquals($meta, $this->_expectedMeta); 125 | 126 | $meta = $metaData->readMetaData($robots); 127 | $this->assertEquals($meta, $this->_expectedMeta); 128 | } 129 | 130 | public function testMetadataAnnotations() 131 | { 132 | 133 | $di = $this->_getDI(); 134 | 135 | $di['modelsMetadata'] = function(){ 136 | $metaData = new Phalcon\Mvc\Model\Metadata\Memory(); 137 | $metaData->setStrategy(new Phalcon\Mvc\Model\MetaData\Strategy\Annotations()); 138 | return $metaData; 139 | }; 140 | 141 | $metaData = $di['modelsMetadata']; 142 | 143 | $robots = new Boutique\Robots(); 144 | 145 | $meta = $metaData->readMetaData($robots); 146 | $this->assertEquals($meta, $this->_expectedMeta); 147 | 148 | $meta = $metaData->readMetaData($robots); 149 | $this->assertEquals($meta, $this->_expectedMeta); 150 | } 151 | 152 | } 153 | -------------------------------------------------------------------------------- /unit-tests/ModelsMetadataTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | class ModelsMetadataTest extends PHPUnit_Framework_TestCase 22 | { 23 | 24 | public function __construct() 25 | { 26 | spl_autoload_register(array($this, 'modelsAutoloader')); 27 | } 28 | 29 | public function __destruct() 30 | { 31 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 32 | } 33 | 34 | public function modelsAutoloader($className) 35 | { 36 | if (file_exists('unit-tests/models/'.$className.'.php')) { 37 | require 'unit-tests/models/'.$className.'.php'; 38 | } 39 | } 40 | 41 | protected function _getDI() 42 | { 43 | 44 | $di = new Phalcon\DI(); 45 | 46 | $di->set('modelsManager', function(){ 47 | return new Phalcon\Mvc\Model\Manager(); 48 | }); 49 | 50 | $di->set('modelsMetadata', function(){ 51 | return new Phalcon\Mvc\Model\Metadata\Memory(); 52 | }); 53 | 54 | return $di; 55 | } 56 | 57 | public function testMetadataMssql() 58 | { 59 | require 'unit-tests/config.db.php'; 60 | if (empty($configMssql)) { 61 | $this->markTestSkipped("Skipped"); 62 | return; 63 | } 64 | 65 | $di = $this->_getDI(); 66 | 67 | $di->set('db', function(){ 68 | include "loader.php"; 69 | require 'unit-tests/config.db.php'; 70 | return new Twm\Db\Adapter\Pdo\Mssql($configMssql); 71 | }); 72 | 73 | $this->_executeTests($di); 74 | 75 | } 76 | 77 | public function testMetadataMysql() 78 | { 79 | require 'unit-tests/config.db.php'; 80 | if (empty($configMysql)) { 81 | $this->markTestSkipped("Skipped"); 82 | return; 83 | } 84 | 85 | $di = $this->_getDI(); 86 | 87 | $di->set('db', function(){ 88 | require 'unit-tests/config.db.php'; 89 | return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 90 | }); 91 | 92 | $this->_executeTests($di); 93 | 94 | } 95 | 96 | protected function _executeTests($di) 97 | { 98 | 99 | $metaData = $di->getShared('modelsMetadata'); 100 | 101 | $personas = new Personas($di); 102 | 103 | $pAttributes = array( 104 | 0 => 'cedula', 105 | 1 => 'tipo_documento_id', 106 | 2 => 'nombres', 107 | 3 => 'telefono', 108 | 4 => 'direccion', 109 | 5 => 'email', 110 | 6 => 'fecha_nacimiento', 111 | 7 => 'ciudad_id', 112 | 8 => 'creado_at', 113 | 9 => 'cupo', 114 | 10 => 'estado', 115 | ); 116 | 117 | $attributes = $metaData->getAttributes($personas); 118 | $this->assertEquals($attributes, $pAttributes); 119 | 120 | $ppkAttributes = array( 121 | 0 => 'cedula' 122 | ); 123 | 124 | $pkAttributes = $metaData->getPrimaryKeyAttributes($personas); 125 | $this->assertEquals($ppkAttributes, $pkAttributes); 126 | 127 | $pnpkAttributes = array( 128 | 0 => 'tipo_documento_id', 129 | 1 => 'nombres', 130 | 2 => 'telefono', 131 | 3 => 'direccion', 132 | 4 => 'email', 133 | 5 => 'fecha_nacimiento', 134 | 6 => 'ciudad_id', 135 | 7 => 'creado_at', 136 | 8 => 'cupo', 137 | 9 => 'estado', 138 | ); 139 | 140 | $npkAttributes = $metaData->getNonPrimaryKeyAttributes($personas); 141 | $this->assertEquals($pnpkAttributes, $npkAttributes); 142 | 143 | $pnnAttributes = array( 144 | 0 => 'cedula', 145 | 1 => 'tipo_documento_id', 146 | 2 => 'nombres', 147 | 3 => 'cupo', 148 | 4 => 'estado' 149 | ); 150 | 151 | $nnAttributes = $metaData->getNotNullAttributes($personas); 152 | $this->assertEquals($nnAttributes, $pnnAttributes); 153 | 154 | $dataTypes = array( 155 | 'cedula' => 5, 156 | 'tipo_documento_id' => 0, 157 | 'nombres' => 2, 158 | 'telefono' => 2, 159 | 'direccion' => 2, 160 | 'email' => 2, 161 | 'fecha_nacimiento' => 1, 162 | 'ciudad_id' => 0, 163 | 'creado_at' => 1, 164 | 'cupo' => 3, 165 | 'estado' => 5, 166 | ); 167 | 168 | $dtAttributes = $metaData->getDataTypes($personas); 169 | $this->assertEquals($dtAttributes, $dataTypes); 170 | 171 | $pndAttributes = array( 172 | 'tipo_documento_id' => true, 173 | 'ciudad_id' => true, 174 | 'cupo' => true, 175 | ); 176 | $ndAttributes = $metaData->getDataTypesNumeric($personas); 177 | $this->assertEquals($ndAttributes, $pndAttributes); 178 | 179 | $bindTypes = array( 180 | 'cedula' => 2, 181 | 'tipo_documento_id' => 1, 182 | 'nombres' => 2, 183 | 'telefono' => 2, 184 | 'direccion' => 2, 185 | 'email' => 2, 186 | 'fecha_nacimiento' => 2, 187 | 'ciudad_id' => 1, 188 | 'creado_at' => 2, 189 | 'cupo' => 32, 190 | 'estado' => 2, 191 | ); 192 | 193 | $btAttributes = $metaData->getBindTypes($personas); 194 | $this->assertEquals($btAttributes, $bindTypes); 195 | 196 | $robots = new Robots($di); 197 | 198 | //Robots 199 | $pAttributes = array( 200 | 0 => 'id', 201 | 1 => 'name', 202 | 2 => 'type', 203 | 3 => 'year' 204 | ); 205 | 206 | $attributes = $metaData->getAttributes($robots); 207 | $this->assertEquals($attributes, $pAttributes); 208 | 209 | $ppkAttributes = array( 210 | 0 => 'id' 211 | ); 212 | 213 | $pkAttributes = $metaData->getPrimaryKeyAttributes($robots); 214 | $this->assertEquals($ppkAttributes, $pkAttributes); 215 | 216 | $pnpkAttributes = array( 217 | 0 => 'name', 218 | 1 => 'type', 219 | 2 => 'year' 220 | ); 221 | 222 | $npkAttributes = $metaData->getNonPrimaryKeyAttributes($robots); 223 | $this->assertEquals($pnpkAttributes, $npkAttributes); 224 | $this->assertEquals($metaData->getIdentityField($robots), 'id'); 225 | 226 | } 227 | 228 | } 229 | -------------------------------------------------------------------------------- /unit-tests/ModelsMultipleSourcesTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | require "loader.php"; 22 | class ModelsMultipleSourcesTest extends PHPUnit_Framework_TestCase 23 | { 24 | 25 | public function __construct() 26 | { 27 | spl_autoload_register(array($this, 'modelsAutoloader')); 28 | } 29 | 30 | public function __destruct() 31 | { 32 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 33 | } 34 | 35 | public function modelsAutoloader($className) 36 | { 37 | $className = str_replace('\\', '/', $className); 38 | if (file_exists('unit-tests/models/'.$className.'.php')) { 39 | require 'unit-tests/models/'.$className.'.php'; 40 | } 41 | } 42 | 43 | protected function _prepareDI() 44 | { 45 | Phalcon\DI::reset(); 46 | 47 | $di = new Phalcon\DI(); 48 | 49 | $di->set('modelsManager', function() { 50 | return new Phalcon\Mvc\Model\Manager(); 51 | }, true); 52 | 53 | $di->set('modelsMetadata', function() { 54 | return new Phalcon\Mvc\Model\Metadata\Memory(); 55 | }, true); 56 | 57 | $di->set('db', function() { 58 | throw new Exception('Using default database source'); 59 | }); 60 | 61 | $di->set('dbOne', function() { 62 | require 'unit-tests/config.db.php'; 63 | return new Twm\Db\Adapter\Pdo\Mssql($configMssql); 64 | }); 65 | 66 | $di->set('dbTwo', function() { 67 | require 'unit-tests/config.db.php'; 68 | return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 69 | }); 70 | 71 | } 72 | 73 | public function testSourcesStatic() 74 | { 75 | 76 | $this->_prepareDI(); 77 | 78 | $robot = Store\Robots::findFirst(); 79 | $this->assertTrue(is_object($robot)); 80 | $this->assertTrue($robot->save()); 81 | 82 | $robotParts = $robot->getRobotParts(); 83 | $this->assertTrue(is_object($robotParts)); 84 | 85 | foreach ($robotParts as $robotPart) { 86 | $this->assertTrue(is_object($robotPart->getRobot())); 87 | $this->assertTrue(is_object($robotPart->getPart())); 88 | } 89 | } 90 | 91 | public function testSourcesInstance() 92 | { 93 | 94 | $this->_prepareDI(); 95 | 96 | $robot = new Store\Robots(); 97 | $this->assertFalse($robot->save()); 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /unit-tests/ModelsQueryBuilderTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | use Phalcon\Mvc\Model\Query\Builder as Builder; 22 | include "loader.php"; 23 | class ModelsQueryBuilderTest extends PHPUnit_Framework_TestCase 24 | { 25 | 26 | public function __construct() 27 | { 28 | spl_autoload_register(array($this, 'modelsAutoloader')); 29 | } 30 | 31 | public function __destruct() 32 | { 33 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 34 | } 35 | 36 | public function modelsAutoloader($className) 37 | { 38 | $className = str_replace("\\", DIRECTORY_SEPARATOR, $className); 39 | $path = 'unit-tests/models/'.$className.'.php'; 40 | if (file_exists($path)) { 41 | require $path; 42 | } 43 | } 44 | 45 | protected function _getDI() 46 | { 47 | 48 | Phalcon\DI::reset(); 49 | 50 | $di = new Phalcon\DI(); 51 | 52 | $di->set('modelsManager', function(){ 53 | return new Phalcon\Mvc\Model\Manager(); 54 | }); 55 | 56 | $di->set('modelsMetadata', function(){ 57 | return new Phalcon\Mvc\Model\Metadata\Memory(); 58 | }); 59 | 60 | $di->set('db', function(){ 61 | require 'unit-tests/config.db.php'; 62 | return new Twm\Db\Adapter\Pdo\Mssql($configMssql); 63 | }); 64 | 65 | return $di; 66 | } 67 | 68 | public function testAction() 69 | { 70 | 71 | $di = $this->_getDI(); 72 | 73 | $builder = new Builder(); 74 | $phql = $builder->setDi($di) 75 | ->from('Robots') 76 | ->getPhql(); 77 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots]'); 78 | 79 | $builder = new Builder(); 80 | $phql = $builder->setDi($di) 81 | ->from(array('Robots', 'RobotsParts')) 82 | ->getPhql(); 83 | $this->assertEquals($phql, 'SELECT [Robots].*, [RobotsParts].* FROM [Robots], [RobotsParts]'); 84 | 85 | $builder = new Builder(); 86 | $phql = $builder->setDi($di) 87 | ->columns('*') 88 | ->from('Robots') 89 | ->getPhql(); 90 | $this->assertEquals($phql, 'SELECT * FROM [Robots]'); 91 | 92 | $builder = new Builder(); 93 | $phql = $builder->setDi($di) 94 | ->columns(array('id', 'name')) 95 | ->from('Robots') 96 | ->getPhql(); 97 | $this->assertEquals($phql, 'SELECT id, name FROM [Robots]'); 98 | 99 | $builder = new Builder(); 100 | $phql = $builder->setDi($di) 101 | ->columns('id') 102 | ->from('Robots') 103 | ->getPhql(); 104 | $this->assertEquals($phql, 'SELECT id FROM [Robots]'); 105 | 106 | $builder = new Builder(); 107 | $phql = $builder->setDi($di) 108 | ->from('Robots') 109 | ->where('Robots.name = "Voltron"') 110 | ->getPhql(); 111 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] WHERE Robots.name = "Voltron"'); 112 | 113 | $builder = new Builder(); 114 | $phql = $builder->setDi($di) 115 | ->from('Robots') 116 | ->where('Robots.name = "Voltron"') 117 | ->andWhere('Robots.id > 100') 118 | ->getPhql(); 119 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] WHERE (Robots.name = "Voltron") AND (Robots.id > 100)'); 120 | 121 | $builder = new Builder(); 122 | $phql = $builder->setDi($di) 123 | ->from('Robots') 124 | ->where('Robots.name = "Voltron"') 125 | ->orWhere('Robots.id > 100') 126 | ->getPhql(); 127 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] WHERE (Robots.name = "Voltron") OR (Robots.id > 100)'); 128 | 129 | $builder = new Builder(); 130 | $phql = $builder->setDi($di) 131 | ->from('Robots') 132 | ->where(100) 133 | ->getPhql(); 134 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] WHERE [Robots].[id] = 100'); 135 | 136 | $builder = new Builder(); 137 | $phql = $builder->setDi($di) 138 | ->from('Robots') 139 | ->groupBy('Robots.name') 140 | ->getPhql(); 141 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] GROUP BY Robots.name'); 142 | 143 | $builder = new Builder(); 144 | $phql = $builder->setDi($di) 145 | ->from('Robots') 146 | ->groupBy(array('Robots.name', 'Robots.id')) 147 | ->getPhql(); 148 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] GROUP BY Robots.name, Robots.id'); 149 | 150 | $builder = new Builder(); 151 | $phql = $builder->setDi($di) 152 | ->columns(array('Robots.name', 'SUM(Robots.price)')) 153 | ->from('Robots') 154 | ->groupBy('Robots.name') 155 | ->getPhql(); 156 | $this->assertEquals($phql, 'SELECT Robots.name, SUM(Robots.price) FROM [Robots] GROUP BY Robots.name'); 157 | 158 | $builder = new Builder(); 159 | $phql = $builder->setDi($di) 160 | ->columns(array('Robots.name', 'SUM(Robots.price)')) 161 | ->from('Robots') 162 | ->groupBy('Robots.name') 163 | ->having('SUM(Robots.price) > 1000') 164 | ->getPhql(); 165 | $this->assertEquals($phql, 'SELECT Robots.name, SUM(Robots.price) FROM [Robots] GROUP BY Robots.name HAVING SUM(Robots.price) > 1000'); 166 | 167 | $builder = new Builder(); 168 | $phql = $builder->setDi($di) 169 | ->from('Robots') 170 | ->join('RobotsParts') 171 | ->getPhql(); 172 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] JOIN [RobotsParts]'); 173 | 174 | $builder = new Builder(); 175 | $phql = $builder->setDi($di) 176 | ->from('Robots') 177 | ->join('RobotsParts', null, 'p') 178 | ->getPhql(); 179 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] JOIN [RobotsParts] AS [p]'); 180 | 181 | $builder = new Builder(); 182 | $phql = $builder->setDi($di) 183 | ->from('Robots') 184 | ->join('RobotsParts', 'Robots.id = RobotsParts.robots_id', 'p') 185 | ->getPhql(); 186 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] JOIN [RobotsParts] AS [p] ON Robots.id = RobotsParts.robots_id'); 187 | 188 | $builder = new Builder(); 189 | $phql = $builder->setDi($di) 190 | ->from('Robots') 191 | ->join('RobotsParts', 'Robots.id = RobotsParts.robots_id', 'p') 192 | ->join('Parts', 'Parts.id = RobotsParts.parts_id', 't') 193 | ->getPhql(); 194 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] JOIN [RobotsParts] AS [p] ON Robots.id = RobotsParts.robots_id JOIN [Parts] AS [t] ON Parts.id = RobotsParts.parts_id'); 195 | 196 | $builder = new Builder(); 197 | $phql = $builder->setDi($di) 198 | ->from('Robots') 199 | ->leftJoin('RobotsParts', 'Robots.id = RobotsParts.robots_id') 200 | ->leftJoin('Parts', 'Parts.id = RobotsParts.parts_id') 201 | ->where('Robots.id > 0') 202 | ->getPhql(); 203 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] LEFT JOIN [RobotsParts] ON Robots.id = RobotsParts.robots_id LEFT JOIN [Parts] ON Parts.id = RobotsParts.parts_id WHERE Robots.id > 0'); 204 | 205 | $builder = new Builder(); 206 | $phql = $builder->setDi($di) 207 | ->addFrom('Robots', 'r') 208 | ->getPhql(); 209 | $this->assertEquals($phql, 'SELECT [r].* FROM [Robots] AS [r]'); 210 | 211 | $builder = new Builder(); 212 | $phql = $builder->setDi($di) 213 | ->from('Robots') 214 | ->addFrom('Parts', 'p') 215 | ->getPhql(); 216 | $this->assertEquals($phql, 'SELECT [Robots].*, [p].* FROM [Robots], [Parts] AS [p]'); 217 | 218 | $builder = new Builder(); 219 | $phql = $builder->setDi($di) 220 | ->from(array('r' => 'Robots')) 221 | ->addFrom('Parts', 'p') 222 | ->getPhql(); 223 | $this->assertEquals($phql, 'SELECT [r].*, [p].* FROM [Robots] AS [r], [Parts] AS [p]'); 224 | 225 | 226 | $builder = new Builder(); 227 | $phql = $builder->setDi($di) 228 | ->from(array('r' => 'Robots', 'p' => 'Parts')) 229 | ->getPhql(); 230 | $this->assertEquals($phql, 'SELECT [r].*, [p].* FROM [Robots] AS [r], [Parts] AS [p]'); 231 | 232 | 233 | $builder = new Builder(); 234 | $phql = $builder->setDi($di) 235 | ->from('Robots') 236 | ->orderBy('Robots.name') 237 | ->getPhql(); 238 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] ORDER BY Robots.name'); 239 | 240 | $builder = new Builder(); 241 | $phql = $builder->setDi($di) 242 | ->from('Robots') 243 | ->orderBy(array(1, 'Robots.name')) 244 | ->getPhql(); 245 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] ORDER BY 1, Robots.name'); 246 | 247 | $builder = new Builder(); 248 | $phql = $builder->setDi($di) 249 | ->from('Robots') 250 | ->limit(10) 251 | ->getPhql(); 252 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] LIMIT 10'); 253 | 254 | $builder = new Builder(); 255 | $phql = $builder->setDi($di) 256 | ->from('Robots') 257 | ->limit(10, 5) 258 | ->getPhql(); 259 | $this->assertEquals($phql, 'SELECT [Robots].* FROM [Robots] LIMIT 10 OFFSET 5'); 260 | } 261 | 262 | } 263 | -------------------------------------------------------------------------------- /unit-tests/ModelsRelationsMagicTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | include "loader.php"; 22 | class ModelsRelationsMagicTest extends PHPUnit_Framework_TestCase 23 | { 24 | 25 | public function __construct() 26 | { 27 | spl_autoload_register(array($this, 'modelsAutoloader')); 28 | } 29 | 30 | public function __destruct() 31 | { 32 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 33 | } 34 | 35 | public function modelsAutoloader($className) 36 | { 37 | $className = str_replace('\\', DIRECTORY_SEPARATOR, $className); 38 | if (file_exists('unit-tests/models/' . $className . '.php')) { 39 | require 'unit-tests/models/' . $className . '.php'; 40 | } 41 | } 42 | 43 | protected function _getDI() 44 | { 45 | 46 | Phalcon\DI::reset(); 47 | 48 | $di = new Phalcon\DI(); 49 | 50 | $di->set('modelsManager', function(){ 51 | return new Phalcon\Mvc\Model\Manager(); 52 | }); 53 | 54 | $di->set('modelsMetadata', function(){ 55 | return new Phalcon\Mvc\Model\Metadata\Memory(); 56 | }); 57 | 58 | return $di; 59 | } 60 | 61 | public function testModelsMssql() 62 | { 63 | 64 | $di = $this->_getDI(); 65 | 66 | require 'unit-tests/config.db.php'; 67 | $connection = new Twm\Db\Adapter\Pdo\Mssql($configMssql); 68 | 69 | $di->set('db', $connection); 70 | 71 | $this->_executeQueryRelated(); 72 | $this->_executeSaveRelatedBelongsTo($connection); 73 | } 74 | 75 | /* 76 | public function testModelsMysql() 77 | { 78 | 79 | $di = $this->_getDI(); 80 | 81 | require 'unit-tests/config.db.php'; 82 | $connection = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 83 | 84 | $di->set('db', $connection); 85 | 86 | $this->_executeQueryRelated(); 87 | $this->_executeSaveRelatedBelongsTo($connection); 88 | } 89 | 90 | public function testModelsPostgresql() 91 | { 92 | 93 | $di = $this->_getDI(); 94 | 95 | $di->set('db', function(){ 96 | require 'unit-tests/config.db.php'; 97 | return new Phalcon\Db\Adapter\Pdo\Postgresql($configPostgresql); 98 | }); 99 | 100 | $this->_executeQueryRelated(); 101 | $this->_executeSaveRelatedBelongsTo($connection); 102 | } 103 | 104 | public function testModelsSqlite() 105 | { 106 | 107 | $di = $this->_getDI(); 108 | 109 | $di->set('db', function(){ 110 | require 'unit-tests/config.db.php'; 111 | return new Phalcon\Db\Adapter\Pdo\Sqlite($configSqlite); 112 | }); 113 | 114 | $this->_executeQueryRelated(); 115 | $this->_executeSaveRelatedBelongsTo($connection); 116 | }*/ 117 | 118 | public function _executeQueryRelated() 119 | { 120 | 121 | //Belongs to 122 | $album = AlbumORama\Albums::findFirst(); 123 | $this->assertEquals(get_class($album), 'AlbumORama\Albums'); 124 | 125 | $artist = $album->artist; 126 | $this->assertEquals(get_class($artist), 'AlbumORama\Artists'); 127 | 128 | $albums = $artist->albums; 129 | $this->assertEquals(get_class($albums), 'Phalcon\Mvc\Model\Resultset\Simple'); 130 | $this->assertEquals(count($albums), 2); 131 | $this->assertEquals(get_class($albums[0]), 'AlbumORama\Albums'); 132 | 133 | $songs = $album->songs; 134 | $this->assertEquals(get_class($songs), 'Phalcon\Mvc\Model\Resultset\Simple'); 135 | $this->assertEquals(count($songs), 7); 136 | $this->assertEquals(get_class($songs[0]), 'AlbumORama\Songs'); 137 | 138 | $originalAlbum = $album->artist->albums[0]; 139 | $this->assertEquals($originalAlbum->id, $album->id); 140 | } 141 | 142 | public function _executeSaveRelatedBelongsTo($connection) 143 | { 144 | $artist = new AlbumORama\Artists(); 145 | 146 | $album = new AlbumORama\Albums(); 147 | $album->artist = $artist; 148 | 149 | //Due to not null fields on both models the album/artist aren't saved 150 | $this->assertFalse($album->save()); 151 | $this->assertFalse((bool) $connection->isUnderTransaction()); 152 | 153 | //The artists must no be saved 154 | $this->assertEquals($artist->getDirtyState(), Phalcon\Mvc\Model::DIRTY_STATE_TRANSIENT); 155 | 156 | //The messages produced are generated by the artist model 157 | $messages = $album->getMessages(); 158 | $this->assertEquals($messages[0]->getMessage(), 'name is required'); 159 | $this->assertEquals(get_class($messages[0]->getModel()), 'AlbumORama\Artists'); 160 | 161 | //Fix the artist problem and try to save again 162 | $artist->name = 'Van She'; 163 | 164 | //Due to not null fields on album model the whole 165 | $this->assertFalse($album->save()); 166 | $this->assertFalse((bool) $connection->isUnderTransaction()); 167 | 168 | //The artist model was saved correctly but album not 169 | $this->assertEquals($artist->getDirtyState(), Phalcon\Mvc\Model::DIRTY_STATE_PERSISTENT); 170 | $this->assertEquals($album->getDirtyState(), Phalcon\Mvc\Model::DIRTY_STATE_TRANSIENT); 171 | 172 | $messages = $album->getMessages(); 173 | $this->assertEquals($messages[0]->getMessage(), 'name is required'); 174 | $this->assertEquals(gettype($messages[0]->getModel()), 'NULL'); 175 | 176 | //Fix the album problem and try to save again 177 | $album->name = 'Idea of Happiness'; 178 | 179 | //Saving OK 180 | $this->assertTrue($album->save()); 181 | $this->assertFalse((bool) $connection->isUnderTransaction()); 182 | 183 | //Both messages must be saved correctly 184 | $this->assertEquals($artist->getDirtyState(), Phalcon\Mvc\Model::DIRTY_STATE_PERSISTENT); 185 | $this->assertEquals($album->getDirtyState(), Phalcon\Mvc\Model::DIRTY_STATE_PERSISTENT); 186 | } 187 | 188 | } 189 | -------------------------------------------------------------------------------- /unit-tests/ModelsResultsetCacheStaticTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | class ModelsResultsetCacheStaticTest extends PHPUnit_Framework_TestCase 22 | { 23 | 24 | public function __construct() 25 | { 26 | spl_autoload_register(array($this, 'modelsAutoloader')); 27 | } 28 | 29 | public function __destruct() 30 | { 31 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 32 | } 33 | 34 | public function modelsAutoloader($className) 35 | { 36 | $className = str_replace('\\', '/', $className); 37 | if (file_exists('unit-tests/models/'.$className.'.php')) { 38 | require 'unit-tests/models/'.$className.'.php'; 39 | } 40 | } 41 | 42 | public function setUp() 43 | { 44 | $iterator = new DirectoryIterator('unit-tests/cache/'); 45 | foreach ($iterator as $item) { 46 | if (!$item->isDir()) { 47 | unlink($item->getPathname()); 48 | } 49 | } 50 | } 51 | 52 | public function testOverrideStaticCache() 53 | { 54 | Phalcon\DI::reset(); 55 | 56 | $di = new Phalcon\DI(); 57 | 58 | $di['db'] = function(){ 59 | require 'unit-tests/config.db.php'; 60 | return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 61 | }; 62 | 63 | $di['modelsManager'] = function(){ 64 | return new Phalcon\Mvc\Model\Manager(); 65 | }; 66 | 67 | $di['modelsMetadata'] = function(){ 68 | return new Phalcon\Mvc\Model\Metadata\Memory(); 69 | }; 70 | 71 | $di['modelsCache'] = function(){ 72 | $frontCache = new Phalcon\Cache\Frontend\Data(); 73 | return new Phalcon\Cache\Backend\File($frontCache, array( 74 | 'cacheDir' => 'unit-tests/cache/' 75 | )); 76 | }; 77 | 78 | $robot = Cacheable\Robots::findFirst(2); 79 | $this->assertEquals(get_class($robot), 'Cacheable\Robots'); 80 | 81 | $robot = Cacheable\Robots::findFirst(2); 82 | $this->assertEquals(get_class($robot), 'Cacheable\Robots'); 83 | 84 | $robot = Cacheable\Robots::findFirst(array('id = 2')); 85 | $this->assertEquals(get_class($robot), 'Cacheable\Robots'); 86 | 87 | $robot = Cacheable\Robots::findFirst(array('id = 2')); 88 | $this->assertEquals(get_class($robot), 'Cacheable\Robots'); 89 | 90 | $robot = Cacheable\Robots::findFirst(array('order' => 'id DESC')); 91 | $this->assertEquals(get_class($robot), 'Cacheable\Robots'); 92 | 93 | $robot = Cacheable\Robots::findFirst(array('order' => 'id DESC')); 94 | $this->assertEquals(get_class($robot), 'Cacheable\Robots'); 95 | 96 | $robot = Cacheable\Robots::findFirst(1); 97 | $this->assertEquals(get_class($robot), 'Cacheable\Robots'); 98 | 99 | $robotParts = $robot->getRobotsParts(); 100 | $this->assertEquals(get_class($robotParts), 'Phalcon\Mvc\Model\Resultset\Simple'); 101 | 102 | $robotParts = $robot->getRobotsParts(); 103 | $this->assertEquals(get_class($robotParts), 'Phalcon\Mvc\Model\Resultset\Simple'); 104 | 105 | $part = $robotParts[0]->getParts(); 106 | $this->assertEquals(get_class($part), 'Cacheable\Parts'); 107 | 108 | $part = $robotParts[0]->getParts(); 109 | $this->assertEquals(get_class($part), 'Cacheable\Parts'); 110 | 111 | $robot = $robotParts[0]->getRobots(); 112 | $this->assertEquals(get_class($robot), 'Cacheable\Robots'); 113 | 114 | $robot = $robotParts[0]->getRobots(); 115 | $this->assertEquals(get_class($robot), 'Cacheable\Robots'); 116 | 117 | } 118 | 119 | 120 | } 121 | -------------------------------------------------------------------------------- /unit-tests/ModelsResultsetCacheTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | class ModelsResultsetCacheTest extends PHPUnit_Framework_TestCase 22 | { 23 | 24 | public function __construct() 25 | { 26 | spl_autoload_register(array($this, 'modelsAutoloader')); 27 | } 28 | 29 | public function __destruct() 30 | { 31 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 32 | } 33 | 34 | public function modelsAutoloader($className) 35 | { 36 | if (file_exists('unit-tests/models/'.$className.'.php')) { 37 | require 'unit-tests/models/'.$className.'.php'; 38 | } 39 | } 40 | 41 | protected function _getDI() 42 | { 43 | 44 | Phalcon\DI::reset(); 45 | 46 | $di = new Phalcon\DI(); 47 | 48 | $di->set('modelsManager', function(){ 49 | return new Phalcon\Mvc\Model\Manager(); 50 | }); 51 | 52 | $di->set('modelsMetadata', function(){ 53 | return new Phalcon\Mvc\Model\Metadata\Memory(); 54 | }); 55 | 56 | return $di; 57 | } 58 | 59 | public function setUp() 60 | { 61 | $iterator = new DirectoryIterator('unit-tests/cache/'); 62 | foreach ($iterator as $item) { 63 | if (!$item->isDir()) { 64 | unlink($item->getPathname()); 65 | } 66 | } 67 | } 68 | 69 | private function _prepareTestMysql() 70 | { 71 | require 'unit-tests/config.db.php'; 72 | if (empty($configMysql)) { 73 | return null; 74 | } 75 | 76 | $di = $this->_getDI(); 77 | 78 | $di->set('db', function(){ 79 | require 'unit-tests/config.db.php'; 80 | return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 81 | }); 82 | 83 | return $di; 84 | } 85 | 86 | private function _prepareTestPostgresql() 87 | { 88 | require 'unit-tests/config.db.php'; 89 | if (empty($configPostgresql)) { 90 | return null; 91 | } 92 | 93 | $di = $this->_getDI(); 94 | 95 | $di->set('db', function(){ 96 | require 'unit-tests/config.db.php'; 97 | return new Phalcon\Db\Adapter\Pdo\Postgresql($configPostgresql); 98 | }); 99 | 100 | return $di; 101 | } 102 | 103 | private function _prepareTestSqlite() 104 | { 105 | require 'unit-tests/config.db.php'; 106 | if (empty($configSqlite)) { 107 | return null; 108 | } 109 | 110 | $di = $this->_getDI(); 111 | 112 | $di->set('db', function(){ 113 | require 'unit-tests/config.db.php'; 114 | return new Phalcon\Db\Adapter\Pdo\Sqlite($configSqlite); 115 | }); 116 | 117 | return $di; 118 | } 119 | 120 | protected function _testCacheDefaultDI($di) 121 | { 122 | 123 | $di->set('modelsCache', function(){ 124 | $frontCache = new Phalcon\Cache\Frontend\Data(); 125 | return new Phalcon\Cache\Backend\File($frontCache, array( 126 | 'cacheDir' => 'unit-tests/cache/' 127 | )); 128 | }, true); 129 | 130 | $robots = Robots::find(array( 131 | 'cache' => array('key' => 'some'), 132 | 'order' => 'id' 133 | )); 134 | $this->assertEquals(count($robots), 3); 135 | $this->assertTrue($robots->isFresh()); 136 | 137 | $robots = Robots::find(array( 138 | 'cache' => array('key' => 'some'), 139 | 'order' => 'id' 140 | )); 141 | $this->assertEquals(count($robots), 3); 142 | $this->assertFalse($robots->isFresh()); 143 | 144 | } 145 | 146 | protected function _testCacheDefaultDIBindings($di) 147 | { 148 | 149 | $di->set('modelsCache', function(){ 150 | $frontCache = new Phalcon\Cache\Frontend\Data(); 151 | return new Phalcon\Cache\Backend\File($frontCache, array( 152 | 'cacheDir' => 'unit-tests/cache/' 153 | )); 154 | }, true); 155 | 156 | $robots = Robots::find(array( 157 | 'cache' => array('key' => 'some'), 158 | 'conditions' => 'id > :id1: and id < :id2:', 159 | 'bind' => array('id1' => 0, 'id2' => 4), 160 | 'order' => 'id' 161 | )); 162 | $this->assertEquals(count($robots), 3); 163 | $this->assertTrue($robots->isFresh()); 164 | 165 | $robots = Robots::find(array( 166 | 'cache' => array('key' => 'some'), 167 | 'conditions' => 'id > :id1: and id < :id2:', 168 | 'bind' => array('id1' => 0, 'id2' => 4), 169 | 'order' => 'id' 170 | )); 171 | $this->assertEquals(count($robots), 3); 172 | $this->assertFalse($robots->isFresh()); 173 | 174 | } 175 | 176 | protected function _testCacheOtherService($di) 177 | { 178 | 179 | $di->set('otherCache', function(){ 180 | $frontCache = new Phalcon\Cache\Frontend\Data(); 181 | return new Phalcon\Cache\Backend\File($frontCache, array( 182 | 'cacheDir' => 'unit-tests/cache/' 183 | )); 184 | }, true); 185 | 186 | $robots = Robots::find(array( 187 | 'cache' => array( 188 | 'key' => 'other-some', 189 | 'lifetime' => 60, 190 | 'service' => 'otherCache' 191 | ), 192 | 'order' => 'id' 193 | )); 194 | $this->assertEquals(count($robots), 3); 195 | $this->assertTrue($robots->isFresh()); 196 | 197 | $robots = Robots::find(array( 198 | 'cache' => array( 199 | 'key' => 'other-some', 200 | 'lifetime' => 60, 201 | 'service' => 'otherCache' 202 | ), 203 | 'order' => 'id' 204 | )); 205 | $this->assertEquals(count($robots), 3); 206 | $this->assertFalse($robots->isFresh()); 207 | 208 | $this->assertEquals($robots->getCache()->getLastKey(), 'other-some'); 209 | 210 | $this->assertEquals($robots->getCache()->queryKeys(), array( 211 | 0 => 'other-some', 212 | )); 213 | } 214 | 215 | public function testCacheDefaultDIMysql() 216 | { 217 | $di = $this->_prepareTestMysql(); 218 | if ($di) { 219 | $this->_testCacheDefaultDI($di); 220 | } 221 | else { 222 | $this->markTestSkipped("Skipped"); 223 | } 224 | } 225 | 226 | public function testCacheDefaultDIPostgresql() 227 | { 228 | $di = $this->_prepareTestPostgresql(); 229 | if ($di) { 230 | $this->_testCacheDefaultDI($di); 231 | } 232 | else { 233 | $this->markTestSkipped("Skipped"); 234 | } 235 | } 236 | 237 | public function testCacheDefaultDISqlite() 238 | { 239 | $di = $this->_prepareTestSqlite(); 240 | if ($di) { 241 | $this->_testCacheDefaultDI($di); 242 | } 243 | else { 244 | $this->markTestSkipped("Skipped"); 245 | } 246 | } 247 | 248 | public function testCacheDefaultDIBindingsMysql() 249 | { 250 | $di = $this->_prepareTestMysql(); 251 | if ($di) { 252 | $this->_testCacheDefaultDIBindings($di); 253 | } 254 | else { 255 | $this->markTestSkipped("Skipped"); 256 | } 257 | } 258 | 259 | public function testCacheDefaultDIBindingsPostgresql() 260 | { 261 | $di = $this->_prepareTestPostgresql(); 262 | if ($di) { 263 | $this->_testCacheDefaultDIBindings($di); 264 | } 265 | else { 266 | $this->markTestSkipped("Skipped"); 267 | } 268 | } 269 | 270 | public function testCacheDefaultDIBindingsSqlite() 271 | { 272 | $di = $this->_prepareTestSqlite(); 273 | if ($di) { 274 | $this->_testCacheDefaultDIBindings($di); 275 | } 276 | else { 277 | $this->markTestSkipped("Skipped"); 278 | } 279 | } 280 | 281 | public function testCacheOtherServiceMysql() 282 | { 283 | $di = $this->_prepareTestMysql(); 284 | if ($di) { 285 | $this->_testCacheOtherService($di); 286 | } 287 | else { 288 | $this->markTestSkipped("Skipped"); 289 | } 290 | } 291 | 292 | public function testCacheOtherServicePostgresql() 293 | { 294 | $di = $this->_prepareTestPostgresql(); 295 | if ($di) { 296 | $robots = $this->_testCacheOtherService($di); 297 | } 298 | else { 299 | $this->markTestSkipped("Skipped"); 300 | } 301 | } 302 | 303 | public function testCacheOtherServiceSqlite() 304 | { 305 | $di = $this->_prepareTestSqlite(); 306 | if ($di) { 307 | $robots = $this->_testCacheOtherService($di); 308 | } 309 | else { 310 | $this->markTestSkipped("Skipped"); 311 | } 312 | } 313 | 314 | } 315 | -------------------------------------------------------------------------------- /unit-tests/ModelsResultsetTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | include "loader.php"; 22 | class ModelsResultsetTest extends PHPUnit_Framework_TestCase 23 | { 24 | 25 | public function __construct() 26 | { 27 | spl_autoload_register(array($this, 'modelsAutoloader')); 28 | } 29 | 30 | public function __destruct() 31 | { 32 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 33 | } 34 | 35 | public function modelsAutoloader($className) 36 | { 37 | if (file_exists('unit-tests/models/'.$className.'.php')) { 38 | require 'unit-tests/models/'.$className.'.php'; 39 | } 40 | } 41 | 42 | protected function _getDI() 43 | { 44 | 45 | Phalcon\DI::reset(); 46 | 47 | $di = new Phalcon\DI(); 48 | 49 | $di->set('modelsManager', function(){ 50 | return new Phalcon\Mvc\Model\Manager(); 51 | }); 52 | 53 | $di->set('modelsMetadata', function(){ 54 | return new Phalcon\Mvc\Model\Metadata\Memory(); 55 | }); 56 | 57 | return $di; 58 | } 59 | 60 | protected function _prepareTestMssql() 61 | { 62 | require 'unit-tests/config.db.php'; 63 | if (empty($configMssql)) { 64 | return false; 65 | } 66 | 67 | $di = $this->_getDI(); 68 | 69 | $di->set('db', function(){ 70 | require 'unit-tests/config.db.php'; 71 | return new Twm\Db\Adapter\Pdo\Mssql($configMssql); 72 | }); 73 | 74 | return true; 75 | } 76 | public function testResultsetNormalMssql() 77 | { 78 | if (!$this->_prepareTestMssql()) { 79 | $this->markTestSkipped("Skipped"); 80 | return; 81 | } 82 | 83 | $robots = Robots::find(array( 84 | 'order' => 'id' 85 | )); 86 | 87 | $this->_applyTests($robots); 88 | } 89 | 90 | public function testResultsetBindingMssql() 91 | { 92 | if (!$this->_prepareTestMssql()) { 93 | $this->markTestSkipped("Skipped"); 94 | return; 95 | } 96 | 97 | $initialId = 0; 98 | $finalId = 4; 99 | 100 | $robots = Robots::find(array( 101 | 'conditions' => 'id > :id1: and id < :id2:', 102 | 'bind' => array('id1' => $initialId, 'id2' => $finalId), 103 | 'order' => 'id' 104 | )); 105 | 106 | $this->_applyTests($robots); 107 | } 108 | 109 | /* 110 | public function testResultsetNormalMysql() 111 | { 112 | if (!$this->_prepareTestMysql()) { 113 | $this->markTestSkipped("Skipped"); 114 | return; 115 | } 116 | 117 | $robots = Robots::find(array( 118 | 'order' => 'id' 119 | )); 120 | 121 | $this->_applyTests($robots); 122 | } 123 | 124 | public function testResultsetBindingMysql() 125 | { 126 | if (!$this->_prepareTestMysql()) { 127 | $this->markTestSkipped("Skipped"); 128 | return; 129 | } 130 | 131 | $initialId = 0; 132 | $finalId = 4; 133 | 134 | $robots = Robots::find(array( 135 | 'conditions' => 'id > :id1: and id < :id2:', 136 | 'bind' => array('id1' => $initialId, 'id2' => $finalId), 137 | 'order' => 'id' 138 | )); 139 | 140 | $this->_applyTests($robots); 141 | } 142 | 143 | public function testResultsetNormalPostgresql() 144 | { 145 | if (!$this->_prepareTestPostgresql()) { 146 | $this->markTestSkipped("Skipped"); 147 | return; 148 | } 149 | 150 | $robots = Robots::find(array('order' => 'id')); 151 | 152 | $this->_applyTests($robots); 153 | } 154 | 155 | public function testResultsetBindingPostgresql() 156 | { 157 | if (!$this->_prepareTestPostgresql()) { 158 | $this->markTestSkipped("Skipped"); 159 | return; 160 | } 161 | 162 | $initialId = 0; 163 | $finalId = 4; 164 | 165 | $robots = Robots::find(array( 166 | 'conditions' => 'id > :id1: and id < :id2:', 167 | 'bind' => array('id1' => $initialId, 'id2' => $finalId), 168 | 'order' => 'id' 169 | )); 170 | 171 | $this->_applyTests($robots); 172 | } 173 | 174 | public function testResultsetNormalSqlite() 175 | { 176 | if (!$this->_prepareTestSqlite()) { 177 | $this->markTestSkipped("Skipped"); 178 | return; 179 | } 180 | 181 | $robots = Robots::find(array('order' => 'id')); 182 | 183 | $this->_applyTests($robots); 184 | } 185 | 186 | public function testResultsetBindingSqlite() 187 | { 188 | if (!$this->_prepareTestSqlite()) { 189 | $this->markTestSkipped("Skipped"); 190 | return; 191 | } 192 | 193 | $initialId = 0; 194 | $finalId = 4; 195 | 196 | $robots = Robots::find(array( 197 | 'conditions' => 'id > :id1: and id < :id2:', 198 | 'bind' => array('id1' => $initialId, 'id2' => $finalId), 199 | 'order' => 'id' 200 | )); 201 | 202 | $this->_applyTests($robots); 203 | } 204 | */ 205 | public function _applyTests($robots) 206 | { 207 | 208 | $this->assertEquals(count($robots), 3); 209 | $this->assertEquals($robots->count(), 3); 210 | 211 | //Using a foreach 212 | $number = 0; 213 | foreach ($robots as $robot) { 214 | $this->assertEquals($robot->id, $number+1); 215 | $number++; 216 | } 217 | $this->assertEquals($number, 3); 218 | 219 | //Using a while 220 | $number = 0; 221 | $robots->rewind(); 222 | while ($robots->valid()) { 223 | $robot = $robots->current(); 224 | $this->assertEquals($robot->id, $number+1); 225 | $robots->next(); 226 | $number++; 227 | } 228 | $this->assertEquals($number, 3); 229 | 230 | $robots->seek(1); 231 | $robots->valid(); 232 | $robot = $robots->current(); 233 | $this->assertEquals($robot->id, 2); 234 | 235 | $robot = $robots->getFirst(); 236 | $this->assertEquals($robot->id, 1); 237 | 238 | $robot = $robots->getLast(); 239 | $this->assertEquals($robot->id, 3); 240 | 241 | $robot = $robots[0]; 242 | $this->assertEquals($robot->id, 1); 243 | 244 | $robot = $robots[2]; 245 | $this->assertEquals($robot->id, 3); 246 | 247 | $this->assertFalse(isset($robots[4])); 248 | 249 | $filtered = $robots->filter(function($robot){ 250 | if ($robot->id < 3) { 251 | return $robot; 252 | } 253 | }); 254 | 255 | $this->assertEquals(count($filtered), 2); 256 | $this->assertEquals($filtered[0]->id, 1); 257 | $this->assertEquals($filtered[1]->id, 2); 258 | 259 | } 260 | 261 | public function _applyTestsBig($personas) 262 | { 263 | 264 | $this->assertEquals(count($personas), 33); 265 | $this->assertEquals($personas->count(), 33); 266 | 267 | //Using a foreach 268 | $number = 0; 269 | foreach ($personas as $persona) { 270 | $number++; 271 | } 272 | $this->assertEquals($number, 33); 273 | 274 | //Using a while 275 | $number = 0; 276 | $personas->rewind(); 277 | while ($personas->valid()) { 278 | $persona = $personas->current(); 279 | $personas->next(); 280 | $number++; 281 | } 282 | $this->assertEquals($number, 33); 283 | 284 | $personas->seek(1); 285 | $personas->valid(); 286 | $persona = $personas->current(); 287 | $this->assertEquals(get_class($persona), 'Personas'); 288 | 289 | $persona = $personas->getFirst(); 290 | $this->assertEquals(get_class($persona), 'Personas'); 291 | 292 | $persona = $personas->getLast(); 293 | $this->assertEquals(get_class($persona), 'Personas'); 294 | 295 | $persona = $personas[0]; 296 | $this->assertEquals(get_class($persona), 'Personas'); 297 | 298 | $persona = $personas[2]; 299 | $this->assertEquals(get_class($persona), 'Personas'); 300 | 301 | $this->assertFalse(isset($personas[40])); 302 | 303 | } 304 | public function testSerializeNormalMssql() 305 | { 306 | require 'unit-tests/config.db.php'; 307 | if (empty($configMysql)) { 308 | $this->markTestSkipped("Skipped"); 309 | return; 310 | } 311 | 312 | $this->_prepareTestMssql(); 313 | 314 | $data = serialize(Robots::find(array('order' => 'id'))); 315 | 316 | $robots = unserialize($data); 317 | 318 | $this->assertEquals(get_class($robots), 'Phalcon\Mvc\Model\Resultset\Simple'); 319 | 320 | $this->_applyTests($robots); 321 | 322 | } 323 | 324 | public function testSerializeBindingsMssql() 325 | { 326 | if (!$this->_prepareTestMssql()) { 327 | $this->markTestSkipped("Skipped"); 328 | return; 329 | } 330 | 331 | $initialId = 0; 332 | $finalId = 4; 333 | 334 | $data = serialize(Robots::find(array( 335 | 'conditions' => 'id > :id1: and id < :id2:', 336 | 'bind' => array('id1' => $initialId, 'id2' => $finalId), 337 | 'order' => 'id' 338 | ))); 339 | 340 | $robots = unserialize($data); 341 | 342 | $this->assertEquals(get_class($robots), 'Phalcon\Mvc\Model\Resultset\Simple'); 343 | 344 | $this->_applyTests($robots); 345 | 346 | } 347 | 348 | public function testSerializeBigMssql() 349 | { 350 | if (!$this->_prepareTestMssql()) { 351 | $this->markTestSkipped("Skipped"); 352 | return; 353 | } 354 | 355 | $data = serialize(Personas::find(array( 356 | 'limit' => 33 357 | ))); 358 | 359 | $personas = unserialize($data); 360 | 361 | $this->assertEquals(get_class($personas), 'Phalcon\Mvc\Model\Resultset\Simple'); 362 | 363 | $this->_applyTestsBig($personas); 364 | 365 | } 366 | 367 | public function testResultsetNormalZero() 368 | { 369 | if (!$this->_prepareTestMssql()) { 370 | $this->markTestSkipped("Skipped"); 371 | return; 372 | } 373 | 374 | $robots = Robots::find('id > 1000'); 375 | 376 | $this->assertEquals(count($robots), 0); 377 | $this->assertEquals($robots->count(), 0); 378 | 379 | //Using a foreach 380 | $number = 0; 381 | foreach ($robots as $robot) { 382 | $number++; 383 | } 384 | $this->assertEquals($number, 0); 385 | 386 | //Using a while 387 | $number = 0; 388 | $robots->rewind(); 389 | while ($robots->valid()) { 390 | $robots->next(); 391 | $number++; 392 | } 393 | $this->assertEquals($number, 0); 394 | 395 | $robots->seek(1); 396 | $robots->valid(); 397 | $robot = $robots->current(); 398 | $this->assertFalse($robot); 399 | 400 | $robot = $robots->getFirst(); 401 | $this->assertFalse($robot); 402 | 403 | $robot = $robots->getLast(); 404 | $this->assertFalse($robot); 405 | 406 | try { 407 | $robot = $robots[0]; 408 | $this->assertFalse(true); 409 | } 410 | catch(Exception $e){ 411 | $this->assertEquals($e->getMessage(), 'The index does not exist in the cursor'); 412 | } 413 | 414 | try { 415 | $robot = $robots[2]; 416 | $this->assertFalse(true); 417 | } 418 | catch(Exception $e){ 419 | $this->assertEquals($e->getMessage(), 'The index does not exist in the cursor'); 420 | } 421 | 422 | $this->assertFalse(isset($robots[0])); 423 | } 424 | 425 | } 426 | -------------------------------------------------------------------------------- /unit-tests/ModelsSerializeTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | require "loader.php"; 22 | class ModelsSerializeTest extends PHPUnit_Framework_TestCase 23 | { 24 | 25 | public function __construct() 26 | { 27 | spl_autoload_register(array($this, 'modelsAutoloader')); 28 | } 29 | 30 | public function __destruct() 31 | { 32 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 33 | } 34 | 35 | public function modelsAutoloader($className) 36 | { 37 | $className = str_replace('\\', '/', $className); 38 | if (file_exists('unit-tests/models/'.$className.'.php')) { 39 | require 'unit-tests/models/'.$className.'.php'; 40 | } 41 | } 42 | 43 | protected function _prepareDI() 44 | { 45 | Phalcon\DI::reset(); 46 | 47 | $di = new Phalcon\DI(); 48 | 49 | $di->set('modelsManager', function() { 50 | return new Phalcon\Mvc\Model\Manager(); 51 | }, true); 52 | 53 | $di->set('modelsMetadata', function() { 54 | return new Phalcon\Mvc\Model\Metadata\Memory(); 55 | }, true); 56 | 57 | $di->set('db', function() { 58 | require 'unit-tests/config.db.php'; 59 | return new Twm\Db\Adapter\Pdo\Mssql($configMssql); 60 | }, true); 61 | 62 | } 63 | 64 | public function testSerialize() 65 | { 66 | 67 | $this->_prepareDI(); 68 | 69 | $robot = Robots::findFirst(); 70 | 71 | $serialized = serialize($robot); 72 | $robot = unserialize($serialized); 73 | 74 | $this->assertTrue($robot->save()); 75 | 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /unit-tests/ModelsSnapshotsTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | require "loader.php"; 22 | class ModelsSnapshotsTest extends PHPUnit_Framework_TestCase 23 | { 24 | 25 | public function __construct() 26 | { 27 | spl_autoload_register(array($this, 'modelsAutoloader')); 28 | } 29 | 30 | public function __destruct() 31 | { 32 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 33 | } 34 | 35 | public function modelsAutoloader($className) 36 | { 37 | $className = str_replace('\\', '/', $className); 38 | if (file_exists('unit-tests/models/' . $className . '.php')) { 39 | require 'unit-tests/models/' . $className . '.php'; 40 | } 41 | } 42 | 43 | protected function _getDI() 44 | { 45 | 46 | Phalcon\DI::reset(); 47 | 48 | $di = new Phalcon\DI(); 49 | 50 | $di->set('modelsManager', function(){ 51 | return new Phalcon\Mvc\Model\Manager(); 52 | }, true); 53 | 54 | $di->set('modelsMetadata', function(){ 55 | return new Phalcon\Mvc\Model\Metadata\Memory(); 56 | }, true); 57 | 58 | return $di; 59 | } 60 | 61 | public function testModelsMysql() 62 | { 63 | 64 | $di = $this->_getDI(); 65 | 66 | $di->set('db', function(){ 67 | require 'unit-tests/config.db.php'; 68 | return new Twm\Db\Adapter\Pdo\Mssql($configMssql); 69 | }); 70 | 71 | $this->_executeTestsNormal($di); 72 | $this->_executeTestsRenamed($di); 73 | $this->_executeTestsNormalComplex($di); 74 | } 75 | 76 | protected function _executeTestsNormal($di) 77 | { 78 | 79 | $snapshots = array( 80 | 1 => array( 81 | 'id' => '1', 82 | 'name' => 'Robotina', 83 | 'type' => 'mechanical', 84 | 'year' => '1972', 85 | ), 86 | 2 => array( 87 | 'id' => '2', 88 | 'name' => 'Astro Boy', 89 | 'type' => 'mechanical', 90 | 'year' => '1952', 91 | ), 92 | 3 => array( 93 | 'id' => '3', 94 | 'name' => 'Terminator', 95 | 'type' => 'cyborg', 96 | 'year' => '2029', 97 | ) 98 | ); 99 | 100 | foreach (Snapshot\Robots::find(array('order' => 'id')) as $robot) { 101 | $this->assertTrue($robot->hasSnapshotData()); 102 | $this->assertEquals($snapshots[$robot->id], $robot->getSnapshotData()); 103 | } 104 | 105 | foreach (Snapshot\Robots::find(array('order' => 'id')) as $robot) { 106 | $robot->name = 'Some'; 107 | $robot->year = 1999; 108 | $this->assertTrue($robot->hasChanged('name')); 109 | $this->assertTrue($robot->hasChanged('year')); 110 | $this->assertFalse($robot->hasChanged('type')); 111 | $this->assertTrue($robot->hasChanged()); 112 | } 113 | 114 | foreach (Snapshot\Robots::find(array('order' => 'id')) as $robot) { 115 | $robot->year = $robot->year; 116 | $this->assertFalse($robot->hasChanged('year')); 117 | $this->assertFalse($robot->hasChanged()); 118 | } 119 | 120 | foreach (Snapshot\Robots::find(array('order' => 'id')) as $robot) { 121 | $robot->name = 'Little'; 122 | $robot->year = 2005; 123 | $this->assertEquals($robot->getChangedFields(), array('name', 'year')); 124 | } 125 | } 126 | 127 | protected function _executeTestsRenamed($di) 128 | { 129 | $snapshots = array( 130 | 1 => array( 131 | 'code' => '1', 132 | 'theName' => 'Robotina', 133 | 'theType' => 'mechanical', 134 | 'theYear' => '1972', 135 | ), 136 | 2 => array( 137 | 'code' => '2', 138 | 'theName' => 'Astro Boy', 139 | 'theType' => 'mechanical', 140 | 'theYear' => '1952', 141 | ), 142 | 3 => array( 143 | 'code' => '3', 144 | 'theName' => 'Terminator', 145 | 'theType' => 'cyborg', 146 | 'theYear' => '2029', 147 | ) 148 | ); 149 | 150 | foreach (Snapshot\Robotters::find(array('order' => 'code')) as $robot) { 151 | $this->assertTrue($robot->hasSnapshotData()); 152 | $this->assertEquals($snapshots[$robot->code], $robot->getSnapshotData()); 153 | } 154 | 155 | foreach (Snapshot\Robotters::find(array('order' => 'code')) as $robot) { 156 | $robot->theName = 'Some'; 157 | $robot->theYear = 1999; 158 | $this->assertTrue($robot->hasChanged('theName')); 159 | $this->assertTrue($robot->hasChanged('theYear')); 160 | $this->assertFalse($robot->hasChanged('theType')); 161 | $this->assertTrue($robot->hasChanged()); 162 | } 163 | 164 | foreach (Snapshot\Robotters::find(array('order' => 'code')) as $robot) { 165 | $robot->theYear = $robot->theYear; 166 | $this->assertFalse($robot->hasChanged('theYear')); 167 | $this->assertFalse($robot->hasChanged()); 168 | } 169 | 170 | foreach (Snapshot\Robotters::find(array('order' => 'code')) as $robot) { 171 | $robot->theName = 'Little'; 172 | $robot->theYear = 2005; 173 | $this->assertEquals($robot->getChangedFields(), array('theName', 'theYear')); 174 | } 175 | } 176 | 177 | protected function _executeTestsNormalComplex($di) 178 | { 179 | 180 | $robots = $di['modelsManager']->executeQuery('SELECT * FROM Snapshot\Robots'); 181 | foreach ($robots as $robot) { 182 | $robot->name = 'Some'; 183 | $robot->year = 1999; 184 | $this->assertTrue($robot->hasChanged('name')); 185 | $this->assertTrue($robot->hasChanged('year')); 186 | $this->assertFalse($robot->hasChanged('type')); 187 | $this->assertTrue($robot->hasChanged()); 188 | $this->assertEquals($robot->getChangedFields(), array('name', 'year')); 189 | } 190 | 191 | $robots = $di['modelsManager']->executeQuery('SELECT robot.*, parts.* FROM Snapshot\Robots robot JOIN Snapshot\RobotsParts parts'); 192 | foreach ($robots as $row) { 193 | 194 | $row->robot->name = 'Some'; 195 | $row->robot->year = 1999; 196 | 197 | $this->assertTrue($row->robot->hasChanged('name')); 198 | $this->assertTrue($row->robot->hasChanged('year')); 199 | $this->assertFalse($row->robot->hasChanged('type')); 200 | $this->assertTrue($row->robot->hasChanged()); 201 | $this->assertEquals($row->robot->getChangedFields(), array('name', 'year')); 202 | 203 | $this->assertFalse($row->parts->hasSnapshotData()); 204 | } 205 | } 206 | 207 | } 208 | -------------------------------------------------------------------------------- /unit-tests/ModelsTransactionsTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | class ModelsTransactionsTest extends PHPUnit_Framework_TestCase { 22 | 23 | public function __construct() 24 | { 25 | spl_autoload_register(array($this, 'modelsAutoloader')); 26 | } 27 | 28 | public function __destruct() 29 | { 30 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 31 | } 32 | 33 | public function modelsAutoloader($className) 34 | { 35 | if (file_exists('unit-tests/models/'.$className.'.php')) { 36 | require 'unit-tests/models/'.$className.'.php'; 37 | } 38 | } 39 | 40 | protected function _getDI() 41 | { 42 | 43 | Phalcon\DI::reset(); 44 | 45 | $di = new Phalcon\DI(); 46 | 47 | $di->set('modelsManager', function(){ 48 | return new Phalcon\Mvc\Model\Manager(); 49 | }); 50 | 51 | $di->set('modelsMetadata', function(){ 52 | return new Phalcon\Mvc\Model\Metadata\Memory(); 53 | }); 54 | 55 | $di->set('transactionManager', function(){ 56 | return new Phalcon\Mvc\Model\Transaction\Manager(); 57 | }); 58 | 59 | return $di; 60 | } 61 | 62 | public function testTransactionsMysql() 63 | { 64 | require 'unit-tests/config.db.php'; 65 | if (empty($configMysql)) { 66 | $this->markTestSkipped("Skipped"); 67 | return; 68 | } 69 | 70 | $di = $this->_getDI(); 71 | 72 | $di->set('db', function(){ 73 | require 'unit-tests/config.db.php'; 74 | return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 75 | }); 76 | 77 | $this->_executeTests($di); 78 | } 79 | 80 | public function testTransactionsPostgresql() 81 | { 82 | require 'unit-tests/config.db.php'; 83 | if (empty($configPostgresql)) { 84 | $this->markTestSkipped("Skipped"); 85 | return; 86 | } 87 | 88 | $di = $this->_getDI(); 89 | 90 | $di->set('db', function(){ 91 | require 'unit-tests/config.db.php'; 92 | return new Phalcon\Db\Adapter\Pdo\Postgresql($configPostgresql); 93 | }); 94 | 95 | $this->_executeTests($di); 96 | } 97 | 98 | public function testTransactionsSqlite() 99 | { 100 | require 'unit-tests/config.db.php'; 101 | if (empty($configSqlite)) { 102 | $this->markTestSkipped("Skipped"); 103 | return; 104 | } 105 | 106 | $di = $this->_getDI(); 107 | 108 | $di->set('db', function(){ 109 | require 'unit-tests/config.db.php'; 110 | return new Phalcon\Db\Adapter\Pdo\Sqlite($configSqlite); 111 | }); 112 | 113 | $this->_executeTests($di); 114 | } 115 | 116 | protected function _executeTests($di) 117 | { 118 | 119 | $connection = $di->getShared('db'); 120 | 121 | $success = $connection->delete("personas", "cedula LIKE 'T-Cx%'"); 122 | $this->assertTrue($success); 123 | 124 | $numPersonas = Personas::count(); 125 | $this->assertGreaterThan(0, $numPersonas); 126 | 127 | $transactionManager = $di->getShared('transactionManager'); 128 | 129 | try { 130 | 131 | $transaction1 = $transactionManager->get(); 132 | $this->assertInstanceOf('Phalcon\Mvc\Model\Transaction', $transaction1); 133 | 134 | $this->assertNotEquals($transaction1->getConnection()->getConnectionId(), $connection->getConnectionId()); 135 | 136 | $p = 100; 137 | for ($i=0; $i<10; $i++) { 138 | $persona = new Personas($di); 139 | $persona->setTransaction($transaction1); 140 | $persona->cedula = 'T-Cx'.$i; 141 | $persona->tipo_documento_id = 1; 142 | $persona->nombres = 'LOST LOST'; 143 | $persona->telefono = '2'; 144 | $persona->cupo = 0; 145 | $persona->estado = 'A'; 146 | $this->assertTrue($persona->save()); 147 | $p++; 148 | } 149 | 150 | //We rollback the transaction 151 | $transaction1->rollback(); 152 | 153 | $this->assertTrue(FALSE, 'oh, Why?'); 154 | 155 | } 156 | catch(Phalcon\Mvc\Model\Transaction\Failed $e){ 157 | $this->assertTrue(true); 158 | } 159 | 160 | //Now we check if the records was correctly rolledbacked 161 | $rollbackNumPersonas = Personas::count(); 162 | $this->assertEquals($numPersonas, $rollbackNumPersonas); 163 | 164 | //Creating another transaction 165 | try { 166 | 167 | $transaction2 = $transactionManager->get(); 168 | $this->assertInstanceOf('Phalcon\Mvc\Model\Transaction', $transaction2); 169 | 170 | $this->assertNotEquals($transaction2->getConnection()->getConnectionId(), $connection->getConnectionId()); 171 | $this->assertNotEquals($transaction1->getConnection()->getConnectionId(), $transaction2->getConnection()->getConnectionId()); 172 | 173 | $p = 200; 174 | for ($i=0; $i<15; $i++) { 175 | $persona = new Personas($di); 176 | $persona->setTransaction($transaction2); 177 | $persona->cedula = 'T-Cx'.$p; 178 | $persona->tipo_documento_id = 1; 179 | $persona->nombres = 'LOST LOST'; 180 | $persona->telefono = '1'; 181 | $persona->cupo = 0; 182 | $persona->estado = 'A'; 183 | $this->assertTrue($persona->save()); 184 | $p++; 185 | } 186 | 187 | //This time we commit the transaction 188 | $transaction2->commit(); 189 | 190 | $commitNumPersonas = Personas::count(); 191 | $this->assertEquals($commitNumPersonas, $numPersonas+15); 192 | 193 | } 194 | catch(Phalcon\Mvc\Model\Transaction\Failed $e){ 195 | $this->assertTrue(FALSE, 'oh, Why?'); 196 | } 197 | 198 | } 199 | 200 | } 201 | 202 | -------------------------------------------------------------------------------- /unit-tests/PlaygroundTest.php: -------------------------------------------------------------------------------- 1 | | 17 | | Eduar Carvajal | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | use Phalcon\Mvc\Model\Query as Query; 22 | include "loader.php"; 23 | 24 | class PlaygroundTest extends PHPUnit_Framework_TestCase 25 | { 26 | 27 | public function __construct() 28 | { 29 | spl_autoload_register(array($this, 'modelsAutoloader')); 30 | } 31 | 32 | public function __destruct() 33 | { 34 | spl_autoload_unregister(array($this, 'modelsAutoloader')); 35 | } 36 | 37 | public function modelsAutoloader($className) 38 | { 39 | $className = str_replace("\\", DIRECTORY_SEPARATOR, $className); 40 | $path = 'unit-tests/models/'.$className.'.php'; 41 | if (file_exists($path)) { 42 | require $path; 43 | } 44 | } 45 | 46 | protected function _getDI() 47 | { 48 | 49 | Phalcon\DI::reset(); 50 | 51 | $di = new Phalcon\DI(); 52 | 53 | $di->set('modelsManager', function(){ 54 | return new Phalcon\Mvc\Model\Manager(); 55 | }); 56 | 57 | $di->set('modelsMetadata', function(){ 58 | return new Phalcon\Mvc\Model\Metadata\Memory(); 59 | }); 60 | 61 | $di->set('db', function(){ 62 | require 'unit-tests/config.db.php'; 63 | //return new Twm\Db\Adapter\Pdo\Mssql($configMssql); 64 | $connection = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); 65 | 66 | 67 | $eventsManager = new Phalcon\Events\Manager(); 68 | 69 | 70 | //Listen all the database events 71 | $eventsManager->attach('db', function($event, $connection){ 72 | if ($event->getType() == 'beforeQuery') { 73 | echo ($connection->getSQLStatement()); 74 | } 75 | }); 76 | 77 | 78 | //Assign the eventsManager to the db adapter instance 79 | $connection->setEventsManager($eventsManager); 80 | return $connection; 81 | }); 82 | 83 | return $di; 84 | } 85 | 86 | public function testSelectParsing() 87 | { 88 | $di = $this->_getDI(); 89 | // $di->get('db')->execute('select 1'); 90 | /* 91 | $robots = Robots::query()->forUpdate(true)->execute(); 92 | return; 93 | $robots = Robots::query() 94 | ->where("type = :type:") 95 | ->andWhere("year < 2000") 96 | ->sharedLock(true) 97 | ->bind(array("type" => "mechanical")) 98 | ->order("name") 99 | ->execute(); 100 | */ 101 | 102 | $personnes = Personnes::find(array( 103 | "conditions" => "cedula >=:d1:", 104 | "bind" => array("d1" => '1'), 105 | "shared_lock" => true, 106 | "for_update" => true, 107 | /* "order" => "cedula, nombres", 108 | 'limit' => array( 109 | 'offset' => 1, 110 | 'number' => 2 111 | )*/) 112 | ); 113 | 114 | /* 115 | $builder = $di['modelsManager']->createBuilder() 116 | ->columns('cedula, nombres') 117 | ->from('Personnes') 118 | ->orderBy('cedula'); 119 | //->limit(500, 0); 120 | 121 | $paginator = new Phalcon\Paginator\Adapter\QueryBuilder(array( 122 | "builder" => $builder, 123 | "limit"=> 10, 124 | "page" => 2 125 | )); 126 | 127 | $page = $paginator->getPaginate(); 128 | 129 | */ 130 | } 131 | 132 | } 133 | -------------------------------------------------------------------------------- /unit-tests/cache/map-robots.php: -------------------------------------------------------------------------------- 1 | NULL, 3 | 1 => NULL, 4 | ); -------------------------------------------------------------------------------- /unit-tests/cache/meta-robots-robots.php: -------------------------------------------------------------------------------- 1 | 3 | array ( 4 | 0 => 'id', 5 | 1 => 'name', 6 | 2 => 'type', 7 | 3 => 'year', 8 | ), 9 | 1 => 10 | array ( 11 | 0 => 'id', 12 | ), 13 | 2 => 14 | array ( 15 | 0 => 'name', 16 | 1 => 'type', 17 | 2 => 'year', 18 | ), 19 | 3 => 20 | array ( 21 | 0 => 'id', 22 | 1 => 'name', 23 | 2 => 'type', 24 | 3 => 'year', 25 | ), 26 | 4 => 27 | array ( 28 | 'id' => 0, 29 | 'name' => 2, 30 | 'type' => 2, 31 | 'year' => 0, 32 | ), 33 | 5 => 34 | array ( 35 | 'id' => true, 36 | 'year' => true, 37 | ), 38 | 8 => 'id', 39 | 9 => 40 | array ( 41 | 'id' => 1, 42 | 'name' => 2, 43 | 'type' => 2, 44 | 'year' => 1, 45 | ), 46 | 10 => 47 | array ( 48 | ), 49 | 11 => 50 | array ( 51 | ), 52 | ); -------------------------------------------------------------------------------- /unit-tests/config.db.local.php: -------------------------------------------------------------------------------- 1 | 'localhost', 4 | 'username' => 'mc2', 5 | 'password' => 'ok1234', 6 | 'dbname' => 'phalcon_test' 7 | ); 8 | 9 | $configPostgresql = array( 10 | 'host' => '127.0.0.1', 11 | 'username' => 'postgres', 12 | 'password' => '', 13 | 'dbname' => 'phalcon_test', 14 | 'schema' => 'public' 15 | ); 16 | 17 | $configSqlite = array( 18 | 'dbname' => '/tmp/phalcon_test.sqlite', 19 | ); 20 | 21 | 22 | $configMssql = array( 23 | 'host' => 'McDev', 24 | 'username' => 'apnewmc', 25 | 'password' => 'Cm!2212@12', 26 | 'dbname' => 'CCCMCDEV2', 27 | 'dialectClass' => '\Twm\Db\Dialect\Mssql', 28 | 'pdoType' => 'dblib' 29 | ); 30 | -------------------------------------------------------------------------------- /unit-tests/config.db.php: -------------------------------------------------------------------------------- 1 | 'localhost', 6 | 'username' => 'root', 7 | 'password' => 'ok123456', 8 | 'dbname' => 'phalcon_test' 9 | ); 10 | 11 | $configPostgresql = array( 12 | 'host' => '127.0.0.1', 13 | 'username' => 'postgres', 14 | 'password' => '', 15 | 'dbname' => 'phalcon_test', 16 | 'schema' => 'public' 17 | ); 18 | 19 | $configSqlite = array( 20 | 'dbname' => '/tmp/phalcon_test.sqlite', 21 | ); 22 | 23 | $configMssql = array( 24 | 'host' => 'phalcon', 25 | 'username' => 'mssql', 26 | 'password' => '', 27 | 'dbname' => 'phalcon_test', 28 | 'dialectClass' => '\Twm\Db\Dialect\Mssql', 29 | 'pdoType' => 'dblib' 30 | ); 31 | } 32 | else { 33 | require 'unit-tests/config.db.local.php'; 34 | } 35 | -------------------------------------------------------------------------------- /unit-tests/db/test.php: -------------------------------------------------------------------------------- 1 | registerDirs( 7 | array('models/') 8 | ); 9 | 10 | //Register some namespaces 11 | $loader->registerNamespaces( 12 | array( 13 | "Twm\Db\Adapter\Pdo" => "adapter/", 14 | "Twm\Db\Dialect" => "dialect/" 15 | ) 16 | ); 17 | 18 | // register autoloader 19 | $loader->register(); 20 | 21 | echo '

connect

'; 22 | $mc = array( 23 | 'host' => 'McDev', 24 | 'username' => 'apnewmc', 25 | 'password' => 'Cm!2212@12', 26 | 'dbname' => 'CCCMCDEV1', 27 | 'dialectClass' => '\Twm\Db\Dialect\Mssql' 28 | 29 | ); 30 | $ec = array( 31 | 'host' => 'MSsql', 32 | 'username' => 'apedtuser2', 33 | 'password' => 'Ecg01dedt', 34 | 'dbname' => 'CCCECST2', 35 | 'dialectClass' => '\Twm\Db\Dialect\Mssql' 36 | ); 37 | $db = new \Twm\Db\Adapter\Pdo\Mssql($mc); 38 | if (!$db->connect()){ 39 | $db->close(); 40 | die('connection failed'); 41 | } 42 | 43 | //testModel($db); 44 | testQueryBinding($db); 45 | 46 | function testModel($db){ 47 | $boutique = new Boutique(); 48 | } 49 | 50 | function testQueryBinding($db){ 51 | echo '

execute query

'; 52 | $sqlStatement = "select * from tb_a_boutique_data where 1=':aaa' and 2=':bbb'"; 53 | $bindParams = array(':aaa'=>'1',':bbb'=>'2'); 54 | 55 | var_dump($db->query($sqlStatement, $bindParams)); 56 | } 57 | 58 | function testDescribeColumns(){ 59 | var_dump($db->describeColumns('tb_a_frist_data')); 60 | } 61 | -------------------------------------------------------------------------------- /unit-tests/helpers/xcache.php: -------------------------------------------------------------------------------- 1 | registerDirs( 7 | array( 8 | 'models/' 9 | ,'/var/www/html/phpunit-3.7/' 10 | ) 11 | ); 12 | 13 | //Register some namespaces 14 | $loader->registerNamespaces( 15 | array( 16 | "Twm\Db\Adapter\Pdo" => "unit-tests/db/adapter/", 17 | "Twm\Db\Dialect" => "unit-tests/db/dialect/" 18 | ) 19 | ); 20 | 21 | // register autoloader 22 | $loader->register(); 23 | 24 | -------------------------------------------------------------------------------- /unit-tests/models/Abonnes.php: -------------------------------------------------------------------------------- 1 | courrierElectronique == 'marina@hotmail.com') { 29 | $this->appendMessage(new Message('Désolé Marina, mais vous n\'êtes pas autorisé ici')); 30 | return false; 31 | } 32 | } 33 | 34 | public function beforeDelete() 35 | { 36 | if ($this->courrierElectronique == 'fuego@hotmail.com') { 37 | //Sorry this cannot be deleted 38 | $this->appendMessage(new Message('Désolé, ce ne peut pas être supprimé')); 39 | return false; 40 | } 41 | } 42 | 43 | public function validation() 44 | { 45 | 46 | $this->validate(new PresenceOfValidator(array( 47 | 'field' => 'creeA', 48 | 'message' => "La date de création est nécessaire" 49 | ))); 50 | 51 | $this->validate(new EmailValidator(array( 52 | 'field' => 'courrierElectronique', 53 | 'message' => 'Le courrier électronique est invalide' 54 | ))); 55 | 56 | $this->validate(new ExclusionInValidator(array( 57 | 'field' => 'statut', 58 | 'domain' => array('X', 'Z'), 59 | 'message' => 'L\'état ne doit être "X" ou "Z"' 60 | ))); 61 | 62 | $this->validate(new InclusionInValidator(array( 63 | 'field' => 'statut', 64 | 'domain' => array('P', 'I', 'w'), 65 | 'message' => 'L\'état doit être "P", "I" ou "w"' 66 | ))); 67 | 68 | $this->validate(new UniquenessValidator(array( 69 | 'field' => 'courrierElectronique', 70 | 'message' => 'Le courrier électronique doit être unique' 71 | ))); 72 | 73 | $this->validate(new RegexValidator(array( 74 | 'field' => 'statut', 75 | 'pattern' => '/[A-Z]/', 76 | 'message' => "L'état ne correspond pas à l'expression régulière" 77 | ))); 78 | 79 | $this->validate(new StringLengthValidator(array( 80 | 'field' => 'courrierElectronique', 81 | 'min' => '7', 82 | 'max' => '50', 83 | 'messageMinimum' => "Le courrier électronique est trop court", 84 | 'messageMaximum' => "Le courrier électronique est trop long" 85 | ))); 86 | 87 | if ($this->validationHasFailed() == true) { 88 | return false; 89 | } 90 | } 91 | 92 | public function columnMap() 93 | { 94 | return array( 95 | 'id' => 'code', 96 | 'email' => 'courrierElectronique', 97 | 'created_at' => 'creeA', 98 | 'status' => 'statut', 99 | ); 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /unit-tests/models/AlbumORama/Albums.php: -------------------------------------------------------------------------------- 1 | belongsTo('artists_id', 'AlbumORama\Artists', 'id', array( 11 | 'alias' => 'artist' 12 | )); 13 | $this->hasMany('id', 'AlbumORama\Songs', 'albums_id', array( 14 | 'alias' => 'songs' 15 | )); 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /unit-tests/models/AlbumORama/Artists.php: -------------------------------------------------------------------------------- 1 | hasMany('id', 'AlbumORama\Albums', 'artists_id', array( 11 | 'alias' => 'albums' 12 | )); 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /unit-tests/models/AlbumORama/Songs.php: -------------------------------------------------------------------------------- 1 | hasMany('id', 'AlbumORama\Albums', 'albums_id', array( 11 | 'alias' => 'album' 12 | )); 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /unit-tests/models/Boutique.php: -------------------------------------------------------------------------------- 1 | $key, 27 | 'lifetime' => 3600 28 | ); 29 | 30 | return $parameters; 31 | } 32 | 33 | public static function findFirst($parameters=null) 34 | { 35 | $parameters = self::getCacheableParams($parameters); 36 | return parent::findFirst($parameters); 37 | } 38 | 39 | public static function find($parameters=null) 40 | { 41 | $parameters = self::getCacheableParams($parameters); 42 | return parent::find($parameters); 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /unit-tests/models/Cacheable/Parts.php: -------------------------------------------------------------------------------- 1 | hasMany('id', 'Cacheable\RobotsParts', 'robots_id', array( 11 | 'alias' => 'RobotsParts' 12 | )); 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /unit-tests/models/Cacheable/Robots.php: -------------------------------------------------------------------------------- 1 | hasMany('id', 'Cacheable\RobotsParts', 'robots_id', array( 11 | 'alias' => 'RobotsParts' 12 | )); 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /unit-tests/models/Cacheable/RobotsParts.php: -------------------------------------------------------------------------------- 1 | belongsTo('robots_id', 'Cacheable\Robots', 'id', array( 10 | 'alias' => 'Robots' 11 | )); 12 | $this->belongsTo('parts_id', 'Cacheable\Parts', 'id', array( 13 | 'alias' => 'Parts' 14 | )); 15 | } 16 | } -------------------------------------------------------------------------------- /unit-tests/models/Deles.php: -------------------------------------------------------------------------------- 1 | 'code', 20 | 'name' => 'theName', 21 | ); 22 | } 23 | 24 | public function initialize() 25 | { 26 | $this->hasMany('code', 'RobottersDeles', 'delesCode', array( 27 | 'foreignKey' => array( 28 | 'message' => 'Deles cannot be deleted because is referenced by a Robotter' 29 | ) 30 | )); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /unit-tests/models/Dynamic/Personas.php: -------------------------------------------------------------------------------- 1 | useDynamicUpdate(true); 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /unit-tests/models/Dynamic/Personers.php: -------------------------------------------------------------------------------- 1 | 'borgerId', 17 | 'tipo_documento_id' => 'slagBorgerId', 18 | 'nombres' => 'navnes', 19 | 'telefono' => 'telefon', 20 | 'direccion' => 'adresse', 21 | 'email' => 'elektroniskPost', 22 | 'fecha_nacimiento' => 'fodtDato', 23 | 'ciudad_id' => 'fodebyId', 24 | 'creado_at' => 'skabtPa', 25 | 'cupo' => 'kredit', 26 | 'estado' => 'status' 27 | ); 28 | } 29 | 30 | public function initialize() 31 | { 32 | $this->setSource('personas'); 33 | $this->useDynamicUpdate(true); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /unit-tests/models/GossipRobots.php: -------------------------------------------------------------------------------- 1 | trace[$method[1]][get_class($this)])) { 17 | $this->trace[$method[1]][get_class($this)] = 1; 18 | } else { 19 | $this->trace[$method[1]][get_class($this)]++; 20 | } 21 | } 22 | 23 | public function beforeValidation() 24 | { 25 | $this->_talk(__METHOD__); 26 | } 27 | 28 | public function beforeValidationOnUpdate() 29 | { 30 | $this->_talk(__METHOD__); 31 | } 32 | 33 | public function afterValidationOnUpdate() 34 | { 35 | $this->_talk(__METHOD__); 36 | } 37 | 38 | public function validation() 39 | { 40 | $this->_talk(__METHOD__); 41 | } 42 | 43 | public function afterValidation() 44 | { 45 | $this->_talk(__METHOD__); 46 | } 47 | 48 | public function beforeSave() 49 | { 50 | $this->_talk(__METHOD__); 51 | } 52 | 53 | public function beforeUpdate() 54 | { 55 | $this->_talk(__METHOD__); 56 | } 57 | 58 | public function afterUpdate() 59 | { 60 | $this->_talk(__METHOD__); 61 | } 62 | 63 | public function afterSave() 64 | { 65 | $this->_talk(__METHOD__); 66 | } 67 | 68 | public function beforeCreate() 69 | { 70 | $this->_talk(__METHOD__); 71 | return false; 72 | } 73 | 74 | public function beforeDelete() 75 | { 76 | $this->_talk(__METHOD__); 77 | return false; 78 | } 79 | 80 | public function notSaved() 81 | { 82 | $this->_talk(__METHOD__); 83 | return false; 84 | } 85 | 86 | } -------------------------------------------------------------------------------- /unit-tests/models/News/Subscribers.php: -------------------------------------------------------------------------------- 1 | addBehavior(new Timestampable(array( 19 | 'beforeCreate' => array( 20 | 'field' => 'created_at', 21 | 'format' => 'Y-m-d H:i:s' 22 | ) 23 | ))); 24 | 25 | $this->addBehavior(new SoftDelete(array( 26 | 'field' => 'status', 27 | 'value' => 'D' 28 | ))); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /unit-tests/models/Parts.php: -------------------------------------------------------------------------------- 1 | hasMany('id', 'RobotsParts', 'parts_id', array( 9 | 'foreignKey' => array( 10 | 'message' => 'Parts cannot be deleted because is referenced by a Robot' 11 | ) 12 | )); 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /unit-tests/models/People.php: -------------------------------------------------------------------------------- 1 | 'borgerId', 20 | 'tipo_documento_id' => 'slagBorgerId', 21 | 'nombres' => 'navnes', 22 | 'telefono' => 'telefon', 23 | 'direccion' => 'adresse', 24 | 'email' => 'elektroniskPost', 25 | 'fecha_nacimiento' => 'fodtDato', 26 | 'ciudad_id' => 'fodebyId', 27 | 'creado_at' => 'skabtPa', 28 | 'cupo' => 'kredit', 29 | 'estado' => 'status' 30 | ); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /unit-tests/models/Personnes.php: -------------------------------------------------------------------------------- 1 | 'identificacao', 20 | 'tipo_documento_id' => 'tipoIdentificacao', 21 | 'nombres' => 'nomes', 22 | 'telefono' => 'telefone', 23 | 'direccion' => 'endereco', 24 | 'email' => 'elektroniskPost', 25 | 'fecha_nacimiento' => 'correio', 26 | 'ciudad_id' => 'cidadeId', 27 | 'creado_at' => 'criadoEm', 28 | 'cupo' => 'credito', 29 | 'estado' => 'estado' 30 | ); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /unit-tests/models/Products.php: -------------------------------------------------------------------------------- 1 | 'code', 20 | 'name' => 'theName', 21 | ); 22 | } 23 | 24 | public function initialize() 25 | { 26 | $this->hasMany('code', 'RobottersDeles', 'delesCode', array( 27 | 'foreignKey' => array( 28 | 'message' => 'Deles cannot be deleted because is referenced by a Robotter' 29 | ) 30 | )); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /unit-tests/models/Relations/M2MParts.php: -------------------------------------------------------------------------------- 1 | hasManyToMany('id', 'M2MRobotsParts', 'robots_id', 'parts_id', 'M2MParts', 'id'); 8 | } 9 | 10 | public function getSource() 11 | { 12 | return 'm2m_robots'; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /unit-tests/models/Relations/M2MRobotsParts.php: -------------------------------------------------------------------------------- 1 | hasMany('id', 'RelationsRobotsParts', 'parts_id', array( 9 | 'foreignKey' => array( 10 | 'message' => 'Parts cannot be deleted because is referenced by a Robot' 11 | ) 12 | )); 13 | } 14 | 15 | public function getSource() 16 | { 17 | return 'parts'; 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /unit-tests/models/Relations/RelationsRobots.php: -------------------------------------------------------------------------------- 1 | hasMany('id', 'RelationsRobotsParts', 'robots_id', array( 9 | 'foreignKey' => true 10 | )); 11 | $this->hasManyToMany('id', 'RelationsRobotsParts', 'robots_id', 'parts_id', 'RelationsParts', 'id'); 12 | } 13 | 14 | public function getSource() 15 | { 16 | return 'robots'; 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /unit-tests/models/Relations/RelationsRobotsParts.php: -------------------------------------------------------------------------------- 1 | belongsTo('parts_id', 'RelationsParts', 'id', array( 9 | 'foreignKey' => true 10 | )); 11 | $this->belongsTo('robots_id', 'RelationsRobots', 'id', array( 12 | 'foreignKey' => array( 13 | 'message' => 'The robot code does not exist' 14 | ) 15 | )); 16 | } 17 | 18 | public function getSource() 19 | { 20 | return 'robots_parts'; 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /unit-tests/models/Relations/Robotters.php: -------------------------------------------------------------------------------- 1 | 'code', 20 | 'name' => 'theName', 21 | 'type' => 'theType', 22 | 'year' => 'theYear' 23 | ); 24 | } 25 | 26 | public function initialize() 27 | { 28 | $this->hasMany('code', 'RobottersDeles', 'robottersCode', array( 29 | 'foreignKey' => true 30 | )); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /unit-tests/models/Relations/RobottersDeles.php: -------------------------------------------------------------------------------- 1 | 'code', 20 | 'robots_id' => 'robottersCode', 21 | 'parts_id' => 'delesCode', 22 | ); 23 | } 24 | 25 | public function initialize() 26 | { 27 | 28 | $this->belongsTo('delesCode', 'Deles', 'code', array( 29 | 'foreignKey' => true 30 | )); 31 | 32 | $this->belongsTo('robottersCode', 'Robotters', 'code', array( 33 | 'foreignKey' => array( 34 | 'message' => 'The robotters code does not exist' 35 | ) 36 | )); 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /unit-tests/models/Relations/Some/Deles.php: -------------------------------------------------------------------------------- 1 | 'code', 22 | 'name' => 'theName', 23 | ); 24 | } 25 | 26 | public function initialize() 27 | { 28 | $this->hasMany('code', 'Some\RobottersDeles', 'delesCode', array( 29 | 'foreignKey' => array( 30 | 'message' => 'Deles cannot be deleted because is referenced by a Robotter' 31 | ) 32 | )); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /unit-tests/models/Relations/Some/Parts.php: -------------------------------------------------------------------------------- 1 | hasMany('id', 'RobotsParts', 'parts_id', array( 16 | 'foreignKey' => array( 17 | 'message' => 'Parts cannot be deleted because is referenced by a Robot' 18 | ) 19 | )); 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /unit-tests/models/Relations/Some/Products.php: -------------------------------------------------------------------------------- 1 | array( 19 | 'id', 'name', 'type', 'price' 20 | ), 21 | MetaData::MODELS_PRIMARY_KEY => array( 22 | 'id' 23 | ), 24 | MetaData::MODELS_NON_PRIMARY_KEY => array( 25 | 'name', 'type', 'price' 26 | ), 27 | MetaData::MODELS_NOT_NULL => array( 28 | 'id', 'name', 'type', 'price' 29 | ), 30 | MetaData::MODELS_DATA_TYPES => array( 31 | 'id' => Column::TYPE_INTEGER, 32 | 'name' => Column::TYPE_VARCHAR, 33 | 'type' => Column::TYPE_VARCHAR, 34 | 'price' => Column::TYPE_INTEGER 35 | ), 36 | MetaData::MODELS_DATA_TYPES_NUMERIC => array( 37 | 'id' => true, 38 | 'price' => true, 39 | ), 40 | MetaData::MODELS_IDENTITY_COLUMN => 'id', 41 | MetaData::MODELS_DATA_TYPES_BIND => array( 42 | 'id' => Column::BIND_PARAM_INT, 43 | 'name' => Column::BIND_PARAM_STR, 44 | 'type' => Column::BIND_PARAM_STR, 45 | 'price' => Column::BIND_PARAM_INT, 46 | ), 47 | MetaData::MODELS_AUTOMATIC_DEFAULT_INSERT => array(), 48 | MetaData::MODELS_AUTOMATIC_DEFAULT_UPDATE => array() 49 | ); 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /unit-tests/models/Relations/Some/Robots.php: -------------------------------------------------------------------------------- 1 | hasMany('id', 'Some\RobotsParts', 'robots_id', array( 16 | 'foreignKey' => true 17 | )); 18 | } 19 | 20 | public function getRobotsParts($arguments=null) 21 | { 22 | return $this->getRelated('Some\RobotsParts', $arguments); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /unit-tests/models/Relations/Some/RobotsParts.php: -------------------------------------------------------------------------------- 1 | belongsTo('parts_id', 'Parts', 'id', array( 16 | 'foreignKey' => true 17 | )); 18 | $this->belongsTo('robots_id', 'Robots', 'id', array( 19 | 'foreignKey' => array( 20 | 'message' => 'The robot code does not exist' 21 | ) 22 | )); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /unit-tests/models/Relations/Some/Robotters.php: -------------------------------------------------------------------------------- 1 | 'code', 22 | 'name' => 'theName', 23 | 'type' => 'theType', 24 | 'year' => 'theYear' 25 | ); 26 | } 27 | 28 | public function initialize() 29 | { 30 | $this->hasMany('code', 'Some\RobottersDeles', 'robottersCode', array( 31 | 'foreignKey' => true 32 | )); 33 | } 34 | 35 | public function getRobottersDeles($arguments=null) 36 | { 37 | return $this->getRelated('Some\RobottersDeles', $arguments); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /unit-tests/models/Relations/Some/RobottersDeles.php: -------------------------------------------------------------------------------- 1 | 'code', 22 | 'robots_id' => 'robottersCode', 23 | 'parts_id' => 'delesCode', 24 | ); 25 | } 26 | 27 | public function initialize() 28 | { 29 | 30 | $this->belongsTo('delesCode', 'Some\Deles', 'code', array( 31 | 'foreignKey' => true 32 | )); 33 | 34 | $this->belongsTo('robottersCode', 'Some\Robotters', 'code', array( 35 | 'foreignKey' => array( 36 | 'message' => 'The robotters code does not exist' 37 | ) 38 | )); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /unit-tests/models/Robots.php: -------------------------------------------------------------------------------- 1 | hasMany('id', 'RobotsParts', 'robots_id', array( 9 | 'foreignKey' => true 10 | )); 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /unit-tests/models/RobotsParts.php: -------------------------------------------------------------------------------- 1 | belongsTo('parts_id', 'Parts', 'id', array( 9 | 'foreignKey' => true 10 | )); 11 | $this->belongsTo('robots_id', 'Robots', 'id', array( 12 | 'foreignKey' => array( 13 | 'message' => 'The robot code does not exist' 14 | ) 15 | )); 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /unit-tests/models/Robotters.php: -------------------------------------------------------------------------------- 1 | 'code', 20 | 'name' => 'theName', 21 | 'type' => 'theType', 22 | 'year' => 'theYear' 23 | ); 24 | } 25 | 26 | public function initialize() 27 | { 28 | $this->hasMany('code', 'RobottersDeles', 'robottersCode', array( 29 | 'foreignKey' => true 30 | )); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /unit-tests/models/RobottersDeles.php: -------------------------------------------------------------------------------- 1 | 'code', 20 | 'robots_id' => 'robottersCode', 21 | 'parts_id' => 'delesCode', 22 | ); 23 | } 24 | 25 | public function initialize() 26 | { 27 | 28 | $this->belongsTo('delesCode', 'Deles', 'code', array( 29 | 'foreignKey' => true 30 | )); 31 | 32 | $this->belongsTo('robottersCode', 'Robotters', 'code', array( 33 | 'foreignKey' => array( 34 | 'message' => 'The robotters code does not exist' 35 | ) 36 | )); 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /unit-tests/models/Robotto.php: -------------------------------------------------------------------------------- 1 | array( 21 | 'id', 'name', 'type', 'year' 22 | ), 23 | MetaData::MODELS_PRIMARY_KEY => array( 24 | 'id' 25 | ), 26 | MetaData::MODELS_NON_PRIMARY_KEY => array( 27 | 'name', 'type', 'year' 28 | ), 29 | MetaData::MODELS_NOT_NULL => array( 30 | 'id', 'name', 'type', 'year' 31 | ), 32 | MetaData::MODELS_DATA_TYPES => array( 33 | 'id' => Column::TYPE_INTEGER, 34 | 'name' => Column::TYPE_VARCHAR, 35 | 'type' => Column::TYPE_VARCHAR, 36 | 'year' => Column::TYPE_INTEGER 37 | ), 38 | MetaData::MODELS_DATA_TYPES_NUMERIC => array( 39 | 'id' => true, 40 | 'year' => true, 41 | ), 42 | MetaData::MODELS_IDENTITY_COLUMN => 'id', 43 | MetaData::MODELS_DATA_TYPES_BIND => array( 44 | 'id' => Column::BIND_PARAM_INT, 45 | 'name' => Column::BIND_PARAM_STR, 46 | 'type' => Column::BIND_PARAM_STR, 47 | 'year' => Column::BIND_PARAM_INT, 48 | ), 49 | MetaData::MODELS_AUTOMATIC_DEFAULT_INSERT => array(), 50 | MetaData::MODELS_AUTOMATIC_DEFAULT_UPDATE => array() 51 | ); 52 | } 53 | 54 | } -------------------------------------------------------------------------------- /unit-tests/models/Snapshot/Parts.php: -------------------------------------------------------------------------------- 1 | hasMany('id', 'Snapshot\RobotsParts', 'robots_id'); 11 | $this->keepSnapshots(true); 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /unit-tests/models/Snapshot/Robots.php: -------------------------------------------------------------------------------- 1 | hasMany('id', 'Snapshot\RobotsParts', 'robots_id'); 11 | $this->keepSnapshots(true); 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /unit-tests/models/Snapshot/RobotsParts.php: -------------------------------------------------------------------------------- 1 | belongsTo('robots_id', 'Snapshot\Robots', 'id'); 11 | $this->belongsTo('parts_id', 'Snapshot\Parts', 'id'); 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /unit-tests/models/Snapshot/Robotters.php: -------------------------------------------------------------------------------- 1 | 'code', 16 | 'name' => 'theName', 17 | 'type' => 'theType', 18 | 'year' => 'theYear' 19 | ); 20 | } 21 | 22 | public function initialize() 23 | { 24 | $this->hasMany('code', 'RobottersDeles', 'robottersCode'); 25 | $this->keepSnapshots(true); 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /unit-tests/models/Some/Deles.php: -------------------------------------------------------------------------------- 1 | 'code', 22 | 'name' => 'theName', 23 | ); 24 | } 25 | 26 | public function initialize() 27 | { 28 | $this->hasMany('code', 'Some\RobottersDeles', 'delesCode', array( 29 | 'foreignKey' => array( 30 | 'message' => 'Deles cannot be deleted because is referenced by a Robotter' 31 | ) 32 | )); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /unit-tests/models/Some/Parts.php: -------------------------------------------------------------------------------- 1 | hasMany('id', 'RobotsParts', 'parts_id', array( 16 | 'foreignKey' => array( 17 | 'message' => 'Parts cannot be deleted because is referenced by a Robot' 18 | ) 19 | )); 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /unit-tests/models/Some/Products.php: -------------------------------------------------------------------------------- 1 | array( 19 | 'id', 'name', 'type', 'price' 20 | ), 21 | MetaData::MODELS_PRIMARY_KEY => array( 22 | 'id' 23 | ), 24 | MetaData::MODELS_NON_PRIMARY_KEY => array( 25 | 'name', 'type', 'price' 26 | ), 27 | MetaData::MODELS_NOT_NULL => array( 28 | 'id', 'name', 'type', 'price' 29 | ), 30 | MetaData::MODELS_DATA_TYPES => array( 31 | 'id' => Column::TYPE_INTEGER, 32 | 'name' => Column::TYPE_VARCHAR, 33 | 'type' => Column::TYPE_VARCHAR, 34 | 'price' => Column::TYPE_INTEGER 35 | ), 36 | MetaData::MODELS_DATA_TYPES_NUMERIC => array( 37 | 'id' => true, 38 | 'price' => true, 39 | ), 40 | MetaData::MODELS_IDENTITY_COLUMN => 'id', 41 | MetaData::MODELS_DATA_TYPES_BIND => array( 42 | 'id' => Column::BIND_PARAM_INT, 43 | 'name' => Column::BIND_PARAM_STR, 44 | 'type' => Column::BIND_PARAM_STR, 45 | 'price' => Column::BIND_PARAM_INT, 46 | ), 47 | MetaData::MODELS_AUTOMATIC_DEFAULT_INSERT => array(), 48 | MetaData::MODELS_AUTOMATIC_DEFAULT_UPDATE => array() 49 | ); 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /unit-tests/models/Some/Robots.php: -------------------------------------------------------------------------------- 1 | hasMany('id', 'Some\RobotsParts', 'robots_id', array( 16 | 'foreignKey' => true 17 | )); 18 | } 19 | 20 | public function getRobotsParts($arguments=null) 21 | { 22 | return $this->getRelated('Some\RobotsParts', $arguments); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /unit-tests/models/Some/RobotsParts.php: -------------------------------------------------------------------------------- 1 | belongsTo('parts_id', 'Parts', 'id', array( 16 | 'foreignKey' => true 17 | )); 18 | $this->belongsTo('robots_id', 'Robots', 'id', array( 19 | 'foreignKey' => array( 20 | 'message' => 'The robot code does not exist' 21 | ) 22 | )); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /unit-tests/models/Some/Robotters.php: -------------------------------------------------------------------------------- 1 | 'code', 22 | 'name' => 'theName', 23 | 'type' => 'theType', 24 | 'year' => 'theYear' 25 | ); 26 | } 27 | 28 | public function initialize() 29 | { 30 | $this->hasMany('code', 'Some\RobottersDeles', 'robottersCode', array( 31 | 'foreignKey' => true 32 | )); 33 | } 34 | 35 | public function getRobottersDeles($arguments=null) 36 | { 37 | return $this->getRelated('Some\RobottersDeles', $arguments); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /unit-tests/models/Some/RobottersDeles.php: -------------------------------------------------------------------------------- 1 | 'code', 22 | 'robots_id' => 'robottersCode', 23 | 'parts_id' => 'delesCode', 24 | ); 25 | } 26 | 27 | public function initialize() 28 | { 29 | 30 | $this->belongsTo('delesCode', 'Some\Deles', 'code', array( 31 | 'foreignKey' => true 32 | )); 33 | 34 | $this->belongsTo('robottersCode', 'Some\Robotters', 'code', array( 35 | 'foreignKey' => array( 36 | 'message' => 'The robotters code does not exist' 37 | ) 38 | )); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /unit-tests/models/Store/Parts.php: -------------------------------------------------------------------------------- 1 | setConnectionService('dbTwo'); 11 | 12 | $this->hasMany('id', 'Some\RobotsParts', 'parts_id', array( 13 | 'alias' => 'RobotParts' 14 | )); 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /unit-tests/models/Store/Robots.php: -------------------------------------------------------------------------------- 1 | setConnectionService('dbOne'); 12 | 13 | $this->hasMany('id', 'Store\RobotsParts', 'robots_id', array( 14 | 'alias' => 'RobotParts' 15 | )); 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /unit-tests/models/Store/RobotsParts.php: -------------------------------------------------------------------------------- 1 | setConnectionService('dbOne'); 11 | 12 | $this->belongsTo('parts_id', 'Store\Parts', 'id', array( 13 | 'alias' => 'Part' 14 | )); 15 | $this->belongsTo('robots_id', 'Store\Robots', 'id', array( 16 | 'alias' => 'Robot' 17 | )); 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /unit-tests/models/Subscribers.php: -------------------------------------------------------------------------------- 1 | email == 'marina@hotmail.com') { 19 | $this->appendMessage(new Message('Sorry Marina, but you are not allowed here')); 20 | return false; 21 | } 22 | } 23 | 24 | public function beforeDelete() 25 | { 26 | if ($this->email == 'fuego@hotmail.com') { 27 | $this->appendMessage(new Message('Sorry this cannot be deleted')); 28 | return false; 29 | } 30 | } 31 | 32 | public function validation() 33 | { 34 | 35 | $this->validate(new PresenceOfValidator(array( 36 | 'field' => 'created_at' 37 | ))); 38 | 39 | $this->validate(new StringLengthValidator(array( 40 | 'field' => 'email', 41 | 'min' => '7', 42 | 'max' => '50' 43 | ))); 44 | 45 | $this->validate(new EmailValidator(array( 46 | 'field' => 'email' 47 | ))); 48 | 49 | $this->validate(new ExclusionInValidator(array( 50 | 'field' => 'status', 51 | 'domain' => array('X', 'Z') 52 | ))); 53 | 54 | $this->validate(new InclusionInValidator(array( 55 | 'field' => 'status', 56 | 'domain' => array('P', 'I', 'w') 57 | ))); 58 | 59 | $this->validate(new UniquenessValidator(array( 60 | 'field' => 'email' 61 | ))); 62 | 63 | $this->validate(new RegexValidator(array( 64 | 'field' => 'status', 65 | 'pattern' => '/[A-Z]/' 66 | ))); 67 | 68 | if ($this->validationHasFailed() == true) { 69 | return false; 70 | } 71 | } 72 | 73 | } 74 | --------------------------------------------------------------------------------