Permalink
Browse files

Add POLYGON support to FixtureBuilder

  • Loading branch information...
1 parent f3fe212 commit 220b0075d011ddf52b0abd4d90bd042372601948 @ginatrapani ginatrapani committed Aug 31, 2012
Showing with 45 additions and 2 deletions.
  1. +15 −1 tests/fixtures/class.FixtureBuilder.php
  2. +30 −1 tests/fixtures/tests/TestOfFixtureBuilder.php
View
16 tests/fixtures/class.FixtureBuilder.php
@@ -231,6 +231,8 @@ public function buildData($table, $args = null) {
$column['value'] = $this->genDate();
} else if (preg_match('/^(point)/', $column['Type'])) {
$column['value'] = "GeometryFromText('Point" . $this->genPoint() . "')";
+ } else if (preg_match('/^(polygon)/', $column['Type'])) {
+ $column['value'] = "PolygonFromText('Polygon" . $this->genPolygon() . "')";
} else if (preg_match('/^(varchar|text|tinytext|mediumtext|longtext|blob)/', $column['Type'])) {
$column['value'] = $this->genVarchar();
}
@@ -250,6 +252,8 @@ public function buildData($table, $args = null) {
$values_string .= $value['function'];
} elseif(preg_match('/^GeometryFromText/',$value)) {
$values_string .= $value;
+ } elseif(preg_match('/^PolygonFromText/',$value)) {
+ $values_string .= $value;
} else {
array_push($values, $value);
$values_string .= '?';
@@ -367,7 +371,6 @@ public function genDecimal($values) {
/*
* Generates point value
- * @param str A 'point(lon,lat)'
* @return string
*/
public function genPoint() {
@@ -377,6 +380,17 @@ public function genPoint() {
}
/*
+ * Generates polygon value
+ * @return string
+ */
+ public function genPolygon() {
+ $first = mt_rand(1, 100);
+ $second = mt_rand(1, 100);
+ $third = mt_rand(1, 100);
+ return "($first $second $third)";
+ }
+
+ /*
* Generates a mysql date
* @param str A date increment or decrement (+3d, -1h, +7m, -2m), or a mysql date string '2010-06-20 16:22:25'
* @return str
View
31 tests/fixtures/tests/TestOfFixtureBuilder.php
@@ -65,6 +65,7 @@ public function setUp() {
'birthday date,' .
'numeric_ip_address int default 2015153756,' .
'a_point point,' .
+ 'a_polygon polygon,' .
'worth decimal(11,2) default 12.99' .
')');
}
@@ -99,6 +100,7 @@ public function testBuildData() {
// test point gen
$this->assertPattern("/GeometryFromText\('Point\(\d+ \d+\)'\)/",$builder->columns['a_point']);
+ $this->assertPattern("/PolygonFromText\('Polygon\(\d+ \d+ \d+\)'\)/",$builder->columns['a_polygon']);
$builder2 = FixtureBuilder::build(self::TEST_TABLE, array('test_id' => 2, 'fav_food' => 'hotdog'), true );
// auto inc id
@@ -164,6 +166,19 @@ public function testBuildData() {
$stmt = $this->pdo->query( 'select t.*, AsText(a_point) as text_point from ' . $this->test_table . ' as t where id = 5');
$data = $stmt->fetch();
$this->assertEqual('POINT(27.1 20.2)', $data['text_point']);
+
+ //set polygon
+ $date_fixture_data = array('test_id' => 6, 'a_polygon' =>
+ "PolygonFromText( 'Polygon((-0.213503 51.512805,-0.105303 51.512805,-0.105303 51.572068,-0.213503 51.572068, ".
+ "-0.213503 51.512805))')");
+ $builder6 = FixtureBuilder::build(self::TEST_TABLE, $date_fixture_data);
+ $mysql_date = strtotime( $builder3->columns['date_created'] );
+ $match_date = time() + (60 * 60 * 24);
+ $stmt = $this->pdo->query( 'select t.*, AsText(a_polygon) as text_polygon from ' . $this->test_table .
+ ' as t where id = 6');
+ $data = $stmt->fetch();
+ $this->assertEqual('POLYGON((-0.213503 51.512805,-0.105303 51.512805,-0.105303 51.572068,-0.213503 51.572068,'.
+ '-0.213503 51.512805))', $data['text_polygon']);
}
public function testDestroyData() {
@@ -209,7 +224,7 @@ public function testDescribeTable() {
$this->assertPattern('/Unable to describe table "tu_notable"/', $e->getMessage());
}
$columns = $this->builder->describeTable(self::TEST_TABLE);
- $this->assertEqual(count($columns), 12, 'column count valid');
+ $this->assertEqual(count($columns), 13, 'column count valid');
}
@@ -356,6 +371,20 @@ public function testGendata() {
if ($fail) {
$this->fail($fail);
}
+
+ // test genPolygon
+ $fail = null;
+ for($i = 0; $i < 1000; $i++) {
+ $polygon = $this->builder->genPolygon();
+ $values = preg_split('/\./', $polygon);
+ $matches = null;
+ preg_match("/\((\d+) (\d+)\)/", $subject, $matches, PREG_OFFSET_CAPTURE, 3);
+ if ($values[0] > 0 && $values[0] < 101) { $fail = "left value is not correct - " . $values[0]; break;}
+ if ($values[1] > 0 && $value[1] < 101) { $fail = "right value is not correct - " . $values[1]; break; }
+ }
+ if ($fail) {
+ $this->fail($fail);
+ }
}
public function _testDatesAreClose($date1, $date2) {

0 comments on commit 220b007

Please sign in to comment.