Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
ShoppingcartTest.php
1 <?php
2 /**
3  * Created by JetBrains PhpStorm.
4  * User: Andrey
5  * Date: 3/5/13
6  * Time: 2:30 PM
7  * To change this template use File | Settings | File Templates.
8  */
9 
10 Yii::import('shoppingcart.models');
11 
13 {
14 
15  public $items = array();
16  public $orders = array();
17  public $tmp_users = array();
18 
19  public function testToCart()
20  {
21  echo "<h3>Correct data test</h3>";
22  $counter = 0;
23  $success =0;
24  $error =0;
25  foreach($this->getItems(true) as $set){
26 
27  $item = new Item;
28  $item->setAttributes($set,false);
29 
30  try{
31  $this->assertTrue($item->save());
32  $success++;
33  }catch (Exception $e){
34  echo $e->getMessage()."<br/>";
35  $error++;
36  }
37  $counter++;
38  }
39  echo "<h3>".$counter." tests completed: ".$success." - success, ".$error." - error</h3>";
40 
41  echo "<h3>Incorrect data test</h3>";
42  $counter = 0;
43  $success =0;
44  $error =0;
45  foreach($this->getItems() as $set){
46 
47  $item = new Item;
48  $item->setAttributes($set,false);
49 
50  try{
51  $this->assertFalse($item->save());
52  $success++;
53  }catch (Exception $e){
54  echo $e->getMessage()."<br/>";
55  $error++;
56  }
57  $counter++;
58  }
59  echo "<h3>".$counter." tests completed: ".$success." - success, ".$error." - error</h3>";
60  }
61  public function testTmpUser()
62  {
63  echo "<h3>Correct data test</h3>";
64  $counter = 0;
65  $success =0;
66  $error =0;
67  foreach($this->getTmpUsers(true) as $set){
68 
69  $item = new TmpUser;
70  $item->setAttributes($set,false);
71 
72  try{
73  $this->assertTrue($item->save());
74  $success++;
75  }catch (Exception $e){
76  echo $e->getMessage()."<br/>";
77  $error++;
78  }
79  $counter++;
80  }
81  echo "<h3>".$counter." tests completed: ".$success." - success, ".$error." - error</h3>";
82 
83 
84  echo "<h3>Incorrect data test</h3>";
85  $counter = 0;
86  $success =0;
87  $error =0;
88 
89  foreach($this->getTmpUsers() as $set){
90 
91  $item = new TmpUser;
92  $item->setAttributes($set,false);
93 
94  try{
95  $this->assertFalse($item->save());
96  $success++;
97  }catch (Exception $e){
98  echo $e->getMessage()."<br/>";
99  $error++;
100  }
101  $counter++;
102  }
103  echo "<h3>".$counter." tests completed: ".$success." - success, ".$error." - error</h3>";
104  }
105 
106  public function getItems($correct = false){
107  $result = array();
108  if($correct){
109  for($i = 1;$i<501; $i++)
110  $result[]= array(
111  'price' => rand(0,$i),
112  'admin_email' => substr(md5(uniqid(rand(), true)),0,30).'@'.substr(md5(uniqid(rand(), true)),0,5).'.com',
113  'page_id' => rand(0,$i),
114  'title' => substr(md5(uniqid(rand(), true)),0,30),
115  'item_id' => rand(0,$i),
116  'order_id' => rand(0,$i),
117  'quantity' => rand(0,$i),
118  'referrer' => substr(md5(uniqid(rand(), true)),0,50),
119  'ip_address' => rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255),
120  );
121  }
122  if(!$correct){
123  for($i = 1;$i<250; $i++)
124  $result[]= array(
125  'price' => rand(-1,-$i-10),
126  'admin_email' => substr(md5(uniqid(rand(), true)),0,30).'@'.substr(md5(uniqid(rand(), true)),0,5).'.'.substr(md5(uniqid(rand(), true)),0,3),
127  'page_id' => rand(0,$i),
128  'title' => substr(md5(uniqid(rand(), true)),0,30),
129  'item_id' => rand(0,$i),
130  'order_id' => rand(0,$i),
131  'quantity' => rand(0,$i),
132  'referrer' => substr(md5(uniqid(rand(), true)),0,50),
133  'ip_address' => rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255),
134  );
135  for($i = 1;$i<252; $i++)
136  $result[]= array(
137  'price' => rand(0,$i),
138  'admin_email' => substr(md5(uniqid(rand(), true)),0,30).'@'.substr(md5(uniqid(rand(), true)),0,5).'.'.substr(md5(uniqid(rand(), true)),0,3),
139  'page_id' => rand(0,$i),
140  'title' => substr(md5(uniqid(rand(), true)),0,30),
141  'item_id' => rand(0,$i),
142  'order_id' => rand(0,$i),
143  'quantity' => rand(-1,-$i-10),
144  'referrer' => substr(md5(uniqid(rand(), true)),0,50),
145  'ip_address' => rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255),
146  );
147  }
148  return $result;
149  }
150 
151 
152  public function getTmpUsers($correct = false){
153  $result = array();
154  $format = Yii::app()->locale->getDateFormat();
155 
156  if($correct){
157  for($i = 1;$i<51; $i++){
158 
159  $result[]= array(
160  'salutation' => substr(md5(uniqid(rand(), true)),0,3),
161  'firstname' => substr(md5(uniqid(rand(), true)),0,12),
162  'lastname' => substr(md5(uniqid(rand(), true)),0,12),
163  'company' => substr(md5(uniqid(rand(), true)),0,12),
164  'birthday' => Yii::app()->dateFormatter->format($format, date('Y-m-d',time())),
165  'street' => substr(md5(uniqid(rand(), true)),0,12),
166  'zip_code' => substr(md5(uniqid(rand(), true)),0,12),
167  'city' => substr(md5(uniqid(rand(), true)),0,12),
168  'country' => substr(md5(uniqid(rand(), true)),0,12),
169  'telephone' => substr(md5(uniqid(rand(), true)),0,12),
170  'email' => substr(md5(uniqid(rand(), true)),0,6).'@'.substr(md5(uniqid(rand(), true)),0,4).'.'.substr(md5(uniqid(rand(), true)),0,3),
171  );
172  }
173  }
174  if(!$correct){
175  for($i = 1;$i<11; $i++){
176 
177  $result[]= array(
178  'salutation' => substr(md5(uniqid(rand(), true)),0,3),
179  'firstname' => substr(md5(uniqid(rand(), true)),0,12),
180  'lastname' => substr(md5(uniqid(rand(), true)),0,12),
181  'company' => substr(md5(uniqid(rand(), true)),0,12),
182  'birthday' => Yii::app()->dateFormatter->format($format, date('Y-m-d',time())),
183  'street' => substr(md5(uniqid(rand(), true)),0,12),
184  'zip_code' => substr(md5(uniqid(rand(), true)),0,12),
185  'city' => substr(md5(uniqid(rand(), true)),0,12),
186  'country' => substr(md5(uniqid(rand(), true)),0,12),
187  'telephone' => substr(md5(uniqid(rand(), true)),0,12),
188  'email' => substr(md5(uniqid(rand(), true)),0,6).'@'.substr(md5(uniqid(rand(), true)),0,4),
189  );
190  }
191  for($i = 1;$i<11; $i++){
192 
193  $result[]= array(
194  'salutation' => substr(md5(uniqid(rand(), true)),0,3),
195  'firstname' => '',
196  'lastname' => substr(md5(uniqid(rand(), true)),0,12),
197  'company' => substr(md5(uniqid(rand(), true)),0,12),
198  'birthday' => Yii::app()->dateFormatter->format($format, date('Y-m-d',time())),
199  'street' => substr(md5(uniqid(rand(), true)),0,12),
200  'zip_code' => substr(md5(uniqid(rand(), true)),0,12),
201  'city' => substr(md5(uniqid(rand(), true)),0,12),
202  'country' => substr(md5(uniqid(rand(), true)),0,12),
203  'telephone' => substr(md5(uniqid(rand(), true)),0,12),
204  'email' => substr(md5(uniqid(rand(), true)),0,6).'@'.substr(md5(uniqid(rand(), true)),0,4).'.ru',
205  );
206  }
207  for($i = 1;$i<11; $i++){
208 
209  $result[]= array(
210  'salutation' => substr(md5(uniqid(rand(), true)),0,3),
211  'firstname' => substr(md5(uniqid(rand(), true)),0,12),
212  'lastname' => '',
213  'company' => substr(md5(uniqid(rand(), true)),0,12),
214  'birthday' => Yii::app()->dateFormatter->format($format, date('Y-m-d',time())),
215  'street' => substr(md5(uniqid(rand(), true)),0,12),
216  'zip_code' => substr(md5(uniqid(rand(), true)),0,12),
217  'city' => substr(md5(uniqid(rand(), true)),0,12),
218  'country' => substr(md5(uniqid(rand(), true)),0,12),
219  'telephone' => substr(md5(uniqid(rand(), true)),0,12),
220  'email' => substr(md5(uniqid(rand(), true)),0,6).'@'.substr(md5(uniqid(rand(), true)),0,4).'.'.substr(md5(uniqid(rand(), true)),0,3),
221  );
222  }
223  for($i = 1;$i<21; $i++){
224 
225  $result[]= array(
226  'salutation' => substr(md5(uniqid(rand(), true)),0,3),
227  'firstname' => substr(md5(uniqid(rand(), true)),0,12),
228  'lastname' => substr(md5(uniqid(rand(), true)),0,12),
229  'company' => substr(md5(uniqid(rand(), true)),0,12),
230  'birthday' => date('Y-m-d',time()),
231  'street' => substr(md5(uniqid(rand(), true)),0,12),
232  'zip_code' => substr(md5(uniqid(rand(), true)),0,12),
233  'city' => substr(md5(uniqid(rand(), true)),0,12),
234  'country' => substr(md5(uniqid(rand(), true)),0,12),
235  'telephone' => substr(md5(uniqid(rand(), true)),0,12),
236  'email' => substr(md5(uniqid(rand(), true)),0,6).'@'.substr(md5(uniqid(rand(), true)),0,4).'.ru',
237  );
238  }
239  }
240 
241  return $result;
242  }
243 
244 }