├── .gitignore ├── LICENSE ├── README.md ├── _config.yml ├── composer.json ├── composer.lock └── src ├── Formatter.php ├── Generator.php ├── Test └── UserTest.php ├── TestCaseGenerator.php ├── TestGenerator.php └── TestGeneratorServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Vignesh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Test Generator 2 | 3 | Auto generate the unit test file for the available routes 4 | 5 | ## Installation 6 | 7 | The package can easily be installed by running `composer require vigneshc91/laravel-test-generator` in your project's root folder. 8 | 9 | If you are running a version of Laravel < 5.5 also make sure you add `Vigneshc91\LaravelTestGenerator\TestGeneratorServiceProvider::class` to the `providers` array in `config/app.php`. 10 | 11 | This will register the artisan command that will be available to you. 12 | 13 | 14 | ## Usage 15 | 16 | Generating the test file is easy, simply run `php artisan laravel-test:generate` in your project root. This will write all the test cases into the file based on controller. 17 | 18 | If you wish to filter for specific routes only, you can pass a filter attribute using --filter, for example `php artisan laravel-test:generate --filter='/api'` 19 | 20 | If you wish to change the directory of creating the test file, you can pass a directory using --dir, for example `php artisan laravel-test:generate --dir='V1'` 21 | 22 | If you wish to add the @depends attribute to all the function except the first function for running test cases synchronously, you can pass a sync attribute using --sync, for example `php artisan laravel-test:generate --sync='true'` 23 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-architect -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vigneshc91/laravel-test-generator", 3 | "description": "Laravel package for generating unit test automatically", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Vignesh C", 8 | "email": "vignesh.jag@gmail.com" 9 | } 10 | ], 11 | "require": { 12 | "fzaninotto/faker": "^1.8" 13 | }, 14 | "autoload": { 15 | "psr-4": { 16 | "Vigneshc91\\LaravelTestGenerator\\": "src/" 17 | } 18 | }, 19 | "extra": { 20 | "laravel": { 21 | "providers": [ 22 | "Vigneshc91\\LaravelTestGenerator\\TestGeneratorServiceProvider" 23 | ] 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "0b95c532b5915b8112e8755f13734f83", 8 | "packages": [ 9 | { 10 | "name": "fzaninotto/faker", 11 | "version": "v1.8.0", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/fzaninotto/Faker.git", 15 | "reference": "f72816b43e74063c8b10357394b6bba8cb1c10de" 16 | }, 17 | "dist": { 18 | "type": "zip", 19 | "url": "https://api.github.com/repos/fzaninotto/Faker/zipball/f72816b43e74063c8b10357394b6bba8cb1c10de", 20 | "reference": "f72816b43e74063c8b10357394b6bba8cb1c10de", 21 | "shasum": "" 22 | }, 23 | "require": { 24 | "php": "^5.3.3 || ^7.0" 25 | }, 26 | "require-dev": { 27 | "ext-intl": "*", 28 | "phpunit/phpunit": "^4.8.35 || ^5.7", 29 | "squizlabs/php_codesniffer": "^1.5" 30 | }, 31 | "type": "library", 32 | "extra": { 33 | "branch-alias": { 34 | "dev-master": "1.8-dev" 35 | } 36 | }, 37 | "autoload": { 38 | "psr-4": { 39 | "Faker\\": "src/Faker/" 40 | } 41 | }, 42 | "notification-url": "https://packagist.org/downloads/", 43 | "license": [ 44 | "MIT" 45 | ], 46 | "authors": [ 47 | { 48 | "name": "François Zaninotto" 49 | } 50 | ], 51 | "description": "Faker is a PHP library that generates fake data for you.", 52 | "keywords": [ 53 | "data", 54 | "faker", 55 | "fixtures" 56 | ], 57 | "time": "2018-07-12T10:23:15+00:00" 58 | } 59 | ], 60 | "packages-dev": [], 61 | "aliases": [], 62 | "minimum-stability": "stable", 63 | "stability-flags": [], 64 | "prefer-stable": false, 65 | "prefer-lowest": false, 66 | "platform": [], 67 | "platform-dev": [] 68 | } 69 | -------------------------------------------------------------------------------- /src/Formatter.php: -------------------------------------------------------------------------------- 1 | directory = $directory; 28 | $this->sync = $sync; 29 | $this->file = __DIR__.'/Test/UserTest.php'; 30 | $this->namespace = 'namespace Tests\Feature' . ($this->directory ? '\\' . $this->directory : '') . ';'; 31 | $this->destinationFilePath = base_path('tests/Feature/' . $this->directory); 32 | $this->cases = []; 33 | } 34 | 35 | /** 36 | * Format the test case in the controller 37 | * 38 | * @param array $case 39 | * @param string $url 40 | * @param string $method 41 | * @param string $controllerName 42 | * @param string $actionName 43 | * @return void 44 | */ 45 | public function format($case, $url, $method, $controllerName, $actionName, $auth) 46 | { 47 | $this->cases[$controllerName]['action'] = $actionName; 48 | $this->cases[$controllerName]['url'] = $url; 49 | $this->cases[$controllerName]['method'] = $method; 50 | $this->cases[$controllerName]['params'] = $case; 51 | $this->cases[$controllerName]['auth'] = $auth; 52 | if(empty($this->cases[$controllerName]['function'])) { 53 | $this->cases[$controllerName]['function'] = []; 54 | } 55 | $this->formatFunction($controllerName); 56 | } 57 | 58 | /** 59 | * Generate the files for all the test cases 60 | * 61 | * @return void 62 | */ 63 | public function generate() 64 | { 65 | $this->createDirectory(); 66 | $this->formatFile(); 67 | } 68 | 69 | /** 70 | * Set the function for success and failure case 71 | * 72 | * @return void 73 | */ 74 | protected function formatFunction($controllerName) 75 | { 76 | $functionName = ''; 77 | $i = 0; 78 | $controller = $this->cases[$controllerName]; 79 | 80 | foreach ($controller['params'] as $index => $item) { 81 | # Add function documentation 82 | $function = "\t" . '/**' . PHP_EOL . "\t" . ' * ' . $controller['action'] . PHP_EOL . "\t" . ' *' . PHP_EOL; 83 | 84 | # Check @depends to be added or not 85 | if($this->sync) { 86 | if($i > 0) { 87 | $function .= "\t" . ' * @depends ' . $functionName . PHP_EOL; 88 | } else { 89 | if(count($controller['function']) > 0) { 90 | $function .= "\t" . ' * @depends ' . end($controller['function'])['name'] . PHP_EOL; 91 | } 92 | } 93 | } 94 | 95 | $function .= "\t" . ' * @return void' . PHP_EOL . "\t" . ' */' . PHP_EOL; 96 | $functionName = $this->getFunctionName($index, $controller['action']); 97 | 98 | # Function name and declaration 99 | $function .= "\t" . 'public function ' . $functionName . '()'; 100 | 101 | # Function definition 102 | $body = "\t\t".'$response = $this->json(\'' . strtoupper($controller['method']) . '\', \'' . $controller['url'] . '\', ['; 103 | 104 | # Request parameters 105 | $params = $this->getParams($item); 106 | $body .= $params ? PHP_EOL . $params . PHP_EOL . "\t\t". ']' : ']'; 107 | 108 | $body .= $controller['auth'] ? ", [\n\t\t\t'Authorization' => 'Bearer '\n\t\t]" : ''; 109 | 110 | $body .= ');'; 111 | # Assert response 112 | $body .= PHP_EOL . PHP_EOL . "\t\t" . '$response->assertStatus(' . ($index == 'failure' ? '400' : '200') . ');' . PHP_EOL; 113 | 114 | # Add the function to the global array 115 | $this->cases[$controllerName]['function'][] = [ 116 | 'name' => $functionName, 117 | 'code' => $function . PHP_EOL . "\t" . '{' . PHP_EOL . $body . PHP_EOL . "\t" . '}' . PHP_EOL 118 | ]; 119 | 120 | $i++; 121 | } 122 | 123 | } 124 | 125 | /** 126 | * Format the test cases for the writing to the file 127 | * 128 | * @return void 129 | */ 130 | protected function formatFile() 131 | { 132 | foreach ($this->cases as $key => $value) { 133 | $lines = file($this->file, FILE_IGNORE_NEW_LINES); 134 | $lines[2] = $this->namespace; 135 | $lines[8] = $this->getClassName($key, $lines[8]); 136 | $functions = implode(PHP_EOL, array_pluck($value['function'], 'code')); 137 | $content = array_merge(array_slice($lines, 0, 10) , [$functions] , array_slice($lines, 11)); 138 | 139 | $this->writeToFile($key . 'Test', $content); 140 | } 141 | } 142 | 143 | /** 144 | * Write the string into the file 145 | * 146 | * @param string $controllerName 147 | * @param string $rule 148 | * @return void 149 | */ 150 | protected function writeToFile($controllerName, $content) 151 | { 152 | $fileName = $this->destinationFilePath . '/' . $controllerName . '.php'; 153 | $file = fopen($fileName, 'w'); 154 | foreach ($content as $index => $value) { 155 | fwrite($file, $value.PHP_EOL); 156 | } 157 | fclose($file); 158 | 159 | echo "\033[32m". basename($fileName). ' Created Successfully'. PHP_EOL; 160 | } 161 | 162 | /** 163 | * Get the class name from the controller name 164 | * 165 | * @param string $controllerName 166 | * @param string $line 167 | * @return string 168 | */ 169 | protected function getClassName($controllerName, $line) 170 | { 171 | return str_replace('UserTest', $controllerName . 'Test', $line); 172 | } 173 | 174 | /** 175 | * Get the request parameters string array format for printing in the file 176 | * 177 | * @param array $param 178 | * @return string 179 | */ 180 | protected function getParams($param) 181 | { 182 | if(empty($param)) { 183 | return ''; 184 | } 185 | $param = json_encode($param); 186 | $param = str_replace(['{', '}'], '', $param); 187 | $param = "\t\t\t".$param; 188 | $param = str_replace('":', '" => ', $param); 189 | $param = str_replace(',', ",\n\t\t\t", $param); 190 | return $param; 191 | } 192 | 193 | /** 194 | * Get the name of the test case function 195 | * 196 | * @param string $index 197 | * @param string $action 198 | * @return string 199 | */ 200 | protected function getFunctionName($index, $action) 201 | { 202 | $name = 'test' . $action; 203 | return $index == 'failure' ? $name . 'WithError' : $name; 204 | } 205 | 206 | /** 207 | * Create a new directory if not exist 208 | * 209 | * @return void 210 | */ 211 | protected function createDirectory() 212 | { 213 | $dirName = $this->destinationFilePath; 214 | if(!is_dir($dirName)) { 215 | mkdir($dirName, 0755, true); 216 | } 217 | } 218 | } -------------------------------------------------------------------------------- /src/Generator.php: -------------------------------------------------------------------------------- 1 | directory = $options['directory']; 37 | $this->routeFilter = $options['filter']; 38 | $this->sync = $options['sync']; 39 | $this->testCaseGenerator = new TestCaseGenerator(); 40 | $this->formatter = new Formatter($this->directory, $this->sync); 41 | } 42 | 43 | /** 44 | * Generate the route methods and write to the file 45 | * 46 | * @return void 47 | */ 48 | public function generate() 49 | { 50 | $this->getRouteMethods(); 51 | $this->formatter->generate(); 52 | } 53 | 54 | /** 55 | * Get the route detail and generate the test cases 56 | * 57 | * @return void 58 | */ 59 | protected function getRouteMethods() 60 | { 61 | foreach ($this->getAppRoutes() as $route) { 62 | $this->originalUri = $uri = $this->getRouteUri($route); 63 | $this->uri = $this->strip_optional_char($uri); 64 | 65 | if ($this->routeFilter && !preg_match('/^' . preg_quote($this->routeFilter, '/') . '/', $this->uri)) { 66 | continue; 67 | } 68 | 69 | $action = $route->getAction('uses'); 70 | $methods = $route->methods(); 71 | $actionName = $this->getActionName($route->getActionName()); 72 | 73 | $controllerName = $this->getControllerName($route->getActionName()); 74 | 75 | foreach ($methods as $method) { 76 | $this->method = strtoupper($method); 77 | 78 | if (in_array($this->method, ['HEAD'])) continue; 79 | 80 | $rules = $this->getFormRules($action); 81 | if(empty($rules)) { 82 | $rules = []; 83 | } 84 | $case = $this->testCaseGenerator->generate($rules); 85 | $hasAuth = $this->isAuthorizationExist($route->middleware()); 86 | $this->formatter->format($case, $this->uri, $this->method, $controllerName, $actionName, $hasAuth); 87 | 88 | } 89 | } 90 | } 91 | 92 | /** 93 | * Check authorization middleware is exist 94 | * 95 | * @param array $middlewares 96 | * @return boolean 97 | */ 98 | protected function isAuthorizationExist($middlewares) 99 | { 100 | $hasAuth = array_filter($middlewares, function ($var) { 101 | return (strpos($var, 'auth') > -1); 102 | }); 103 | 104 | return $hasAuth; 105 | } 106 | 107 | /** 108 | * Replace the optional params from the URL 109 | * 110 | * @param string $uri 111 | * @return string 112 | */ 113 | protected function strip_optional_char($uri) 114 | { 115 | return str_replace('?', '', $uri); 116 | } 117 | 118 | /** 119 | * Get the routes of the application 120 | * 121 | * @return array 122 | */ 123 | protected function getAppRoutes() 124 | { 125 | return app('router')->getRoutes(); 126 | } 127 | 128 | /** 129 | * Get the URI of the route 130 | * 131 | * @param Route $route 132 | * @return string 133 | */ 134 | protected function getRouteUri(Route $route) 135 | { 136 | $uri = $route->uri(); 137 | 138 | if (!starts_with($uri, '/')) { 139 | $uri = '/' . $uri; 140 | } 141 | 142 | return $uri; 143 | } 144 | 145 | /** 146 | * Get the form rules for creating the parameters 147 | * 148 | * @param [type] $action 149 | * @return array 150 | */ 151 | protected function getFormRules($action) 152 | { 153 | if (!is_string($action)) return false; 154 | 155 | $parsedAction = Str::parseCallback($action); 156 | 157 | $reflector = (new ReflectionMethod($parsedAction[0], $parsedAction[1])); 158 | $parameters = $reflector->getParameters(); 159 | 160 | foreach ($parameters as $parameter) { 161 | $class = optional($parameter->getType())->getName(); 162 | 163 | if (is_subclass_of($class, FormRequest::class)) { 164 | return (new $class)->rules(); 165 | } 166 | } 167 | } 168 | 169 | /** 170 | * Return's the controller name 171 | * 172 | * @param string $controller 173 | * @return string 174 | */ 175 | protected function getControllerName($controller) 176 | { 177 | $namespaceReplaced = substr($controller, strrpos($controller, '\\')+1); 178 | $actionNameReplaced = substr($namespaceReplaced, 0, strpos($namespaceReplaced, '@')); 179 | $controllerReplaced = str_replace('Controller', '', $actionNameReplaced); 180 | $controllerNameArray = preg_split('/(?=[A-Z])/', $controllerReplaced); 181 | $controllerName = trim(implode('', $controllerNameArray)); 182 | 183 | return $controllerName; 184 | } 185 | 186 | /** 187 | * Return's the action name 188 | * 189 | * @param string $actionName 190 | * @return string 191 | */ 192 | protected function getActionName($actionName) 193 | { 194 | $actionNameSubString = substr($actionName, strpos($actionName, '@')+1); 195 | $actionNameArray = preg_split('/(?=[A-Z])/', ucfirst($actionNameSubString)); 196 | $actionName = trim(implode('', $actionNameArray)); 197 | 198 | return $actionName; 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /src/Test/UserTest.php: -------------------------------------------------------------------------------- 1 | faker = Faker\Factory::create(); 23 | $this->cases = []; 24 | } 25 | 26 | /** 27 | * Initialize the params and rules and generates the test cases 28 | * 29 | * @param array $rules 30 | * @return array 31 | */ 32 | public function generate($rules) 33 | { 34 | $this->params = array_keys($rules); 35 | $this->rules = array_values($rules); 36 | return $this->generateCase(); 37 | } 38 | 39 | /** 40 | * Generate success and failure test case 41 | * 42 | * @return void 43 | */ 44 | protected function generateCase() 45 | { 46 | $this->generateFailureCase(); 47 | $this->generateSuccessCase(); 48 | return $this->cases; 49 | } 50 | 51 | /** 52 | * Generate the success test case 53 | * 54 | * @return void 55 | */ 56 | protected function generateSuccessCase() 57 | { 58 | $case = []; 59 | $value = ''; 60 | foreach ($this->params as $key => $val) { 61 | $case[$val] = $this->getValue(is_string($val) ? $val : strval($val), $this->rules[$key]); 62 | } 63 | 64 | $this->cases['success'] = $case; 65 | } 66 | 67 | /** 68 | * Get the value for the given field with the applied rules 69 | * 70 | * @param string $param 71 | * @param [array] $rules 72 | * @return string 73 | */ 74 | protected function getValue($param, $rules) 75 | { 76 | if(is_string($rules)) { 77 | $rules = explode('|', $rules); 78 | } 79 | $value = ''; 80 | 81 | switch ($rules) { 82 | case $this->isEmail($rules): 83 | $value = $this->faker->email; 84 | break; 85 | case $this->isCompanyName($rules, $param): 86 | $value = $this->faker->company; 87 | break; 88 | case $this->isAddress($rules, $param): 89 | $value = $this->faker->address; 90 | break; 91 | case $this->isName($rules, $param): 92 | $value = $this->faker->name; 93 | break; 94 | case $this->isStreetName($rules, $param): 95 | $value = $this->faker->streetName; 96 | break; 97 | case $this->isStreetAddress($rules, $param): 98 | $value = $this->faker->streetAddress; 99 | break; 100 | case $this->isCity($rules, $param): 101 | $value = $this->faker->city; 102 | break; 103 | case $this->isState($rules, $param): 104 | $value = $this->faker->state; 105 | break; 106 | case $this->isCountry($rules, $param): 107 | $value = $this->faker->country; 108 | break; 109 | case $this->isZip($rules, $param): 110 | $value = $this->faker->postcode; 111 | break; 112 | case $this->isLatitude($param): 113 | $value = $this->faker->latitude; 114 | break; 115 | case $this->isLongitude($param): 116 | $value = $this->faker->longitude; 117 | break; 118 | case $this->isPhone($param): 119 | $value = $this->faker->e164PhoneNumber; 120 | break; 121 | case $this->isBoolean($rules): 122 | $value = rand(0, 1); 123 | break; 124 | case $this->isDate($rules): 125 | $value = $this->faker->date; 126 | break; 127 | case $this->isDateFormat($rules): 128 | $format = array_values(array_filter($rules, function($val){ 129 | return preg_match('/^date_format/', $val); 130 | })); 131 | $format = str_replace('date_format:', '', $format[0]); 132 | $value = $this->faker->date($format); 133 | break; 134 | } 135 | 136 | return $value; 137 | } 138 | 139 | /** 140 | * Check whether email is applicable for the given field 141 | * 142 | * @param array $rules 143 | * @return boolean 144 | */ 145 | protected function isEmail($rules) 146 | { 147 | return in_array('email', $rules); 148 | } 149 | 150 | /** 151 | * Check whether company name is applicable for the given field 152 | * 153 | * @param array $rules 154 | * @param string $param 155 | * @return boolean 156 | */ 157 | protected function isCompanyName($rules, $param) 158 | { 159 | return strpos('company', $param) !== false && in_array('string', $rules); 160 | } 161 | 162 | /** 163 | * Check whether address is applicable for the given field 164 | * 165 | * @param array $rules 166 | * @param string $param 167 | * @return boolean 168 | */ 169 | protected function isAddress($rules, $param) 170 | { 171 | return strpos('address', $param) !== false && in_array('string', $rules); 172 | } 173 | 174 | /** 175 | * Check whether name is applicable for the given field 176 | * 177 | * @param array $rules 178 | * @param string $param 179 | * @return boolean 180 | */ 181 | protected function isName($rules, $param) 182 | { 183 | return strpos('name', $param) !== false && in_array('string', $rules); 184 | } 185 | 186 | /** 187 | * Check whether stree name is applicable for the given field 188 | * 189 | * @param array $rules 190 | * @param string $param 191 | * @return boolean 192 | */ 193 | protected function isStreetName($rules, $param) 194 | { 195 | return strpos('street', $param) !== false && in_array('string', $rules); 196 | } 197 | 198 | /** 199 | * Check whether street address is applicable for the given field 200 | * 201 | * @param array $rules 202 | * @param string $param 203 | * @return boolean 204 | */ 205 | protected function isStreetAddress($rules, $param) 206 | { 207 | return strpos('street_address', $param) !== false && in_array('string', $rules); 208 | } 209 | 210 | /** 211 | * Check whether city is applicable for the given field 212 | * 213 | * @param array $rules 214 | * @param string $param 215 | * @return boolean 216 | */ 217 | protected function isCity($rules, $param) 218 | { 219 | return strpos('city', $param) !== false && in_array('string', $rules); 220 | } 221 | 222 | /** 223 | * Check whether state is applicable for the given field 224 | * 225 | * @param array $rules 226 | * @param string $param 227 | * @return boolean 228 | */ 229 | protected function isState($rules, $param) 230 | { 231 | return strpos('state', $param) !== false && in_array('string', $rules); 232 | } 233 | 234 | /** 235 | * Check whether country is applicable for the given field 236 | * 237 | * @param array $rules 238 | * @param string $param 239 | * @return boolean 240 | */ 241 | protected function isCountry($rules, $param) 242 | { 243 | return strpos('country', $param) !== false && in_array('string', $rules); 244 | } 245 | 246 | /** 247 | * Check whether zip is applicable for the given field 248 | * 249 | * @param array $rules 250 | * @param string $param 251 | * @return boolean 252 | */ 253 | protected function isZip($rules, $param) 254 | { 255 | return (strpos('zip', $param) !== false || strpos('pin', $param) !== false ) && in_array('string', $rules); 256 | } 257 | 258 | /** 259 | * Check whether latitude is applicable for the given field 260 | * 261 | * @param string $param 262 | * @return boolean 263 | */ 264 | protected function isLatitude($param) 265 | { 266 | return strpos('latitude', $param) !== false; 267 | } 268 | 269 | /** 270 | * Check whether longitude is applicable for the given field 271 | * 272 | * @param string $param 273 | * @return boolean 274 | */ 275 | protected function isLongitude($param) 276 | { 277 | return strpos('longitude', $param) !== false; 278 | } 279 | 280 | /** 281 | * Check whether phone number is applicable for the given field 282 | * 283 | * @param string $param 284 | * @return boolean 285 | */ 286 | protected function isPhone($param) 287 | { 288 | return strpos('phone', $param) !== false || strpos('mobile', $param) !== false; 289 | } 290 | 291 | /** 292 | * Check whether boolean type is applicable for the given field 293 | * 294 | * @param array $rules 295 | * @return boolean 296 | */ 297 | protected function isBoolean($rules) 298 | { 299 | return in_array('boolean', $rules); 300 | } 301 | 302 | /** 303 | * Check whether date type is applicable for the given field 304 | * 305 | * @param array $rules 306 | * @return boolean 307 | */ 308 | protected function isDate($rules) 309 | { 310 | return in_array('date', $rules); 311 | } 312 | 313 | /** 314 | * Check whether date or time is applicable for the given field 315 | * 316 | * @param array $rules 317 | * @return boolean 318 | */ 319 | protected function isDateFormat($rules) 320 | { 321 | $format = array_filter($rules, function($val){ 322 | return preg_match('/^date_format/', $val); 323 | }); 324 | return count($format); 325 | } 326 | 327 | /** 328 | * Generate failure test case 329 | * 330 | * @return void 331 | */ 332 | protected function generateFailureCase() 333 | { 334 | $this->cases['failure'] = array_fill_keys($this->params, ''); 335 | } 336 | 337 | } 338 | -------------------------------------------------------------------------------- /src/TestGenerator.php: -------------------------------------------------------------------------------- 1 | $this->option('dir') ? $this->option('dir') : '', 45 | 'sync' => $this->option('sync') ? true : false, 46 | 'filter' => $this->option('filter') 47 | ]; 48 | $generator = new Generator($options); 49 | $generator->generate(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/TestGeneratorServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 17 | $this->commands([ 18 | TestGenerator::class 19 | ]); 20 | } 21 | } 22 | 23 | /** 24 | * Register services. 25 | * 26 | * @return void 27 | */ 28 | public function register() 29 | { 30 | // 31 | } 32 | } 33 | --------------------------------------------------------------------------------