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