-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblis3TestsGen.php
executable file
·122 lines (102 loc) · 4.95 KB
/
blis3TestsGen.php
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
<?php
function generateTest($conn,$table){
$test="<?php
namespace Tests\Unit;
/**
* (c) @iLabAfrica
* BLIS\t\t\t - a port of the Basic Laboratory Information System (BLIS) to Laravel.
* Team Lead\t - Emmanuel Kweyu.
* Devs\t\t\t - Brian Maiyo|Ann Chemutai|Winnie Mbaka|Ken Mutuma|Anthony Ereng
*/
use Tests\SetUp;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;";
$describe="describe ".$table;
$table_structure = mysqli_query($conn, $describe);
$mockFieldsOriginal="\n";
$mockFieldsUpdated="\n";
$rules_set="";
$modelName = Pluralizer::singular(ucwords(preg_replace_callback('/_([a-z]?)/', function($match) {
return strtoupper($match[1]);
}, $table)));
$test.="\n\n";
$test.="class ".$modelName."Test extends TestCase\n{\n";
$test.="\tuse SetUp;\n";
$test.="\tuse DatabaseMigrations;\n";
$test.="\tpublic function setVariables(){\n";
$subject='';
while ($row2 = mysqli_fetch_assoc($table_structure)) {
//Generate original and update data
if($row2['Field']=="id" || $row2['Field']=="created_at" || $row2['Field']=="updated_at" || $row2['Field']=="deleted_at"){
}else{
if($row2['Type']=='date'){
//generate store objects and exlude id and other autogenerated fields
$mockFieldsOriginal.="\t\t\t".'"'.$row2['Field'].'"'."=>'2017:12:12 15:30:00',\n";
$mockFieldsUpdated.="\t\t\t".'"'.$row2['Field'].'"'."=>'2016:12:12 15:30:00',\n";
}elseif($row2['Type']=='int(10) unsigned'){
//generate store objects and exlude id and other autogenerated fields
$mockFieldsOriginal.="\t\t\t".'"'.$row2['Field'].'"'."=>1,\n";
$mockFieldsUpdated.="\t\t\t".'"'.$row2['Field'].'"'."=>1,\n";
}else{
$mockFieldsOriginal.="\t\t\t".'"'.$row2['Field'].'"'."=>'Sample String',\n";
$mockFieldsUpdated.="\t\t\t".'"'.$row2['Field'].'"'."=>'Sample updated String',\n";
}
$subject = $row2['Field'];
}
}
$test.="\t\t$"."this->".lcfirst($modelName)."Data=array(".$mockFieldsOriginal."\t\t);\n";
$test.="\t\t$"."this->updated".$modelName."Data=array(".$mockFieldsUpdated."\t\t);";
//Test Store
$test.="\n\t}\n\n";
$test.="\tpublic function testStore".$modelName."()\n\t{\n\t";
$test.="\t$"."response="."$"."this->post('/api/".strtolower($modelName)."',";
$test.="$"."this->".lcfirst($modelName)."Data);\n";
$test.="\t\t$"."this->assertEquals(200,"."$"."response->getStatusCode());\n";
$test.="\t\t$"."this->assertArrayHasKey(\"".$subject."\",$"."response->original);\n";
$test.="\t}\n\n";
//Test lists
$test.="\tpublic function testList".$modelName."()\n\t{\n";
$test.="\t\t$"."response="."$"."this->get('/api/".strtolower($modelName)."');\n";
$test.="\t\t$"."this->assertEquals(200,"."$"."response->getStatusCode());\n";
$test.="\t}\n\n";
//Test show
$test.="\tpublic function testShow".$modelName."()\n\t{\n\t";
$test.="\t$"."response="."$"."this->post('/api/".strtolower($modelName)."',";
$test.="$"."this->".lcfirst($modelName)."Data);\n";
$test.="\t\t$"."response="."$"."this->get('/api/".strtolower($modelName)."/1');\n";
$test.="\t\t$"."this->assertEquals(200,"."$"."response->getStatusCode());\n";
$test.="\t\t$"."this->assertArrayHasKey(\"".$subject."\",$"."response->original);\n";
$test.="\t}\n\n";
//Test Update
$test.="\tpublic function testUpdate".$modelName."()\n\t{\n\t";
$test.="\t$"."response="."$"."this->post('/api/".strtolower($modelName)."',";
$test.="$"."this->".lcfirst($modelName)."Data);\n";
$test.="\t\t$"."response="."$"."this->put('/api/".strtolower($modelName)."/1',";
$test.="$"."this->updated".$modelName."Data);\n";
$test.="\t\t$"."this->assertEquals(200,"."$"."response->getStatusCode());\n";
$test.="\t\t$"."this->assertArrayHasKey(\"".$subject."\",$"."response->original);\n";
$test.="\t}\n\n";
//Test delete
$test.="\tpublic function testDelete".$modelName."()\n\t{\n\t";
$test.="\t$"."response="."$"."this->post('/api/".strtolower($modelName)."',";
$test.="$"."this->".lcfirst($modelName)."Data);\n";
$test.="\t\t$"."response="."$"."this->delete('/api/".strtolower($modelName)."/1');\n";
$test.="\t\t$"."this->assertEquals(200,"."$"."response->getStatusCode());\n";
$test.="\t}\n\n";
$test.="}";
$route="";
$route.="Route::resource('".strtolower($modelName)."', '".$modelName."Controller');\n";
generateFile($modelName,$test);
generateRoutes($route);
}
function generateFile($modelName,$test){
$myfile = fopen("/var/www/Blis-V3/tests/Unit/".$modelName."Test.php", "w") or die("Unable to open file!");
fwrite($myfile, $test);
fclose($myfile);
}
function generateRoutes($test){
$myfile = fopen("api.php", "a") or die("Unable to open file!");
fwrite($myfile, $test);
fclose($myfile);
}