TestMakeCommand.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Filesystem\Filesystem;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. class TestMakeCommand 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 getMethodName()
  52. {
  53. $input= trim($this->argument('name'));
  54. return ucfirst(explode(':',$input)[1]).'Test';
  55. }
  56. protected function getStub()
  57. {
  58. if ($this->option('controller')
  59. ||$this->option('controllers')) {
  60. return __DIR__.('/stubs/test.controller.stub');
  61. }
  62. if ($this->option('services')
  63. ||$this->option('service')) {
  64. return __DIR__.('/stubs/test.service.stub');
  65. }
  66. return $this->option('unit')
  67. ? $this->resolveStubPath('/stubs/test.unit.stub')
  68. : $this->resolveStubPath('/stubs/test.stub');
  69. }
  70. protected function replaceClass($stub, $name)
  71. {
  72. $class = str_replace($this->getNamespace($name).'\\', '', $this->getMethodName());
  73. $class = str_replace('\\', '', $class);
  74. $stub = str_replace(['{{ serviceName }}', '{{serviceName}}'], $this->getServiceName(), $stub);
  75. $stub = str_replace(['{{ modelName }}', '{{modelName}}'], $this->getModelName(), $stub);
  76. $stub = str_replace(['{{ modelNameUc }}', '{{modelNameUc}}'], ucfirst($this->getModelName()), $stub);
  77. return str_replace(['DummyClass', '{{ class }}', '{{class}}'], $class, $stub);
  78. }
  79. }