MakeTestCommand.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Filesystem\Filesystem;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. class MakeTestCommand extends \Illuminate\Foundation\Console\TestMakeCommand
  6. {
  7. protected $signature = 'make:test {name : The name of the class}
  8. {--unit : Create a unit test}
  9. {--controller : Create a controller test}
  10. {--controllers : Create a controllers test}
  11. {--service : Create a service test}
  12. {--services : Create a services test}
  13. ';
  14. protected function getDefaultNamespace($rootNamespace)
  15. {
  16. if ($this->option('controller')
  17. ||$this->option('controllers')) {
  18. return $rootNamespace.'\Controllers';
  19. }
  20. if ($this->option('services')
  21. ||$this->option('service')) {
  22. return $rootNamespace.'\Services\\'
  23. .$this->getServiceName()
  24. ;
  25. }
  26. if ($this->option('unit')) {
  27. return $rootNamespace.'\Unit';
  28. } else {
  29. return $rootNamespace.'\Feature';
  30. }
  31. }
  32. protected function getNameInput()
  33. {
  34. $input= trim($this->argument('name'));
  35. $inputs = explode(':', $input);
  36. return $inputs[1]
  37. ?$this->getMethodName()
  38. :$inputs[0]
  39. ;
  40. }
  41. protected function getServiceName()
  42. {
  43. $input= trim($this->argument('name'));
  44. return ucfirst(explode(':',$input)[0]);
  45. }
  46. protected function getModelName()
  47. {
  48. $input= trim($this->argument('name'));
  49. return str_replace('Service','',(explode(':',$input)[0]));
  50. }
  51. protected function getModelNamePlural()
  52. {
  53. $modelName=preg_replace('/s$/','$ses',lcfirst($this->getModelName()));
  54. $modelName=preg_replace('/ch$/','$ches',$modelName);
  55. $modelName=preg_replace('/sh$/','$shes',$modelName);
  56. $modelName=preg_replace('/y$/','ies',$modelName);
  57. $modelName=preg_replace('/[cC]hild$/','children',$modelName);
  58. if(preg_match('/[cC]hildren$/',$modelName)==0){
  59. $modelName=preg_replace('/([^s])$/','$1s',$modelName);
  60. }
  61. return $modelName;
  62. }
  63. protected function getMethodName()
  64. {
  65. $input= trim($this->argument('name'));
  66. return ucfirst(explode(':',$input)[1]).'Test';
  67. }
  68. protected function getStub()
  69. {
  70. if ($this->option('controller')
  71. ||$this->option('controllers')) {
  72. return __DIR__.('/stubs/test.controller.stub');
  73. }
  74. if ($this->option('services')
  75. ||$this->option('service')) {
  76. return __DIR__.('/stubs/test.service.stub');
  77. }
  78. return $this->option('unit')
  79. ? $this->resolveStubPath('/stubs/test.unit.stub')
  80. : $this->resolveStubPath('/stubs/test.stub');
  81. }
  82. protected function replaceClass($stub, $name)
  83. {
  84. $class = str_replace($this->getNamespace($name).'\\', '', $this->getMethodName());
  85. $class = str_replace('\\', '', $class);
  86. $stub = str_replace(['{{ serviceName }}', '{{serviceName}}'], $this->getServiceName(), $stub);
  87. $stub = str_replace(['{{ modelName }}', '{{modelName}}'], $this->getModelName(), $stub);
  88. $stub = str_replace(['{{ modelNameUc }}', '{{modelNameUc}}'], ucfirst($this->getModelName()), $stub);
  89. $stub = str_replace(['{{ modelNamePlural }}', '{{modelNamePlural}}'], ucfirst($this->getModelNamePlural()), $stub);
  90. return str_replace(['DummyClass', '{{ class }}', '{{class}}'], $class, $stub);
  91. }
  92. }