From: <gem...@li...> - 2012-09-07 11:11:16
|
Revision: 934 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=934&view=rev Author: mennodekker Date: 2012-09-07 11:11:06 +0000 (Fri, 07 Sep 2012) Log Message: ----------- Database test for Token object created, using multiple (reusable) tabledefs but separate dataset for each test class to keep data private to the test. Modified Paths: -------------- trunk/test/classes/Gems/Tracker/TokenTest.php Added Paths: ----------- trunk/test/classes/Gems/Test/DbTestAbstract.php trunk/test/classes/Gems/Tracker/TokenTest.xml trunk/test/data/gems__consents.sql trunk/test/data/gems__reception_codes.sql trunk/test/data/gems__respondent2org.sql trunk/test/data/gems__respondents.sql trunk/test/data/gems__tokens.sql Added: trunk/test/classes/Gems/Test/DbTestAbstract.php =================================================================== --- trunk/test/classes/Gems/Test/DbTestAbstract.php (rev 0) +++ trunk/test/classes/Gems/Test/DbTestAbstract.php 2012-09-07 11:11:06 UTC (rev 934) @@ -0,0 +1,114 @@ +<?php + +/** + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * Base test class for Gems object test cases + * + * @package Gems + * @subpackage Test + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @version $Id: TestAbstract.php 925 2012-09-05 09:59:13Z mennodekker $ + */ + +/** + * Base test class for Gems object test cases that involve a database test + * + * @package Gems + * @subpackage Test + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @version $Id: TestAbstract.php 925 2012-09-05 09:59:13Z mennodekker $ + */ +abstract class Gems_Test_DbTestAbstract extends Zend_Test_PHPUnit_DatabaseTestCase +{ + /** + * @var Gems_Loader + */ + protected $loader = null; + + /** + * @var Zend_Db + */ + protected $db = null; + + /** + * + * @var PHPUnit_Extensions_Database_DB_IDatabaseConnection + */ + protected $_connectionMock; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + protected function setUp() + { + parent::setUp(); + + global $GEMS_DIRS; + + $this->db = $this->getConnection()->getConnection(); + + Zend_Registry::set('db', $this->db); + + $this->loader = new Gems_Loader(Zend_Registry::getInstance(), $GEMS_DIRS); + + Zend_Registry::set('loader', $this->loader); + } + + /** + * Returns the test database connection. + * + * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection + */ + protected function getConnection() + { + if($this->_connectionMock == null) { + $connection = Zend_Db::factory('Pdo_Sqlite', array('dbname' => ':memory:', 'username' => 'test')); + + if ($sqlFiles = $this->getInitSql()) { + foreach ($sqlFiles as $file) { + $sql = file_get_contents($file); + $stmt = $connection->query($sql); + } + } + $this->_connectionMock = $this->createZendDbConnection( + $connection, 'zfunittests' + ); + Zend_Db_Table_Abstract::setDefaultAdapter($connection); + } + + return $this->_connectionMock; + } + + /** + * Return an array of files to run on init + */ + abstract protected function getInitSql(); +} \ No newline at end of file Modified: trunk/test/classes/Gems/Tracker/TokenTest.php =================================================================== --- trunk/test/classes/Gems/Tracker/TokenTest.php 2012-09-07 07:25:28 UTC (rev 933) +++ trunk/test/classes/Gems/Tracker/TokenTest.php 2012-09-07 11:11:06 UTC (rev 934) @@ -47,7 +47,7 @@ * @license New BSD License * @version $Id$ */ -class Gems_Tracker_TokenTest extends Gems_Test_TestAbstract +class Gems_Tracker_TokenTest extends Gems_Test_DbTestAbstract { /** * @var Gems_Tracker_Token @@ -55,12 +55,19 @@ protected $token; /** + * @var Gems_Tracker + */ + protected $tracker; + + /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ protected function setUp() { parent::setUp(); + + $this->tracker = $this->loader->getTracker(); $this->token = $this->tracker->getToken(array('gto_id_token' => 500)); } @@ -73,7 +80,33 @@ { } + protected function getInitSql() + { + $path = GEMS_WEB_DIR . '/data/'; + + // For successful testing of the complete tokens class, we need more tables + return array( + $path . 'gems__tokens.sql', + $path . 'gems__reception_codes.sql', + $path . 'gems__respondents.sql', + $path . 'gems__respondent2org.sql', + $path . 'gems__consents.sql' + ); + } + /** + * Returns the test dataset. + * + * @return PHPUnit_Extensions_Database_DataSet_IDataSet + */ + protected function getDataSet() + { + //Dataset TokenTest.xml has the minimal data we need to perform our tests + $classFile = str_replace('.php', '.xml', __FILE__); + return $this->createFlatXMLDataSet($classFile); + } + + /** * @covers Gems_Tracker_Token::applyToMenuSource * @todo Implement testApplyToMenuSource(). */ @@ -395,7 +428,13 @@ */ public function testGetRespondentId() { - $this->assertNotEmpty($this->token->getRespondentId(), 'Any token should have a respondentId'); + $this->assertNotEmpty($this->tracker->getToken('1')->getRespondentId(), 'Any token should have a respondentId'); + + try { + // This one does not exists and should throw an error + $this->tracker->getToken('2')->getRespondentId(); + } catch (Exception $e) {} + $this->assertInstanceOf('Gems_Exception', $e, 'Token not loaded correctly'); } /** @@ -817,4 +856,30 @@ 'This test has not been implemented yet.' ); } + + /** + * This tests if the public property exists is set correctly + * + * @dataProvider providerTokenData + */ + public function testTokenExists($tokenData, $exists) + { + $token = $this->tracker->getToken($tokenData); + $this->assertEquals($exists, $token->exists); + } + + public function providerTokenData() + { + return array( + array( + '1', + true), // Is in the table + array( + '2', + false), // Is not in the table + array( + array('gto_id_token' => '111'), // Array provided so not checked and accepted as is + true) + ); + } } Added: trunk/test/classes/Gems/Tracker/TokenTest.xml =================================================================== --- trunk/test/classes/Gems/Tracker/TokenTest.xml (rev 0) +++ trunk/test/classes/Gems/Tracker/TokenTest.xml 2012-09-07 11:11:06 UTC (rev 934) @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<dataset> + <gems__tokens gto_id_token="1" gto_id_respondent_track="1" gto_id_round="0" gto_id_respondent="1234" gto_id_organization="1" gto_id_track="1" gto_id_survey="1" gto_round_order="1" gto_round_description="" gto_valid_from="NULL" gto_valid_until="NULL" gto_mail_sent_date="NULL" gto_mail_sent_num="0" gto_next_mail_date="NULL" gto_start_time="NULL" gto_completion_time="NULL" gto_duration_in_sec="NULL" gto_result="NULL" gto_comment="NULL" gto_reception_code="OK" gto_return_url="NULL" gto_in_source="1" gto_by="1" gto_changed="2012-03-29 13:50:22" gto_changed_by="1" gto_created="2012-03-29 10:55:55" gto_created_by="1" /> + <gems__reception_codes grc_id_reception_code="OK" grc_description="Ok code" grc_changed_by="1" grc_created="2012-09-07" grc_created_by="1"/> + <gems__respondents grs_id_user="1234" grs_changed_by="1" grs_created="2012-09-07" grs_created_by="1"/> + <gems__respondent2org gr2o_id_user="1234" gr2o_id_organization="1" gr2o_consent="ok" gr2o_patient_nr="abc" gr2o_opened_by="1" gr2o_changed="2012-09-07" gr2o_changed_by="1" gr2o_created="2012-09-07" gr2o_created_by="1"/> + <gems__consents gco_description="informed" gco_changed_by="1" gco_created="2012-09-07" gco_created_by="1"/> +</dataset> \ No newline at end of file Added: trunk/test/data/gems__consents.sql =================================================================== --- trunk/test/data/gems__consents.sql (rev 0) +++ trunk/test/data/gems__consents.sql 2012-09-07 11:11:06 UTC (rev 934) @@ -0,0 +1,12 @@ +CREATE TABLE gems__consents ( + gco_description varchar(20) not null, + gco_order smallint not null default 10, + gco_code varchar(20) not null default 'do not use', + + gco_changed timestamp not null default current_timestamp, + gco_changed_by bigint unsigned not null, + gco_created timestamp not null, + gco_created_by bigint unsigned not null, + + PRIMARY KEY (gco_description) + ); \ No newline at end of file Added: trunk/test/data/gems__reception_codes.sql =================================================================== --- trunk/test/data/gems__reception_codes.sql (rev 0) +++ trunk/test/data/gems__reception_codes.sql 2012-09-07 11:11:06 UTC (rev 934) @@ -0,0 +1,20 @@ +CREATE TABLE gems__reception_codes ( + grc_id_reception_code varchar(20) not null, + grc_description varchar(40) not null, + + grc_success boolean not null default 0, + + grc_for_surveys tinyint not null default 0, + grc_redo_survey tinyint not null default 0, + grc_for_tracks boolean not null default 0, + grc_for_respondents boolean not null default 0, + grc_overwrite_answers boolean not null default 0, + grc_active boolean not null default 1, + + grc_changed timestamp not null default current_timestamp, + grc_changed_by bigint unsigned not null, + grc_created timestamp not null, + grc_created_by bigint unsigned not null, + + PRIMARY KEY (grc_id_reception_code) + ); \ No newline at end of file Added: trunk/test/data/gems__respondent2org.sql =================================================================== --- trunk/test/data/gems__respondent2org.sql (rev 0) +++ trunk/test/data/gems__respondent2org.sql 2012-09-07 11:11:06 UTC (rev 934) @@ -0,0 +1,22 @@ +CREATE TABLE gems__respondent2org ( + gr2o_patient_nr varchar(7) not null, + gr2o_id_organization bigint unsigned not null + references gems__organizations (gor_id_organization), + + gr2o_id_user bigint unsigned not null + references gems__respondents (grs_id_user), + + gr2o_consent varchar(20) not null default 'Unknown' + references gems__consents (gco_description), + gr2o_reception_code varchar(20) default 'OK' not null + references gems__reception_codes (grc_id_reception_code), + + gr2o_opened timestamp not null default current_timestamp, + gr2o_opened_by bigint unsigned not null, + gr2o_changed timestamp not null, + gr2o_changed_by bigint unsigned not null, + gr2o_created timestamp not null, + gr2o_created_by bigint unsigned not null, + + PRIMARY KEY (gr2o_patient_nr, gr2o_id_organization) + ); \ No newline at end of file Added: trunk/test/data/gems__respondents.sql =================================================================== --- trunk/test/data/gems__respondents.sql (rev 0) +++ trunk/test/data/gems__respondents.sql 2012-09-07 11:11:06 UTC (rev 934) @@ -0,0 +1,32 @@ +CREATE TABLE gems__respondents ( + grs_id_user bigint unsigned not null, + + grs_ssn varchar(32) + null unique, + + grs_iso_lang char(2) + not null default 'en' references gems__languages (gml_iso_lang), + + grs_email varchar(100) null, + + grs_first_name varchar(30) , + grs_surname_prefix varchar(10) , + grs_last_name varchar(50) , + grs_gender char(1) + not null default 'U', + grs_birthday date, + + grs_address_1 varchar(80) , + grs_address_2 varchar(80) , + grs_zipcode varchar(10) , + grs_city varchar(40) , + grs_iso_country char(2) not null default 'NL', + grs_phone_1 varchar(25) , + + grs_changed timestamp not null default current_timestamp, + grs_changed_by bigint unsigned not null, + grs_created timestamp not null, + grs_created_by bigint unsigned not null, + + PRIMARY KEY(grs_id_user) + ); \ No newline at end of file Added: trunk/test/data/gems__tokens.sql =================================================================== --- trunk/test/data/gems__tokens.sql (rev 0) +++ trunk/test/data/gems__tokens.sql 2012-09-07 11:11:06 UTC (rev 934) @@ -0,0 +1,45 @@ +CREATE TABLE gems__tokens ( + gto_id_token varchar(9) not null, + + gto_id_respondent_track bigint not null, + gto_id_round bigint not null, + + -- non-changing fields calculated from previous two: + gto_id_respondent bigint not null, + gto_id_organization bigint not null, + gto_id_track bigint not null, + + -- values initially filled from gems__rounds, but that may get different values later on + gto_id_survey bigint, + + -- values initially filled from gems__rounds, but that might get different values later on, but but not now + gto_round_order int not null default 10, + gto_round_description varchar(100) null, + + -- real data + gto_valid_from datetime, + gto_valid_until datetime, + gto_mail_sent_date date, + gto_mail_sent_num int(11) not null default 0, + gto_next_mail_date date, + + gto_start_time datetime, + gto_in_source boolean not null default 0, + gto_by bigint(20) NULL, + + gto_completion_time datetime, + gto_duration_in_sec bigint(20) NULL, + gto_result varchar(20) , + + gto_comment varchar(250) null default null, + gto_reception_code varchar(20) default 'OK' not null, + + gto_return_url varchar(250) null default null, + + gto_changed timestamp not null default current_timestamp, + gto_changed_by bigint not null, + gto_created timestamp not null, + gto_created_by bigint not null, + + PRIMARY KEY (gto_id_token) + ); \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |