123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace Tests\Traits;
- use Illuminate\Foundation\Testing\TestResponse;
- use Illuminate\Support\Str;
- trait RequestActions
- {
- /**
- * 返回路由名前缀
- *
- * @return string
- */
- protected function routePrefix(): string
- {
- if (isset($this->routePrefix)) {
- return $this->routePrefix.'.';
- } else {
- return '';
- }
- }
- /**
- * 使用 route 函数时, 自动加上路由名前缀
- *
- * @param string $name
- * @param mixed $parameters
- * @param bool $absolute
- *
- * @return string
- */
- protected function route(string $name, $parameters = [], $absolute = true): string
- {
- return route($this->routePrefix().$name, $parameters, $absolute);
- }
- /**
- * 把短横线分割的复数的资源名, 转成下划线分割的单数
- *
- * @param string $name
- *
- * @return string
- */
- protected function varName(string $name): string
- {
- return str_replace('-', '_', Str::singular($name));
- }
- /**
- * 请求资源集合
- *
- * @param array $params
- * @param string $name
- *
- * @return TestResponse
- */
- protected function getResources(array $params = [], string $name = null): TestResponse
- {
- $name = $name ?: $this->resourceName;
- return $this->get($this->route("{$name}.index", $params));
- }
- /**
- * 请求添加资源
- *
- * @param array $data
- * @param string $name
- * @param array $routeParams
- *
- * @return TestResponse
- */
- protected function storeResource(array $data = [], string $name = null, array $routeParams = []): TestResponse
- {
- $name = $name ?: $this->resourceName;
- return $this->post($this->route("{$name}.store", $routeParams), $data);
- }
- /**
- * 请求软删除一个资源
- *
- * @param int $id
- * @param string $name
- *
- * @return TestResponse
- */
- protected function destroyResource(int $id, string $name = null): TestResponse
- {
- $name = $name ?: $this->resourceName;
- return $this->delete($this->route("{$name}.destroy", [$this->varName($name) => $id]));
- }
- /**
- * 请求获取单个资源
- *
- * @param int $id
- * @param string $name
- *
- * @return TestResponse
- */
- protected function getResource(int $id, string $name = null): TestResponse
- {
- $name = $name ?: $this->resourceName;
- return $this->get($this->route("{$name}.show", [$this->varName($name) => $id]));
- }
- /**
- * 请求更新一个资源
- *
- * @param int $id
- * @param array $data
- * @param string $name
- *
- * @return TestResponse
- */
- protected function updateResource(int $id, array $data = [], string $name = null): TestResponse
- {
- $name = $name ?: $this->resourceName;
- return $this->put($this->route("{$name}.update", [$this->varName($name) => $id]), $data);
- }
- /**
- * 请求编辑一个资源
- *
- * @param int $id
- * @param string $name
- *
- * @return TestResponse
- */
- protected function editResource(int $id, string $name = null): TestResponse
- {
- $name = $name ?: $this->resourceName;
- return $this->get($this->route("{$name}.edit", [$this->varName($name) => $id]));
- }
- /**
- * 请求创建一个资源
- *
- * @param int $id
- * @param string $name
- *
- * @return TestResponse
- */
- protected function createResource(int $id, string $name = null): TestResponse
- {
- $name = $name ?: $this->resourceName;
- return $this->get($this->route("{$name}.create", [$this->varName($name) => $id]));
- }
- }
|