ColumnTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. include 'helpers/config.php';
  3. use ActiveRecord\Column;
  4. use ActiveRecord\DateTime;
  5. use ActiveRecord\DatabaseException;
  6. class ColumnTest extends SnakeCase_PHPUnit_Framework_TestCase
  7. {
  8. public function set_up()
  9. {
  10. $this->column = new Column();
  11. try {
  12. $this->conn = ActiveRecord\ConnectionManager::get_connection(ActiveRecord\Config::instance()->get_default_connection());
  13. } catch (DatabaseException $e) {
  14. $this->mark_test_skipped('failed to connect using default connection. '.$e->getMessage());
  15. }
  16. }
  17. public function assert_mapped_type($type, $raw_type)
  18. {
  19. $this->column->raw_type = $raw_type;
  20. $this->assert_equals($type,$this->column->map_raw_type());
  21. }
  22. public function assert_cast($type, $casted_value, $original_value)
  23. {
  24. $this->column->type = $type;
  25. $value = $this->column->cast($original_value,$this->conn);
  26. if ($original_value != null && ($type == Column::DATETIME || $type == Column::DATE))
  27. $this->assert_true($value instanceof DateTime);
  28. else
  29. $this->assert_same($casted_value,$value);
  30. }
  31. public function test_map_raw_type_dates()
  32. {
  33. $this->assert_mapped_type(Column::DATETIME,'datetime');
  34. $this->assert_mapped_type(Column::DATE,'date');
  35. }
  36. public function test_map_raw_type_integers()
  37. {
  38. $this->assert_mapped_type(Column::INTEGER,'integer');
  39. $this->assert_mapped_type(Column::INTEGER,'int');
  40. $this->assert_mapped_type(Column::INTEGER,'tinyint');
  41. $this->assert_mapped_type(Column::INTEGER,'smallint');
  42. $this->assert_mapped_type(Column::INTEGER,'mediumint');
  43. $this->assert_mapped_type(Column::INTEGER,'bigint');
  44. }
  45. public function test_map_raw_type_decimals()
  46. {
  47. $this->assert_mapped_type(Column::DECIMAL,'float');
  48. $this->assert_mapped_type(Column::DECIMAL,'double');
  49. $this->assert_mapped_type(Column::DECIMAL,'numeric');
  50. $this->assert_mapped_type(Column::DECIMAL,'dec');
  51. }
  52. public function test_map_raw_type_strings()
  53. {
  54. $this->assert_mapped_type(Column::STRING,'string');
  55. $this->assert_mapped_type(Column::STRING,'varchar');
  56. $this->assert_mapped_type(Column::STRING,'text');
  57. }
  58. public function test_map_raw_type_default_to_string()
  59. {
  60. $this->assert_mapped_type(Column::STRING,'bajdslfjasklfjlksfd');
  61. }
  62. public function test_map_raw_type_changes_integer_to_int()
  63. {
  64. $this->column->raw_type = 'integer';
  65. $this->column->map_raw_type();
  66. $this->assert_equals('int',$this->column->raw_type);
  67. }
  68. public function test_cast()
  69. {
  70. $datetime = new DateTime('2001-01-01');
  71. $this->assert_cast(Column::INTEGER,1,'1');
  72. $this->assert_cast(Column::INTEGER,1,'1.5');
  73. $this->assert_cast(Column::DECIMAL,1.5,'1.5');
  74. $this->assert_cast(Column::DATETIME,$datetime,'2001-01-01');
  75. $this->assert_cast(Column::DATE,$datetime,'2001-01-01');
  76. $this->assert_cast(Column::DATE,$datetime,$datetime);
  77. $this->assert_cast(Column::STRING,'bubble tea','bubble tea');
  78. }
  79. public function test_cast_leave_null_alone()
  80. {
  81. $types = array(
  82. Column::STRING,
  83. Column::INTEGER,
  84. Column::DECIMAL,
  85. Column::DATETIME,
  86. Column::DATE);
  87. foreach ($types as $type) {
  88. $this->assert_cast($type,null,null);
  89. }
  90. }
  91. public function test_empty_and_null_date_strings_should_return_null()
  92. {
  93. $column = new Column();
  94. $column->type = Column::DATE;
  95. $this->assert_equals(null,$column->cast(null,$this->conn));
  96. $this->assert_equals(null,$column->cast('',$this->conn));
  97. }
  98. public function test_empty_and_null_datetime_strings_should_return_null()
  99. {
  100. $column = new Column();
  101. $column->type = Column::DATETIME;
  102. $this->assert_equals(null,$column->cast(null,$this->conn));
  103. $this->assert_equals(null,$column->cast('',$this->conn));
  104. }
  105. }
  106. ?>