-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestGenerator.php
More file actions
79 lines (65 loc) · 1.87 KB
/
TestGenerator.php
File metadata and controls
79 lines (65 loc) · 1.87 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
<?php
namespace Test;
require './User.php';
require './Post.php';
require './Annotations.php';
require './Tests/ParamsTest.php';
require './Tests/ResultHasKeysTest.php';
require './Tests/ResultHasTypeTest.php';
require './Tests/InDBTest.php';
require './Tests/NotInDBTest.php';
require './Tests/EqualsTest.php';
require './Tests/ValidateTest.php';
use Test\TestGenerator,
Test\Annotations,
Test\Test,
Test\ParamsTest;
/**
* Description of TestGenerator
*
* @author peter
*/
class TestGenerator {
private $annotation;
public function __construct(Annotations $annotation) {
$this->annotation = $annotation;
}
public function generateTest($class) {
$annotation = $this->annotation;
$specs = $annotation->getContracts($class);
$methodTest = '';
echo '<h1>Test class ' . $class . '</h1>';
foreach($specs as $method => $spec) {
echo '<h2>Test method ' . $method . ':</h2><ul>';
echo '<li>Given that<ul>';
$methodTest .= $this->compileAnnotation($spec['requires']);
echo '</ul></li></ul><ul><li>Check that</li><ul>';
$methodTest .= $this->compileAnnotation($spec['ensures']);
echo '</ul></li></ul>';
}
}
private function compileAnnotation($value) {
foreach($value as $val) {
// Split into several
$class = '\\Test\\'.\ucfirst($this->getClass($val)) . 'Test';
$params = $this->getParams($val);
call_user_func($class.'::test', $params);
}
}
private function getClass($value) {
return substr($value, 0, strpos($value, '('));
}
private function getParams($value) {
$params = substr($value, strpos($value, '(')+1, strlen($value) - strpos($value, '(') -2 );
$res_whitespace = explode(',', $params);
//Clean whitspaces
$res = array();
foreach($res_whitespace as $elem) {
$res[] = \trim($elem);
}
return $res;
}
}
$gen = new TestGenerator(new Annotations());
$gen->generateTest('\Test\User');
$gen->generateTest('\Test\Post');