MakeTestCommand.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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|x|ch|sh)$/','ses',lcfirst($this->getModelName()));
  54. $modelName=preg_replace('/y$/','ies',$modelName);
  55. $modelName=preg_replace('/[cC]hild$/','children',$modelName);
  56. if(preg_match('/[cC]hildren$/',$modelName)==0){
  57. $modelName=preg_replace('/([^s])$/','$1s',$modelName);
  58. }
  59. return $modelName;
  60. }
  61. protected function getMethodName()
  62. {
  63. $input= trim($this->argument('name'));
  64. return ucfirst(explode(':',$input)[1]).'Test';
  65. }
  66. protected function getStub()
  67. {
  68. if ($this->option('controller')
  69. ||$this->option('controllers')) {
  70. return __DIR__.('/stubs/test.controller.stub');
  71. }
  72. if ($this->option('services')
  73. ||$this->option('service')) {
  74. return __DIR__.('/stubs/test.service.stub');
  75. }
  76. return $this->option('unit')
  77. ? $this->resolveStubPath('/stubs/test.unit.stub')
  78. : $this->resolveStubPath('/stubs/test.stub');
  79. }
  80. protected function replaceClass($stub, $name)
  81. {
  82. $class = str_replace($this->getNamespace($name).'\\', '', $this->getMethodName());
  83. $class = str_replace('\\', '', $class);
  84. $stub = str_replace(['{{ serviceName }}', '{{serviceName}}'], $this->getServiceName(), $stub);
  85. $stub = str_replace(['{{ modelName }}', '{{modelName}}'], $this->getModelName(), $stub);
  86. $stub = str_replace(['{{ modelNameUc }}', '{{modelNameUc}}'], ucfirst($this->getModelName()), $stub);
  87. $stub = str_replace(['{{ modelNamePlural }}', '{{modelNamePlural}}'], ucfirst($this->getModelNamePlural()), $stub);
  88. return str_replace(['DummyClass', '{{ class }}', '{{class}}'], $class, $stub);
  89. }
  90. }