RequestActions.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Tests\Traits;
  3. use Illuminate\Foundation\Testing\TestResponse;
  4. use Illuminate\Support\Str;
  5. trait RequestActions
  6. {
  7. /**
  8. * 返回路由名前缀
  9. *
  10. * @return string
  11. */
  12. protected function routePrefix(): string
  13. {
  14. if (isset($this->routePrefix)) {
  15. return $this->routePrefix.'.';
  16. } else {
  17. return '';
  18. }
  19. }
  20. /**
  21. * 使用 route 函数时, 自动加上路由名前缀
  22. *
  23. * @param string $name
  24. * @param mixed $parameters
  25. * @param bool $absolute
  26. *
  27. * @return string
  28. */
  29. protected function route(string $name, $parameters = [], $absolute = true): string
  30. {
  31. return route($this->routePrefix().$name, $parameters, $absolute);
  32. }
  33. /**
  34. * 把短横线分割的复数的资源名, 转成下划线分割的单数
  35. *
  36. * @param string $name
  37. *
  38. * @return string
  39. */
  40. protected function varName(string $name): string
  41. {
  42. return str_replace('-', '_', Str::singular($name));
  43. }
  44. /**
  45. * 请求资源集合
  46. *
  47. * @param array $params
  48. * @param string $name
  49. *
  50. * @return TestResponse
  51. */
  52. protected function getResources(array $params = [], string $name = null): TestResponse
  53. {
  54. $name = $name ?: $this->resourceName;
  55. return $this->get($this->route("{$name}.index", $params));
  56. }
  57. /**
  58. * 请求添加资源
  59. *
  60. * @param array $data
  61. * @param string $name
  62. * @param array $routeParams
  63. *
  64. * @return TestResponse
  65. */
  66. protected function storeResource(array $data = [], string $name = null, array $routeParams = []): TestResponse
  67. {
  68. $name = $name ?: $this->resourceName;
  69. return $this->post($this->route("{$name}.store", $routeParams), $data);
  70. }
  71. /**
  72. * 请求软删除一个资源
  73. *
  74. * @param int $id
  75. * @param string $name
  76. *
  77. * @return TestResponse
  78. */
  79. protected function destroyResource(int $id, string $name = null): TestResponse
  80. {
  81. $name = $name ?: $this->resourceName;
  82. return $this->delete($this->route("{$name}.destroy", [$this->varName($name) => $id]));
  83. }
  84. /**
  85. * 请求获取单个资源
  86. *
  87. * @param int $id
  88. * @param string $name
  89. *
  90. * @return TestResponse
  91. */
  92. protected function getResource(int $id, string $name = null): TestResponse
  93. {
  94. $name = $name ?: $this->resourceName;
  95. return $this->get($this->route("{$name}.show", [$this->varName($name) => $id]));
  96. }
  97. /**
  98. * 请求更新一个资源
  99. *
  100. * @param int $id
  101. * @param array $data
  102. * @param string $name
  103. *
  104. * @return TestResponse
  105. */
  106. protected function updateResource(int $id, array $data = [], string $name = null): TestResponse
  107. {
  108. $name = $name ?: $this->resourceName;
  109. return $this->put($this->route("{$name}.update", [$this->varName($name) => $id]), $data);
  110. }
  111. /**
  112. * 请求编辑一个资源
  113. *
  114. * @param int $id
  115. * @param string $name
  116. *
  117. * @return TestResponse
  118. */
  119. protected function editResource(int $id, string $name = null): TestResponse
  120. {
  121. $name = $name ?: $this->resourceName;
  122. return $this->get($this->route("{$name}.edit", [$this->varName($name) => $id]));
  123. }
  124. /**
  125. * 请求创建一个资源
  126. *
  127. * @param int $id
  128. * @param string $name
  129. *
  130. * @return TestResponse
  131. */
  132. protected function createResource(int $id, string $name = null): TestResponse
  133. {
  134. $name = $name ?: $this->resourceName;
  135. return $this->get($this->route("{$name}.create", [$this->varName($name) => $id]));
  136. }
  137. }