PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/fixtures/tests/TestOfFixtureBuilder.php

https://github.com/dagda/ThinkUp
PHP | 299 lines | 210 code | 54 blank | 35 comment | 31 complexity | 59242207da6c2b740e7ebeb123283bc6 MD5 | raw file
  1. <?php
  2. require_once THINKUP_ROOT_PATH.'webapp/config.inc.php';
  3. require_once THINKUP_ROOT_PATH.'tests/config.tests.inc.php';
  4. require_once THINKUP_ROOT_PATH.'webapp/_lib/extlib/simpletest/autorun.php';
  5. require_once 'webapp/model/class.Config.php';
  6. require_once THINKUP_ROOT_PATH.'tests/fixtures/class.FixtureBuilder.php';
  7. class TestOfixtureBuilder extends UnitTestCase {
  8. const TEST_TABLE = 'test_table';
  9. function setUp() {
  10. global $TEST_DATABASE;
  11. $this->config = Config::getInstance();
  12. $this->config->setValue('db_name', $TEST_DATABASE);
  13. //add prefix to the test table
  14. $this->test_table = Config::getInstance()->getValue('table_prefix') . self::TEST_TABLE;
  15. // build test table
  16. $this->builder = new FixtureBuilder();
  17. $this->pdo = FixtureBuilder::$pdo;
  18. $this->pdo->query('CREATE TABLE ' . $this->test_table . '(' .
  19. 'id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,' .
  20. 'test_name varchar(20),' .
  21. 'test_city varchar(20) not null default "",' .
  22. 'test_id int(11),' .
  23. "fav_color enum('red', 'blue', 'green')," .
  24. "fav_food enum('apple''s', 'hotdog', 'roll') not null default 'roll' ," .
  25. 'unique key test_id_idx (test_id),' .
  26. 'date_created timestamp default CURRENT_TIMESTAMP,' .
  27. 'date_updated datetime,' .
  28. 'birthday date,' .
  29. 'worth decimal(11,2) default 12.99' .
  30. ')');
  31. }
  32. function tearDown() {
  33. $this->pdo->query('drop table ' . $this->test_table);
  34. }
  35. function testBuildData() {
  36. $builder = FixtureBuilder::build(self::TEST_TABLE, array('test_id' => 1), true );
  37. // auto inc id
  38. $this->assertEqual(1, $builder->columns['last_insert_id'], 'our id is 1');
  39. // test_name is a string?
  40. $this->assertTrue(is_string($builder->columns['test_name']), 'we have a name string');
  41. // test_city is a string?
  42. $this->assertTrue(is_string($builder->columns['test_city']), 'we have a city string');
  43. $this->assertTrue(strlen($builder->columns['test_city']) > 0, 'we have a city string');
  44. // test_id is an int?
  45. $this->assertEqual(1, $builder->columns['test_id'], 'we have a test_id');
  46. // test fav_food enum
  47. $enum_array = array('red', 'blue', 'green');
  48. $this->assertTrue($this->_testEnum($enum_array, $builder->columns['fav_color']), 'we have a valid enum value ' . $builder->columns['fav_color']);
  49. // test fav_food enum
  50. $enum_array = array("apple''s", 'hotdog', 'roll');
  51. $this->assertEqual($builder->columns['fav_food'], 'roll', 'we have a default enum value: roll');
  52. $builder2 = FixtureBuilder::build(self::TEST_TABLE, array('test_id' => 2, 'fav_food' => 'hotdog'), true );
  53. // auto inc id
  54. $this->assertEqual(2, $builder2->columns['last_insert_id'], 'our id is 2');
  55. // test_name is a string?
  56. $this->assertTrue(is_string($builder2->columns['test_name']), 'we have a name string');
  57. // test_id is an int?
  58. $this->assertEqual(2, $builder2->columns['test_id'], 'we have a test_id');
  59. // test fav_color enum
  60. $enum_array = array('red', 'blue', 'green');
  61. $this->assertTrue($this->_testEnum($enum_array, $builder2->columns['fav_color']), 'we have a valid enum value ' . $builder->columns['fav_color']);
  62. // test fav_food enum
  63. $this->assertEqual($builder2->columns['fav_food'], 'hotdog', 'we have a enum value: hotdog');
  64. //test date fields
  65. $date_time = new DateTime($builder2->columns['date_created']);
  66. $this->assertTrue(is_a( $date_time , 'DateTime'), 'we have a date');
  67. $date_time = new DateTime($builder2->columns['date_updated']);
  68. $this->assertTrue(is_a( $date_time , 'DateTime'), 'we have a date');
  69. $date_time = new DateTime($builder2->columns['birthday']);
  70. $this->assertTrue(is_a( $date_time , 'DateTime'), 'we have a date');
  71. // set dates
  72. $date_fixture_data = array('test_id' => 3, 'date_created' => '+1d', 'birthday' => '1978-06-20');
  73. $builder3 = FixtureBuilder::build(self::TEST_TABLE, $date_fixture_data);
  74. $mysql_date = strtotime( $builder3->columns['date_created'] );
  75. $match_date = time() + (60 * 60 * 24);
  76. $this->assertTrue($this->_testDatesAreClose($mysql_date, $match_date), 'dates are within 2 seconds');
  77. $this->assertEqual('1978-06-20', $builder3->columns['birthday'], 'birthday set properly');
  78. $this->assertEqual('12.99', $builder3->columns['worth'], 'worth 12.99');
  79. }
  80. function testDestroyData() {
  81. $builder = FixtureBuilder::build(self::TEST_TABLE, array('test_id' => 1) );
  82. $stmt = $this->pdo->query( "select count(*) as count from " . $this->test_table );
  83. $data = $stmt->fetch();
  84. $this->assertEqual(1, $data['count'], 'we have one row');
  85. $builder = null;
  86. // builder is now out of scope, so _destruct should have deleted our data
  87. $stmt = $this->pdo->query( "select count(*) as count from " . $this->test_table );
  88. $data = $stmt->fetch();
  89. $this->assertEqual(0, $data['count'], 'we have no rows');
  90. }
  91. function testTruncateTable() {
  92. // bad table name
  93. try {
  94. FixtureBuilder::truncateTable('notable');
  95. $this->fail("should throw FixtureBuilderException");
  96. } catch(FixtureBuilderException $e) {
  97. $this->assertPattern('/Unable to truncate table "tu_notable"/', $e->getMessage());
  98. }
  99. //add a row, query it, and count should be one
  100. $this->pdo->query( sprintf("insert into %s (test_name, test_id) values ('mary', 1)", $this->test_table) );
  101. $stmt = $this->pdo->query( "select count(*) as count from " . $this->test_table );
  102. $data = $stmt->fetch();
  103. $this->assertEqual(1, $data['count'], 'we have one row');
  104. //truncate row, and count should be 0
  105. FixtureBuilder::truncateTable(self::TEST_TABLE);
  106. $stmt = $this->pdo->query( "select count(*) as count from " . $this->test_table );
  107. $data = $stmt->fetch();
  108. $this->assertEqual(0, $data['count'], 'we have a truncated table');
  109. }
  110. function testDescribeTable() {
  111. try {
  112. $this->builder->describeTable('notable');
  113. } catch(FixtureBuilderException $e) {
  114. $this->assertPattern('/Unable to describe table "tu_notable"/', $e->getMessage());
  115. }
  116. $columns = $this->builder->describeTable(self::TEST_TABLE);
  117. $this->assertEqual(count($columns), 10, 'column count valid');
  118. }
  119. function testGendata() {
  120. // test enum
  121. $enum_array = array("apple''s",'hotdog','roll');
  122. $value = $this->builder->genEnum( "enum('apple''s','hotdog','roll')");
  123. $this->assertTrue($this->_testEnum($enum_array, $value), 'we have a valid enum value ' . $value);
  124. //test int gen
  125. $fail = 0;
  126. for($i = 0; $i < 1000; $i++) {
  127. $int = $this->builder->genInt();
  128. if( $int > $this->builder->DATA_DEFAULTS['int'] ) { $fail++; }
  129. }
  130. if($fail > 0) { $this->fail("failed genInt()"); }
  131. $fail = 0;
  132. for($i = 0; $i < 1000; $i++) {
  133. $int = $this->builder->genInt(2);
  134. if( $int > 2 ) { $fail++; }
  135. }
  136. if($fail > 0) { $this->fail("failed genInt(2) $fail"); }
  137. //test
  138. // bigint gen
  139. $fail = 0;
  140. for($i = 0; $i < 1000; $i++) {
  141. $int = $this->builder->genBigInt();
  142. if( $int > $this->builder->DATA_DEFAULTS['bigint']) { $fail++; }
  143. }
  144. if($fail > 0) { $this->fail("failed genBigInt() $fail"); }
  145. $fail = 0;
  146. for($i = 0; $i < 1000; $i++) {
  147. $int = $this->builder->genBigInt(3);
  148. if( $int > 3) { $fail++; }
  149. }
  150. if($fail > 0) { $this->fail("failed genBigInt(3) $fail"); }
  151. //test tiny int gen
  152. $fail = 0;
  153. for($i = 0; $i < 1000; $i++) {
  154. $int = $this->builder->genTinyInt();
  155. if( $int > $this->builder->DATA_DEFAULTS['tinyint']) { $fail++; }
  156. }
  157. if($fail > 0) { $this->fail("failed genTinyInt() $fail"); }
  158. $fail = 0;
  159. for($i = 0; $i < 1000; $i++) {
  160. $int = $this->builder->genTinyInt(3);
  161. if( $int > 3) { $fail++; }
  162. }
  163. if($fail > 0) { $this->fail("failed genTinyInt(3) $fail"); }
  164. //test varchars
  165. $fail = 0;
  166. for($i = 0; $i < 1000; $i++) {
  167. $text = $this->builder->genVarchar();
  168. if(strlen($text) > $this->builder->DATA_DEFAULTS['varchar']) {
  169. $fail++;
  170. }
  171. }
  172. $fail = 0;
  173. for($i = 0; $i < 1000; $i++) {
  174. $text = $this->builder->genVarchar(2);
  175. if(strlen($text) > 2) {
  176. $fail++;
  177. }
  178. }
  179. if($fail > 0) { $this->fail("failed $fail genVarchar(2) tests"); }
  180. // test dates 3 days
  181. $date_text = $this->builder->genDate('+3d');
  182. $mysql_date = strtotime( $date_text );
  183. $match_date = time() + (3 * 60 * 60 * 24);
  184. $this->assertTrue($this->_testDatesAreClose($mysql_date, $match_date), 'dates are within 2 seconds');
  185. // test dates -3 days
  186. $date_text = $this->builder->genDate('-3d');
  187. $mysql_date = strtotime( $date_text );
  188. $match_date = time() - (3 * 60 * 60 * 24);
  189. $this->assertTrue($this->_testDatesAreClose($mysql_date, $match_date), 'dates are within 2 seconds');
  190. // test dates 1 hour
  191. $date_text = $this->builder->genDate('+1h');
  192. $mysql_date = strtotime( $date_text );
  193. $match_date = time() + (60 * 60);
  194. $this->assertTrue($this->_testDatesAreClose($mysql_date, $match_date), 'dates are within 2 seconds');
  195. // test dates -1 hour
  196. $date_text = $this->builder->genDate('-1h');
  197. $mysql_date = strtotime( $date_text );
  198. $match_date = time() - (60 * 60);
  199. $this->assertTrue($this->_testDatesAreClose($mysql_date, $match_date), 'dates are within 2 seconds');
  200. // test dates 17 minutes
  201. $date_text = $this->builder->genDate('+17m');
  202. $mysql_date = strtotime( $date_text );
  203. $match_date = time() + (17 * 60);
  204. $this->assertTrue($this->_testDatesAreClose($mysql_date, $match_date), 'dates are within 2 seconds');
  205. // test dates -342minutes
  206. $date_text = $this->builder->genDate('-346m');
  207. $mysql_date = strtotime( $date_text );
  208. $match_date = time() - (346 * 60);
  209. $this->assertTrue($this->_testDatesAreClose($mysql_date, $match_date), 'dates are within 2 seconds');
  210. // test dates 4 seconds
  211. $date_text = $this->builder->genDate('+4s');
  212. $mysql_date = strtotime( $date_text );
  213. $match_date = time() + (4);
  214. $this->assertTrue($this->_testDatesAreClose($mysql_date, $match_date), 'dates are within 4 seconds');
  215. // test dates -766minutes
  216. $date_text = $this->builder->genDate('-766m');
  217. $mysql_date = strtotime( $date_text );
  218. $match_date = time() - (766 * 60);
  219. $this->assertTrue( $this->_testDatesAreClose($mysql_date, $match_date), 'dates are within 2 seconds');
  220. // test date string passed in value
  221. $this->assertEqual($this->builder->genDate('2010-06-20 16:22:25'), '2010-06-20 16:22:25', 'date matches');
  222. // test genDecimal
  223. $fail = null;
  224. for($i = 0; $i < 1000; $i++) {
  225. $dec = $this->builder->genDecimal('decimal(3,2)');
  226. $values = preg_split('/\./', $dec);
  227. if($values[0] >= 1000) { $fail = "left value is not less than 1000 - " . $values[0]; break;}
  228. if($values[1] >= 100) { $fail = "right value is not less than 100 - " . $values[1]; break; }
  229. }
  230. if($fail) {
  231. $this->fail($fail);
  232. }
  233. }
  234. function _testDatesAreClose($date1, $date2) {
  235. $date_diff = $date1 - $date2;
  236. return ($date_diff < 2 && $date_diff > - 2);
  237. }
  238. function _testEnum($enum_array, $value) {
  239. $pass = false;
  240. for($i = 0; $i < count($enum_array); $i++) {
  241. if( $value == $enum_array[$i] ) { $pass = true; }
  242. }
  243. return $pass;
  244. }
  245. }