|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Parse\Test; |
| 4 | + |
| 5 | +use Parse\ParseGeoPoint; |
| 6 | +use Parse\ParsePolygon; |
| 7 | +use Parse\ParseObject; |
| 8 | +use Parse\ParseQuery; |
| 9 | + |
| 10 | +class ParsePolygonTest extends \PHPUnit_Framework_TestCase |
| 11 | +{ |
| 12 | + public static function setUpBeforeClass() |
| 13 | + { |
| 14 | + Helper::setUp(); |
| 15 | + } |
| 16 | + |
| 17 | + public function setUp() |
| 18 | + { |
| 19 | + Helper::clearClass('TestObject'); |
| 20 | + } |
| 21 | + |
| 22 | + public function tearDown() |
| 23 | + { |
| 24 | + Helper::tearDown(); |
| 25 | + } |
| 26 | + |
| 27 | + public function testPolygonWithPoints() |
| 28 | + { |
| 29 | + $openPoints = [[0,0],[0,1],[1,1],[1,0]]; |
| 30 | + $closedPoints = [[0,0],[0,1],[1,1],[1,0],[0,0]]; |
| 31 | + $polygon = new ParsePolygon($openPoints); |
| 32 | + |
| 33 | + $obj = ParseObject::create('TestObject'); |
| 34 | + $obj->set('polygon', $polygon); |
| 35 | + $obj->save(); |
| 36 | + |
| 37 | + // Query by open points |
| 38 | + $query = new ParseQuery('TestObject'); |
| 39 | + $query->equalTo('polygon', $polygon); |
| 40 | + |
| 41 | + $results = $query->find(); |
| 42 | + $actualPolygon = $results[0]->get('polygon'); |
| 43 | + |
| 44 | + $this->assertEquals(1, count($results)); |
| 45 | + $this->assertEquals($closedPoints, $actualPolygon->getCoordinates()); |
| 46 | + |
| 47 | + // Query by closed points |
| 48 | + $polygon = new ParsePolygon($closedPoints); |
| 49 | + $query = new ParseQuery('TestObject'); |
| 50 | + $query->equalTo('polygon', $polygon); |
| 51 | + |
| 52 | + $results = $query->find(); |
| 53 | + $actualPolygon = $results[0]->get('polygon'); |
| 54 | + |
| 55 | + $this->assertEquals(1, count($results)); |
| 56 | + $this->assertEquals($closedPoints, $actualPolygon->getCoordinates()); |
| 57 | + } |
| 58 | + |
| 59 | + public function testPolygonWithGeoPoints() |
| 60 | + { |
| 61 | + $p1 = new ParseGeoPoint(0, 0); |
| 62 | + $p2 = new ParseGeoPoint(0, 1); |
| 63 | + $p3 = new ParseGeoPoint(1, 1); |
| 64 | + $p4 = new ParseGeoPoint(1, 0); |
| 65 | + $p5 = new ParseGeoPoint(0, 0); |
| 66 | + |
| 67 | + $points = [$p1, $p2, $p3, $p4]; |
| 68 | + $openPoints = [[0,0],[0,1],[1,1],[1,0]]; |
| 69 | + $closedPoints = [[0,0],[0,1],[1,1],[1,0],[0,0]]; |
| 70 | + $polygon = new ParsePolygon($points); |
| 71 | + |
| 72 | + $obj = ParseObject::create('TestObject'); |
| 73 | + $obj->set('polygon', $polygon); |
| 74 | + $obj->save(); |
| 75 | + |
| 76 | + // Query by open points |
| 77 | + $query = new ParseQuery('TestObject'); |
| 78 | + $query->equalTo('polygon', $polygon); |
| 79 | + |
| 80 | + $results = $query->find(); |
| 81 | + $actualPolygon = $results[0]->get('polygon'); |
| 82 | + |
| 83 | + $this->assertEquals(1, count($results)); |
| 84 | + $this->assertEquals($closedPoints, $actualPolygon->getCoordinates()); |
| 85 | + |
| 86 | + // Query by closed points |
| 87 | + $polygon = new ParsePolygon($closedPoints); |
| 88 | + $query = new ParseQuery('TestObject'); |
| 89 | + $query->equalTo('polygon', $polygon); |
| 90 | + |
| 91 | + $results = $query->find(); |
| 92 | + $actualPolygon = $results[0]->get('polygon'); |
| 93 | + |
| 94 | + $this->assertEquals(1, count($results)); |
| 95 | + $this->assertEquals($closedPoints, $actualPolygon->getCoordinates()); |
| 96 | + } |
| 97 | + |
| 98 | + public function testPolygonMinimum() |
| 99 | + { |
| 100 | + $this->setExpectedException( |
| 101 | + '\Parse\ParseException', |
| 102 | + 'Polygon must have at least 3 GeoPoints or Points' |
| 103 | + ); |
| 104 | + $polygon = new ParsePolygon([[0,0]]); |
| 105 | + $obj = ParseObject::create('TestObject'); |
| 106 | + $obj->set('polygon', $polygon); |
| 107 | + $obj->save(); |
| 108 | + } |
| 109 | + |
| 110 | + public function testPolygonInvalidInput() |
| 111 | + { |
| 112 | + $this->setExpectedException( |
| 113 | + '\Parse\ParseException', |
| 114 | + 'Coordinates must be an Array' |
| 115 | + ); |
| 116 | + $polygon = new ParsePolygon(1234); |
| 117 | + $obj = ParseObject::create('TestObject'); |
| 118 | + $obj->set('polygon', $polygon); |
| 119 | + $obj->save(); |
| 120 | + } |
| 121 | + |
| 122 | + public function testPolygonInvalidArray() |
| 123 | + { |
| 124 | + $this->setExpectedException( |
| 125 | + '\Parse\ParseException', |
| 126 | + 'Coordinates must be an Array of GeoPoints or Points' |
| 127 | + ); |
| 128 | + $polygon = new ParsePolygon([['str1'],['str2'],['str3']]); |
| 129 | + $obj = ParseObject::create('TestObject'); |
| 130 | + $obj->set('polygon', $polygon); |
| 131 | + $obj->save(); |
| 132 | + } |
| 133 | + |
| 134 | + public function testPolygonContains() |
| 135 | + { |
| 136 | + $points1 = [[0,0],[0,1],[1,1],[1,0]]; |
| 137 | + $points2 = [[0,0],[0,2],[2,2],[2,0]]; |
| 138 | + $points3 = [[10,10],[10,15],[15,15],[15,10],[10,10]]; |
| 139 | + |
| 140 | + $polygon1 = new ParsePolygon($points1); |
| 141 | + $polygon2 = new ParsePolygon($points2); |
| 142 | + $polygon3 = new ParsePolygon($points3); |
| 143 | + |
| 144 | + $obj1 = ParseObject::create('TestObject'); |
| 145 | + $obj2 = ParseObject::create('TestObject'); |
| 146 | + $obj3 = ParseObject::create('TestObject'); |
| 147 | + |
| 148 | + $obj1->set('polygon', $polygon1); |
| 149 | + $obj2->set('polygon', $polygon2); |
| 150 | + $obj3->set('polygon', $polygon3); |
| 151 | + |
| 152 | + ParseObject::saveAll([$obj1, $obj2, $obj3]); |
| 153 | + |
| 154 | + $point = new ParseGeoPoint(0.5, 0.5); |
| 155 | + $query = new ParseQuery('TestObject'); |
| 156 | + $query->polygonContains('polygon', $point); |
| 157 | + $results = $query->find(); |
| 158 | + $this->assertEquals(2, count($results)); |
| 159 | + } |
| 160 | + |
| 161 | + public function testPolygonContainsInvalidInput() |
| 162 | + { |
| 163 | + $this->setExpectedException( |
| 164 | + '\Parse\ParseException', |
| 165 | + 'bad $geoIntersect value; $point should be GeoPoint' |
| 166 | + ); |
| 167 | + $points = [[0,0],[0,1],[1,1],[1,0]]; |
| 168 | + $polygon = new ParsePolygon($points); |
| 169 | + $obj = ParseObject::create('TestObject'); |
| 170 | + $obj->set('polygon', $polygon); |
| 171 | + $obj->save(); |
| 172 | + |
| 173 | + $query = new ParseQuery('TestObject'); |
| 174 | + $query->polygonContains('polygon', 1234); |
| 175 | + $results = $query->find(); |
| 176 | + } |
| 177 | +} |
0 commit comments