|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\UX\Map\Tests; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\UX\Map\Exception\InvalidArgumentException; |
| 16 | +use Symfony\UX\Map\InfoWindow; |
| 17 | +use Symfony\UX\Map\Point; |
| 18 | +use Symfony\UX\Map\Polygon; |
| 19 | + |
| 20 | +class PolygonTest extends TestCase |
| 21 | +{ |
| 22 | + public function testToArray() |
| 23 | + { |
| 24 | + $point1 = new Point(1.1, 2.2); |
| 25 | + $point2 = new Point(3.3, 4.4); |
| 26 | + |
| 27 | + $infoWindow = new InfoWindow('info content'); |
| 28 | + |
| 29 | + $polygon = new Polygon( |
| 30 | + points: [$point1, $point2], |
| 31 | + title: 'Test Polygon', |
| 32 | + infoWindow: $infoWindow, |
| 33 | + extra: ['foo' => 'bar'], |
| 34 | + id: 'poly1' |
| 35 | + ); |
| 36 | + |
| 37 | + $array = $polygon->toArray(); |
| 38 | + $this->assertSame([ |
| 39 | + 'points' => [['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]], |
| 40 | + 'title' => 'Test Polygon', |
| 41 | + 'infoWindow' => [ |
| 42 | + 'headerContent' => 'info content', |
| 43 | + 'content' => null, |
| 44 | + 'position' => null, |
| 45 | + 'opened' => false, |
| 46 | + 'autoClose' => true, |
| 47 | + 'extra' => $array['infoWindow']['extra'], |
| 48 | + ], |
| 49 | + 'extra' => ['foo' => 'bar'], |
| 50 | + 'id' => 'poly1', |
| 51 | + ], $array); |
| 52 | + } |
| 53 | + |
| 54 | + public function testToArrayMultidimensional() |
| 55 | + { |
| 56 | + $point1 = new Point(1.1, 2.2); |
| 57 | + $point2 = new Point(3.3, 4.4); |
| 58 | + $point3 = new Point(5.5, 6.6); |
| 59 | + |
| 60 | + $polygon = new Polygon( |
| 61 | + points: [[$point1, $point2], [$point3]], |
| 62 | + ); |
| 63 | + |
| 64 | + $array = $polygon->toArray(); |
| 65 | + $this->assertSame([ |
| 66 | + 'points' => [ |
| 67 | + [['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]], |
| 68 | + [['lat' => 5.5, 'lng' => 6.6]], |
| 69 | + ], |
| 70 | + 'title' => null, |
| 71 | + 'infoWindow' => null, |
| 72 | + 'extra' => $array['extra'], |
| 73 | + 'id' => null, |
| 74 | + ], $array); |
| 75 | + } |
| 76 | + |
| 77 | + public function testFromArray() |
| 78 | + { |
| 79 | + $data = [ |
| 80 | + 'points' => [ |
| 81 | + ['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4], |
| 82 | + ], |
| 83 | + 'title' => 'Test Polygon', |
| 84 | + 'infoWindow' => ['content' => 'info content'], |
| 85 | + 'extra' => ['foo' => 'bar'], |
| 86 | + 'id' => 'poly1', |
| 87 | + ]; |
| 88 | + |
| 89 | + $polygon = Polygon::fromArray($data); |
| 90 | + |
| 91 | + $this->assertInstanceOf(Polygon::class, $polygon); |
| 92 | + |
| 93 | + $array = $polygon->toArray(); |
| 94 | + $this->assertSame([ |
| 95 | + 'points' => [['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]], |
| 96 | + 'title' => 'Test Polygon', |
| 97 | + 'infoWindow' => [ |
| 98 | + 'headerContent' => null, |
| 99 | + 'content' => 'info content', |
| 100 | + 'position' => null, |
| 101 | + 'opened' => false, |
| 102 | + 'autoClose' => true, |
| 103 | + 'extra' => $array['infoWindow']['extra'], |
| 104 | + ], |
| 105 | + 'extra' => ['foo' => 'bar'], |
| 106 | + 'id' => 'poly1', |
| 107 | + ], $array); |
| 108 | + } |
| 109 | + |
| 110 | + public function testFromArrayMultidimensional() |
| 111 | + { |
| 112 | + $data = [ |
| 113 | + 'points' => [ |
| 114 | + [['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]], |
| 115 | + [['lat' => 5.5, 'lng' => 6.6]], |
| 116 | + ], |
| 117 | + 'title' => 'Test Polygon', |
| 118 | + 'infoWindow' => ['content' => 'info content'], |
| 119 | + 'extra' => ['foo' => 'bar'], |
| 120 | + 'id' => 'poly1', |
| 121 | + ]; |
| 122 | + |
| 123 | + $polygon = Polygon::fromArray($data); |
| 124 | + |
| 125 | + $this->assertInstanceOf(Polygon::class, $polygon); |
| 126 | + |
| 127 | + $array = $polygon->toArray(); |
| 128 | + $this->assertSame([ |
| 129 | + 'points' => [ |
| 130 | + [['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]], |
| 131 | + [['lat' => 5.5, 'lng' => 6.6]], |
| 132 | + ], |
| 133 | + 'title' => 'Test Polygon', |
| 134 | + 'infoWindow' => [ |
| 135 | + 'headerContent' => null, |
| 136 | + 'content' => 'info content', |
| 137 | + 'position' => null, |
| 138 | + 'opened' => false, |
| 139 | + 'autoClose' => true, |
| 140 | + 'extra' => $array['infoWindow']['extra'], |
| 141 | + ], |
| 142 | + 'extra' => ['foo' => 'bar'], |
| 143 | + 'id' => 'poly1', |
| 144 | + ], $array); |
| 145 | + } |
| 146 | + |
| 147 | + public function testFromArrayThrowsExceptionIfPointsMissing() |
| 148 | + { |
| 149 | + $this->expectException(InvalidArgumentException::class); |
| 150 | + Polygon::fromArray(['invalid' => 'No points']); |
| 151 | + } |
| 152 | +} |
0 commit comments