forked from luka8088/phops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
845 lines (695 loc) · 24.5 KB
/
test.php
File metadata and controls
845 lines (695 loc) · 24.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
<?php
/*
* This software is the property of its authors.
* See the copyright.txt file for more details.
*
*/
class unitTestEnvironmentException extends safeException {}
class unitTestRequiredException extends safeException {}
function runSetupTests () {
$result = (object) array();
$flushResult = function () use ($result) {
if (isset($_SERVER['setupTest_result']))
file_put_contents($_SERVER['setupTest_result'], json_encode($result) . "\n");
};
$flushResult();
$timestamp = microtime(true);
echo 'Listing files ..';
$files = array();
$setupTest_excludePath = array();
if (isset($_SERVER['setupTest_excludePath'])) {
$addPaths = function ($paths) use (&$setupTest_excludePath, &$addPaths) {
if (is_array($paths) || is_object($paths))
foreach ($paths as $path)
$addPaths($path);
else
$setupTest_excludePath[] = rtrim(str_replace(array('\\', '/'), array('/', '/'), $paths), '/') . '/';
};
$addPaths($_SERVER['setupTest_excludePath']);
}
$isExcluded = function ($file) use ($setupTest_excludePath) {
$sFile = str_replace(array('\\', '/'), array('/', '/'), $file);
foreach ($setupTest_excludePath as $excludePath)
if (substr($sFile, 0, strlen($excludePath)) == $excludePath)
return true;
return false;
};
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($_SERVER['basePath'])) as $file) {
if (microtime(true) > $timestamp + 1) {
$timestamp = microtime(true);
echo '.';
}
if ($isExcluded($file))
continue;
if (in_array($file->getExtension(), array('php'))) {
$fileContent = file_get_contents($file);
if (preg_match('/(?si)function\s*setuptest/', $fileContent) == 0)
continue;
$files[] = $file;
}
}
echo ' ' . count($files) . "\n";
usort($files, function ($lhs, $rhs) {
return filemtime($rhs) - filemtime($lhs);
});
echo 'Including files ..';
$i = 0;
// php include can overwrite variable values so we need to isolate include in a function
$includeFile = function ($file) {
ob_start();
include_once (string) $file;
ob_end_clean();
};
foreach ($files as $file) {
if (microtime(true) > $timestamp + 1) {
$timestamp = microtime(true);
echo '.';
}
if (in_array(realpath($file), get_included_files()))
continue;
$i++;
$includeFile($file);
}
echo ' ' . $i . "\n";
$tests = array();
echo 'Listing tests ..';
$functions = get_defined_functions();
foreach (array_merge($functions['internal'], $functions['user']) as $function) {
if (microtime(true) > $timestamp + 1) {
$timestamp = microtime(true);
echo '.';
}
if (substr(strtolower($function), 0, 9) == 'setuptest')
$tests[] = $function;
}
foreach (get_declared_classes() as $class) {
foreach (get_class_methods($class) as $method) {
if (microtime(true) > $timestamp + 1) {
$timestamp = microtime(true);
echo '.';
}
$reflector = new ReflectionMethod($class, $method);
if ($reflector->getDeclaringClass()->getName() != $class)
continue;
if (substr(strtolower($method), 0, 9) == 'setuptest')
$tests[] = array($class, $method);
}
}
echo ' ' . count($tests) . "\n";
$result->tests = (object) array(
'count' => count($tests),
'all' => array(),
'successes' => array(),
'failures' => array(),
'errors' => array(),
);
usort($tests, function ($lhs, $rhs) {
$lhsReflection = is_string($lhs) ? new ReflectionFunction($lhs) : new ReflectionMethod($lhs[0], $lhs[1]);
$rhsReflection = is_string($rhs) ? new ReflectionFunction($rhs) : new ReflectionMethod($rhs[0], $rhs[1]);
return filemtime($rhsReflection->getFileName()) - filemtime($lhsReflection->getFileName());
});
echo "\nRunning " . count($tests) . " tests:\n";
foreach ($tests as $key => $test) {
$id = (is_string($test) ? $test : $test[0] . '::' . $test[1]) . '()';
$result->tests->all[$id] = (object) array('status' => 'unknown');
}
$flushResult();
do {
$unhandledCount = count($tests);
foreach ($tests as $key => $test) {
$id = (is_string($test) ? $test : $test[0] . '::' . $test[1]) . '()';
echo ' ' . $id;
try {
call_user_func($test);
echo ": success";
$result->tests->all[$id]->status = 'success';
$result->tests->successes[] = $id;
isSetupTestRun($test, true);
} catch (Exception $e) {
echo ": failure - " . $e->getMessage();
$result->tests->all[$id]->status = 'failure';
$result->tests->all[$id]->report = error_report($e);
$result->tests->all[$id]->message = $e->getMessage();
$result->tests->all[$id]->trace = (string) $e;
$result->tests->failures[] = $id;
isSetupTestRun($test, false);
}
unset($tests[$key]);
echo "\n";
$flushResult();
}
} while (count($tests) > 0 && count($tests) < $unhandledCount);
$flushResult();
echo "\nResults: \n";
echo " Total: " . $result->tests->count . "\n";
echo " Success: " . count($result->tests->successes) . "\n";
echo " Failure: " . count($result->tests->failures) . "\n";
echo " Errors: " . count($result->tests->errors) . "\n";
if (count($result->tests->successes) != $result->tests->count)
echo "\nTests completed with failures, errors or skipped tests !\n\n";
else
echo "\nEverything completed successfully !\n\n";
echo "Done\n";
}
function isSetupTestRun ($test, $set = null) {
static $runedTests = array();
$id = (is_string($test) ? $test : $test[0] . '::' . $test[1]) . '()';
if (isset($set))
$runedTests[$id] = $set;
return isset($runedTests[$id]) ? $runedTests[$id] : null;
}
function runUnitTests () {
$result = (object) array();
$flushResult = function () use ($result) {
if (isset($_SERVER['unitTest_result']))
file_put_contents($_SERVER['unitTest_result'], json_encode($result) . "\n");
};
$flushResult();
$timestamp = microtime(true);
echo 'Listing files ..';
$files = array();
$unitTest_excludePath = array();
if (isset($_SERVER['unitTest_excludePath'])) {
$addPaths = function ($paths) use (&$unitTest_excludePath, &$addPaths) {
if (is_array($paths) || is_object($paths))
foreach ($paths as $path)
$addPaths($path);
else
$unitTest_excludePath[] = rtrim(str_replace(array('\\', '/'), array('/', '/'), $paths), '/') . '/';
};
$addPaths($_SERVER['unitTest_excludePath']);
}
$isExcluded = function ($file) use ($unitTest_excludePath) {
$sFile = str_replace(array('\\', '/'), array('/', '/'), $file);
foreach ($unitTest_excludePath as $excludePath)
if (substr($sFile, 0, strlen($excludePath)) == $excludePath)
return true;
return false;
};
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($_SERVER['basePath'])) as $file) {
if (microtime(true) > $timestamp + 1) {
$timestamp = microtime(true);
echo '.';
}
if ($isExcluded($file))
continue;
if (in_array($file->getExtension(), array('php'))) {
$fileContent = file_get_contents($file);
if (preg_match('/(?si)function\s*unittest/', $fileContent) == 0)
continue;
$files[] = $file;
}
}
echo ' ' . count($files) . "\n";
usort($files, function ($lhs, $rhs) {
return filemtime($rhs) - filemtime($lhs);
});
echo 'Including files ..';
$i = 0;
// php include can overwrite variable values so we need to isolate include in a function
$includeFile = function ($file) {
ob_start();
include_once (string) $file;
ob_end_clean();
};
foreach ($files as $file) {
if (microtime(true) > $timestamp + 1) {
$timestamp = microtime(true);
echo '.';
}
if (in_array(realpath($file), get_included_files()))
continue;
$i++;
$includeFile($file);
}
echo ' ' . $i . "\n";
$tests = array();
echo 'Listing tests ..';
$functions = get_defined_functions();
foreach (array_merge($functions['internal'], $functions['user']) as $function) {
if (microtime(true) > $timestamp + 1) {
$timestamp = microtime(true);
echo '.';
}
if (substr(strtolower($function), 0, 8) == 'unittest')
$tests[] = $function;
}
foreach (get_declared_classes() as $class) {
foreach (get_class_methods($class) as $method) {
if (microtime(true) > $timestamp + 1) {
$timestamp = microtime(true);
echo '.';
}
$reflector = new ReflectionMethod($class, $method);
if ($reflector->getDeclaringClass()->getName() != $class)
continue;
if (substr(strtolower($method), 0, 8) == 'unittest' && !in_array("$class::$method", array(
'unitTest_webBrowser::unitTestElement',
'unitTest_element::unitTestElement',
'unitTest_webContext::unitTestElement',
'unitTest_webBrowser::unitTestElements',
'unitTest_element::unitTestElements',
'unitTest_webContext::unitTestElements',
)))
$tests[] = array($class, $method);
}
}
echo ' ' . count($tests) . "\n";
$result->tests = (object) array(
'count' => count($tests),
'all' => array(),
'successes' => array(),
'failures' => array(),
'errors' => array(),
);
usort($tests, function ($lhs, $rhs) {
$lhsReflection = is_string($lhs) ? new ReflectionFunction($lhs) : new ReflectionMethod($lhs[0], $lhs[1]);
$rhsReflection = is_string($rhs) ? new ReflectionFunction($rhs) : new ReflectionMethod($rhs[0], $rhs[1]);
return filemtime($rhsReflection->getFileName()) - filemtime($lhsReflection->getFileName());
});
echo "\nRunning " . count($tests) . " tests:\n";
foreach ($tests as $key => $test) {
$id = (is_string($test) ? $test : $test[0] . '::' . $test[1]) . '()';
$result->tests->all[$id] = (object) array('status' => 'unknown');
}
$flushResult();
do {
$unhandledCount = count($tests);
foreach ($tests as $key => $test) {
$id = (is_string($test) ? $test : $test[0] . '::' . $test[1]) . '()';
echo ' ' . $id;
try {
call_user_func($test);
echo ": success";
$result->tests->all[$id]->status = 'success';
$result->tests->successes[] = $id;
isUnitTestRun($test, true);
} catch (unitTestRequiredException $e) {
echo ": delayed";
$tests[] = $test;
} catch (unitTestEnvironmentException $e) {
echo ": error";
$result->tests->all[$id]->status = 'error';
$result->tests->all[$id]->report = error_report($e);
$result->tests->all[$id]->message = $e->getMessage();
$result->tests->all[$id]->trace = (string) $e;
$result->tests->errors[] = $id;
isUnitTestRun($test, false);
} catch (Exception $e) {
echo ": failure - " . $e->getMessage();
$result->tests->all[$id]->status = 'failure';
$result->tests->all[$id]->report = error_report($e);
$result->tests->all[$id]->message = $e->getMessage();
$result->tests->all[$id]->trace = (string) $e;
$result->tests->failures[] = $id;
isUnitTestRun($test, false);
}
unset($tests[$key]);
echo "\n";
$flushResult();
}
} while (count($tests) > 0 && count($tests) < $unhandledCount);
$flushResult();
echo "\nResults: \n";
echo " Total: " . $result->tests->count . "\n";
echo " Success: " . count($result->tests->successes) . "\n";
echo " Failure: " . count($result->tests->failures) . "\n";
echo " Errors: " . count($result->tests->errors) . "\n";
if (count($result->tests->successes) != $result->tests->count)
echo "\nTests completed with failures, errors or skipped tests !\n\n";
else
echo "\nEverything completed successfully !\n\n";
echo "Done\n";
}
function isUnitTestRun ($test, $set = null) {
static $runedTests = array();
$id = (is_string($test) ? $test : $test[0] . '::' . $test[1]) . '()';
if (isset($set))
$runedTests[$id] = $set;
return isset($runedTests[$id]) ? $runedTests[$id] : null;
}
function requireUnitTest () {
$test = func_get_args();
$id = (is_string($test) ? $test : $test[0] . '::' . $test[1]) . '()';
if (!isUnitTestRun($test))
throw new unitTestRequiredException("Unit test $id required");
}
function webBrowser () {
static $browser;
if (!isset($browser)) {
assertTrue(isset($_SERVER['unitTest_wdUrl']));
$browser = new unitTest_webBrowser($_SERVER['unitTest_wdUrl']);
}
return $browser;
}
class unitTest_webContext {
protected $browser;
protected $context;
function __get ($name) {
return $this->element($name);
}
function __set ($name, $value) {
assertTrue(false);
}
function element ($name) {
$element = $this->_element($name);
version_assert and assertTrue($element instanceof unitTest_element);
version_assert and $i = 0;
while ($element->hasAttribute('data-alias-this')) {
version_assert and $i++;
version_assert and assertTrue($i < 64, 'Infinite loop detected');
$element = $element->query('//*[@data-mangle="' . $element->attribute('data-alias-this')->value . '"]')->one();
}
return $element;
}
function _element ($name) {
version_assert and assertTrue(preg_match('/(?i)^[a-z0-9_\-\[\]]+$/', $name) > 0, "'$name' is not a valid name");
$this->browser->synchronize();
if (false) {
while (true) {
$elements = $allElements->query('self::*[@data-element="' . $name . '"]');
if (count($elements) == 0) {
$allElements = $allElements->query('./*[descendant-or-self::*/@data-element]');
if (count($allElements) == 0)
break;
continue;
}
return $elements;
}
}
// temporary optimization
$elements = $this->query('//*[@data-element="' . $name . '" or @name="' . $name . '"]');
if (count($elements) == 1)
return $elements->one();
if (true) {
$allElements = $this->query('./*');
while (true) {
$elements = $allElements->query('self::*[@data-element="' . $name . '" or @name="' . $name . '"]');
if (count($elements) == 0) {
$allElements = $allElements->query('./*[descendant-or-self::*/@data-element or descendant-or-self::*/@name]');
if (count($allElements) == 0)
break;
continue;
}
version_assert and assertTrue(count($elements) === 1, 'found ' . count($elements) . " elements, 1 expected");
return $elements->one();
}
}
assertTrue(false, "Undefined " . get_called_class() . "->$name");
}
function hasElement ($name) {
version_assert and assertTrue(preg_match('/(?i)^[a-z0-9_\-\[\]]+$/', $name) > 0, "'$name' is not a valid name");
$this->browser->synchronize();
if (false) {
while (true) {
$elements = $allElements->query('self::*[@data-element="' . $name . '"]');
if (count($elements) == 0) {
$allElements = $allElements->query('./*[descendant-or-self::*/@data-element]');
if (count($allElements) == 0)
break;
continue;
}
return true;
}
}
// temporary optimization
$elements = $this->query('//*[@data-element="' . $name . '" or @name="' . $name . '"]');
if (count($elements) == 1)
return true;
if (true) {
$allElements = $this->query('./*');
while (true) {
$elements = $allElements->query('self::*[@data-element="' . $name . '" or @name="' . $name . '"]');
if (count($elements) == 0) {
$allElements = $allElements->query('./*[descendant-or-self::*/@data-element or descendant-or-self::*/@name]');
if (count($allElements) == 0)
break;
continue;
}
return true;
}
}
return false;
}
function query ($query) {
$using = 'cssSelector';
if (in_array($query, array('.', '..')) || preg_match('/\/|\@|\:\:|\.\./', $query))
$using = 'xpath';
$results = $this->context->findElements(call_user_func(array('WebDriverBy', $using), $query));
$wrappedResults = new unitTest_elements();
foreach ($results as $result)
$wrappedResults[] = new unitTest_element($this->browser, $result);
return $wrappedResults;
}
function queryOne ($query) {
$results = $this->query($query);
assertTrue(count($results) == 1, 'found ' . count($results) . ' results (1 expected)');
return $results[0];
}
function waitForQuery ($query) {
$t = microtime(true);
// todo: is 30 seconds too much ? or not enough ? or wait in some different way ?
while (microtime(true) < $t + 30) {
$results = $this->query($query);
if (count($results) > 0)
return;
foreach ($results as $result)
return;
usleep(100000);
}
throw new Exception("Timeout waiting for query");
}
function unitTestElement ($id) {
$results = $this->query("[data-unit-test-element=$id]");
if (count($results) == 0)
$results = $this->query("[unit-test-element=$id]");
assertTrue(count($results) == 1, 'found ' . count($results) . ' results (1 expected)');
return $results[0];
}
function unitTestElements ($id) {
$results = $this->query("[data-unit-test-element=$id]");
if (count($results) == 0)
$results = $this->query("[unit-test-element=$id]");
//having empty list should not be an issue
//assertTrue(count($results) > 0, 'found ' . count($results) . ' results (more then 0 expected)');
return $results;
}
function waitForUnitTestElements ($id) {
$this->waitForQuery("[data-unit-test-element=$id], [unit-test-element=$id]");
}
function click () {
$context = $this->context;
$this->_ensureVisible(function () use ($context) {
$context->click();
});
assertTrue(!$this->browser->hasError(), 'Error in page: ' . $this->browser->errorMessage());
return $this;
}
function execute ($script, $arguments = array()) {
foreach ($arguments as $key => $value)
if (is_object($value) && $value instanceof unitTest_element)
$arguments[$key] = (object) array('ELEMENT' => $value->context->getID());
// hack: double quotes bug
$script = " var arguments_BaU4UfAZJ7iYW2VmMwpDtpXK = arguments; return eval('(function () { "
. "arguments = arguments_BaU4UfAZJ7iYW2VmMwpDtpXK; ' + unescape('" . rawurlencode($script) . "') + '})()');";
return $this->browser->context->executeScript($script, $arguments);
}
function exists () {
try {
$this->_get_tag();
return true;
} catch (ObsoleteElementWebDriverError $e) {
return false;
}
assertTrue(false);
}
}
class unitTest_webBrowser extends unitTest_webContext {
private $driver;
function __construct ($url) {
try {
$this->driver = new WebDriver($url, array(WebDriverCapabilityType::BROWSER_NAME => 'firefox'));
$this->context = $this->driver;
} catch (WebDriverException $e) {
throw new unitTestEnvironmentException("Could not open a browser session", 0, $e);
} catch (WebDriverCurlException $e) {
throw new unitTestEnvironmentException("Could not open a browser session", 0, $e);
}
$this->browser = $this;
}
function __destruct () {
if (isset($this->context))
$this->context->close();
}
function redirect ($url) {
if (strpos($url, '://') == false) {
enforce('unitTestEnvironmentException', isset($_SERVER['baseUrl']), "baseUrl not set");
$url = $_SERVER['baseUrl'] . (substr($url, 0, 1) == '/' ? substr($url, 1) : $url);
}
$this->context->get($url);
assertTrue(!$this->hasError(), 'Error in page: ' . $this->errorMessage());
}
function url () {
return $this->context->getCurrentURL();
}
function synchronize () {
$t = microtime(true);
$waiter = new WebDriverWait($this->driver);
$self = $this;
$waiter->until(function () use ($self) {
return $self->execute('return typeof(waitForBrowser) == \'undefined\' || !waitForBrowser;');
}, 'Browser synchronization timeout');
}
function close () {
$this->context->close();
}
function source () {
return $this->context->getPageSource();
}
function hasError () {
return strpos($this->source(), 'error-hmnb9a525V77pG545SXkqmfW') !== false;
}
function errorMessage () {
if (!$this->hasError())
return 'no error';
$result = preg_match('/error-hmnb9a525V77pG545SXkqmfW\:\s*"((\\\\"|[^\"])+)"/', $this->source(), $match);
return $result > 0 ? $match[1] : 'unknown error';
}
}
class unitTest_elements extends ArrayObject {
function query ($query) {
$results = new unitTest_elements();
foreach ($this as $queryElement)
foreach ($queryElement->query($query) as $resultElement)
$results[] = $resultElement;
return $results;
}
function __call ($name, $arguments) {
assertTrue(count($this) == 1);
return call_user_func_array(array($this[0], $name), $arguments);
}
function __set ($name, $value) {
assertTrue(count($this) == 1);
$this[0]->$name = $value;
}
function __get ($name) {
assertTrue(count($this) == 1);
return $this[0]->$name;
}
function one () {
assertTrue(count($this) == 1, 'found ' . count($this) . ' elements (1 expected)');
return $this[0];
}
function any () {
return $this[0];
}
}
class unitTest_element extends unitTest_webContext {
private $info;
function __construct ($browser, $context) {
$this->browser = $browser;
$this->context = $context;
$this->info = (object) array();
}
function __set ($name, $value) {
$this->{'_set_' . $name}($value);
}
function __get ($name) {
if (method_exists($this, '_get_' . $name))
return $this->{'_get_' . $name}();
return parent::__get($name);
}
function _get_tag () {
return $this->context->getTagName();
}
function _get_text () {
return $this->context->getText();
}
function _set_value ($value) {
$context = $this->context;
$self = $this;
$browser = $this->browser;
$this->_ensureVisible(function () use ($self, $browser, $context, $value) {
$browser->execute('arguments[0].value = "";', array($self));
$context->sendKeys($value);
});
}
function _get_value () {
return $this->context->getAttribute('value');
}
function _get_displayed () {
return $this->context->isDisplayed();
}
function hasAttribute ($name) {
return $this->context->getAttribute($name) !== null;
}
function attribute ($name) {
return new unitTest_attribute($this->context->getAttribute($name));
}
function select () {
assertTrue($this->_get_tag() == 'option');
$select = $this->queryOne('..');
assertTrue($select->_get_tag() == 'select');
$option = $this;
$select->_ensureVisible(function () use ($option) {
$option->click();
});
}
function _ensureVisible ($f) {
assertTrue(is_callable($f));
if ($this->displayed) {
$f();
return;
}
$state = (object) array(
'display' => $this->context->getCSSValue('display'),
'visibility' => $this->context->getCSSValue('visibility'),
'hiddenInput' => $this->_get_tag() == 'input' && $this->attribute('type')->value == 'hidden',
);
try {
if ($state->display == 'none' || $state->visibility == 'hidden')
$this->browser->execute('arguments[0].style.display = "block"; arguments[0].style.visibility = "visible";',
array($this));
if ($state->hiddenInput)
$this->browser->execute('arguments[0].type = "text";', array($this));
// todo: rewrite
if ($this->_get_tag() != 'html')
$parentCollection = $this->query('..');
if (!isset($parentCollection) || count($parentCollection) == 0)
$f();
else
$parentCollection->one()->_ensureVisible($f);
$e = null;
} catch (Exception $e) {}
// finally {
// we don't care if the elements disappear in the meantime, we only want to make sure
// to hide set their visibility state as it was if they still exist
if ($this->exists()) {
if ($state->display == 'none' || $state->visibility == 'hidden')
$this->browser->execute('arguments[0].style.display = ' . json_encode($state->display)
. '; arguments[0].style.visibility = ' . json_encode($state->visibility) . ';', array($this));
if ($state->hiddenInput)
$this->browser->execute('arguments[0].type = "hidden";', array($this));
}
// }
if (isset($e))
throw $e;
}
function moveTo () {
$this->browser->context->moveto(array('element' => $this->context->getID()));
}
}
class unitTest_attribute {
public $value;
function __construct ($value) {
$this->value = $value;
}
function __toString () {
try {
return $this->value;
} catch (Exception $e) {
// __toString is not allowed to throw an exception
return (string) $e;
}
}
}