Development

/plugins/swToolboxPlugin/sf1.2/trunk/test/unit/swGmapAddressWidgetTest.php

You must first sign up to be able to contribute.

root/plugins/swToolboxPlugin/sf1.2/trunk/test/unit/swGmapAddressWidgetTest.php

Revision 15935, 4.7 kB (checked in by rande, 2 years ago)

add unit test

Line 
1 <?php
2
3 include(dirname(__FILE__).'/../bootstrap/unit.php');
4
5 $t = new lime_test(9, new lime_output_color());
6
7
8 class NestedMapForm extends sfForm
9 {
10   public function configure()
11   {
12     $this->widgetSchema['nested_map']    = new swWidgetFormGMapAddress;
13     $this->validatorSchema['nested_map'] = new swValidatorGMapAddress;
14   }
15 }
16
17 class PrimaryForm extends sfForm
18 {
19   public function configure()
20   {
21     $this->widgetSchema->setNameFormat('primary_form[%s]');
22     
23     $this->widgetSchema['map']    = new swWidgetFormGMapAddress;
24     $this->validatorSchema['map'] = new swValidatorGMapAddress;
25     
26     $this->embedForm('nested_form', new NestedMapForm);
27   }
28  
29   public function save()
30   {
31     $address = $this->getValue('map');
32     
33     $this->myObject->setAddress($address['address']);
34     $this->myObject->setLng($address['lng']);
35     $this->myObject->setLat($address['lat']);
36   }
37 }
38
39 $form = new PrimaryForm;
40
41 $result =<<<FORM
42 <tr>
43   <th>Map</th>
44   <td>
45       <div id="primary_form_map" class="sw-gmap-widget">
46         <label for="primary_form_map_address">Address</label>
47   <input style="width: 300px;" type="text" name="primary_form[map][address]" id="primary_form_map_address" />
48
49  <input type="submit" value="lookup address"  id="primary_form_map_lookup" /> <br />
50        <label for="primary_form_map_lat">Lat</label>
51   <input readonly="1" type="text" name="primary_form[map][lat]" id="primary_form_map_lat" />
52
53  - <label for="primary_form_map_lng">Lng</label>
54   <input readonly="1" type="text" name="primary_form[map][lng]" id="primary_form_map_lng" />
55
56  <br />
57          <div id="primary_form_map_map" style="width: 500px; height: 300px"></div>
58        </div>
59       <script>
60         jQuery(window).bind("load", function() {
61           new swGmapWidget({
62             lng: "primary_form_map_lng",
63             lat: "primary_form_map_lat",
64             address: "primary_form_map_address",
65             lookup: "primary_form_map_lookup",
66             map: "primary_form_map_map"
67           });
68         })
69       </script>
70     </td>
71 </tr>
72 <tr>
73   <th>Nested form</th>
74   <td><table>
75   <tr>
76   <th>Nested map</th>
77   <td>
78       <div id="primary_form_nested_form_nested_map" class="sw-gmap-widget">
79         <label for="primary_form_nested_form_nested_map_address">Address</label>
80   <input style="width: 300px;" type="text" name="primary_form[nested_form][nested_map][address]" id="primary_form_nested_form_nested_map_address" />
81
82  <input type="submit" value="lookup address"  id="primary_form_nested_form_nested_map_lookup" /> <br />
83        <label for="primary_form_nested_form_nested_map_lat">Lat</label>
84   <input readonly="1" type="text" name="primary_form[nested_form][nested_map][lat]" id="primary_form_nested_form_nested_map_lat" />
85
86  - <label for="primary_form_nested_form_nested_map_lng">Lng</label>
87   <input readonly="1" type="text" name="primary_form[nested_form][nested_map][lng]" id="primary_form_nested_form_nested_map_lng" />
88
89  <br />
90          <div id="primary_form_nested_form_nested_map_map" style="width: 500px; height: 300px"></div>
91        </div>
92       <script>
93         jQuery(window).bind("load", function() {
94           new swGmapWidget({
95             lng: "primary_form_nested_form_nested_map_lng",
96             lat: "primary_form_nested_form_nested_map_lat",
97             address: "primary_form_nested_form_nested_map_address",
98             lookup: "primary_form_nested_form_nested_map_lookup",
99             map: "primary_form_nested_form_nested_map_map"
100           });
101         })
102       </script>
103     </td>
104 </tr>
105 </table></td>
106 </tr>
107
108 FORM;
109
110 $t->cmp_ok($result, '==', $form->render(), 'html output is ok');
111
112
113 $params = array(
114   'map' => array('lat' => 39, 'lng' =>  40),
115   'nested_form' => array('map' => array('lat' => 200, 'lng' =>  200),)
116 );
117
118 $form->bind($params);
119
120 $errors = $form->getErrorSchema()->getErrors();
121
122 $t->ok(isset($errors['map']['address']), 'address error presents');
123 $t->ok(!isset($errors['map']['lat']), 'lat error not presents');
124 $t->ok(!isset($errors['map']['lng']), 'lng error not presents');
125
126 $t->isa_ok($errors['nested_form'], 'sfValidatorErrorSchema', 'nested error instanceof sfValidatorErrorSchema');
127 $nested_errors = $errors['nested_form']->getErrors();
128
129 $t->ok(isset($nested_errors['nested_map']['address']), 'address error presents');
130 $t->ok(isset($nested_errors['nested_map']['lat']), 'lat error presents');
131 $t->ok(isset($nested_errors['nested_map']['lng']), 'lng error presents');
132
133
134 $params = array(
135   'map' => array('lat' => 39, 'lng' =>  40, 'address' => 'la cantine, 151 rue montmatre, paris'),
136   'nested_form' => array('nested_map' => array('lat' => 39, 'lng' =>  40, 'address' => 'la cantine, 151 rue montmatre, paris'),)
137 );
138
139 $form->bind($params);
140 $t->cmp_ok(count($form->getErrorSchema()->getErrors()), '==', 0, 'no error form is valid');
141
Note: See TracBrowser for help on using the browser.

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting.
Sensio Labs also supports several large Open-Source projects.