MakeTestCommand.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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->isForService()) {
  21. return $rootNamespace.'\Services\\'
  22. .$this->getServiceName()
  23. ;
  24. }
  25. if ($this->option('unit')) {
  26. return $rootNamespace.'\Unit';
  27. } else {
  28. return $rootNamespace.'\Feature';
  29. }
  30. }
  31. protected function isForService()
  32. {
  33. if ($this->option('services')
  34. ||$this->option('service')) {
  35. return true;
  36. }
  37. return false;
  38. }
  39. protected function getNameInput()
  40. {
  41. $input= trim($this->argument('name'));
  42. if(!$this->isForService()){
  43. return $input;
  44. }
  45. $inputs = explode(':', $input);
  46. return $inputs[1]
  47. ?$this->getMethodName()
  48. :$inputs[0]
  49. ;
  50. }
  51. protected function getServiceName()
  52. {
  53. $input= trim($this->argument('name'));
  54. if(!$this->isForService()){
  55. return $input;
  56. }
  57. return ucfirst(explode(':',$input)[0]);
  58. }
  59. protected function getModelName()
  60. {
  61. $input= trim($this->argument('name'));
  62. if(!$this->isForService()){
  63. return $input;
  64. }
  65. return str_replace('Service','',(explode(':',$input)[0]));
  66. }
  67. protected function getModelNamePlural()
  68. {
  69. $modelName=preg_replace('/(s|x|ch|sh)$/','$1es',lcfirst($this->getModelName()));
  70. $modelName=preg_replace('/y$/','ies',$modelName);
  71. $modelName=preg_replace('/[cC]hild$/','children',$modelName);
  72. if(preg_match('/[cC]hildren$/',$modelName)==0){
  73. $modelName=preg_replace('/([^s])$/','$1s',$modelName);
  74. }
  75. return $modelName;
  76. }
  77. protected function getMethodName()
  78. {
  79. $input= trim($this->argument('name'));
  80. if(!$this->isForService()){
  81. return $input;
  82. }
  83. return ucfirst(explode(':',$input)[1]).'Test';
  84. }
  85. protected function getStub()
  86. {
  87. if ($this->option('controller')
  88. ||$this->option('controllers')) {
  89. return __DIR__.('/stubs/test.controller.stub');
  90. }
  91. if ($this->option('services')
  92. ||$this->option('service')) {
  93. return __DIR__.('/stubs/test.service.stub');
  94. }
  95. return $this->option('unit')
  96. ? $this->resolveStubPath('/stubs/test.unit.stub')
  97. : $this->resolveStubPath('/stubs/test.stub');
  98. }
  99. protected function replaceClass($stub, $name)
  100. {
  101. $class = str_replace($this->getNamespace($name).'\\', '', $this->getMethodName());
  102. $class = str_replace('\\', '', $class);
  103. $stub = str_replace(['{{ serviceName }}', '{{serviceName}}'], $this->getServiceName(), $stub);
  104. $stub = str_replace(['{{ modelName }}', '{{modelName}}'], $this->getModelName(), $stub);
  105. $stub = str_replace(['{{ modelNameUc }}', '{{modelNameUc}}'], ucfirst($this->getModelName()), $stub);
  106. $stub = str_replace(['{{ modelNamePlural }}', '{{modelNamePlural}}'], ucfirst($this->getModelNamePlural()), $stub);
  107. return str_replace(['DummyClass', '{{ class }}', '{{class}}'], $class, $stub);
  108. }
  109. }