From 66aef609f26ce877f0c6f74c95a68076b2520514 Mon Sep 17 00:00:00 2001 From: ajcastro Date: Mon, 5 Jul 2021 17:01:01 +0800 Subject: [PATCH 1/5] Save --- .../BodyParameters/GetFromPerformedTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/Extracting/Strategies/BodyParameters/GetFromPerformedTest.php diff --git a/src/Extracting/Strategies/BodyParameters/GetFromPerformedTest.php b/src/Extracting/Strategies/BodyParameters/GetFromPerformedTest.php new file mode 100644 index 00000000..8c280ca5 --- /dev/null +++ b/src/Extracting/Strategies/BodyParameters/GetFromPerformedTest.php @@ -0,0 +1,13 @@ + Date: Fri, 9 Jul 2021 10:29:28 +0800 Subject: [PATCH 2/5] Save --- src/Exceptions/LaravelNotPresent.php | 16 +++ src/Extracting/RouteDocBlocker.php | 10 ++ .../Strategies/Headers/GetFromHeaderTag.php | 2 + .../Metadata/GetFromDocBlocksFromTest.php | 20 ++++ src/HttpExamples/HttpExampleCreator.php | 105 ++++++++++++++++++ .../HttpExampleCreatorMiddleware.php | 28 +++++ src/HttpExamples/RequestInfo.php | 82 ++++++++++++++ src/HttpExamples/RequestInspector.php | 34 ++++++ src/HttpExamples/ResponseInfo.php | 54 +++++++++ src/HttpExamples/ResponseInspector.php | 29 +++++ src/HttpExamples/RouteInfo.php | 37 ++++++ src/HttpExamples/RouteInspector.php | 47 ++++++++ src/HttpExamples/SessionInspector.php | 32 ++++++ src/ScribeTestServiceProvider.php | 26 +++++ src/Tests/ExampleCreator.php | 30 +++++ src/Tests/ScribeSetup.php | 44 ++++++++ 16 files changed, 596 insertions(+) create mode 100644 src/Exceptions/LaravelNotPresent.php create mode 100644 src/Extracting/Strategies/Metadata/GetFromDocBlocksFromTest.php create mode 100644 src/HttpExamples/HttpExampleCreator.php create mode 100644 src/HttpExamples/HttpExampleCreatorMiddleware.php create mode 100644 src/HttpExamples/RequestInfo.php create mode 100644 src/HttpExamples/RequestInspector.php create mode 100644 src/HttpExamples/ResponseInfo.php create mode 100644 src/HttpExamples/ResponseInspector.php create mode 100644 src/HttpExamples/RouteInfo.php create mode 100644 src/HttpExamples/RouteInspector.php create mode 100644 src/HttpExamples/SessionInspector.php create mode 100644 src/ScribeTestServiceProvider.php create mode 100644 src/Tests/ExampleCreator.php create mode 100644 src/Tests/ScribeSetup.php diff --git a/src/Exceptions/LaravelNotPresent.php b/src/Exceptions/LaravelNotPresent.php new file mode 100644 index 00000000..3ba11fff --- /dev/null +++ b/src/Exceptions/LaravelNotPresent.php @@ -0,0 +1,16 @@ +route)['method']; + // dd($methodDocBlock); + return $this->getHeadersFromDocBlock($methodDocBlock->getTags()); } diff --git a/src/Extracting/Strategies/Metadata/GetFromDocBlocksFromTest.php b/src/Extracting/Strategies/Metadata/GetFromDocBlocksFromTest.php new file mode 100644 index 00000000..a1e71b76 --- /dev/null +++ b/src/Extracting/Strategies/Metadata/GetFromDocBlocksFromTest.php @@ -0,0 +1,20 @@ +route); + $methodDocBlock = $docBlocks['method']; + $classDocBlock = $docBlocks['class']; + + return $this->getMetadataFromDocBlock($methodDocBlock, $classDocBlock); + } +} diff --git a/src/HttpExamples/HttpExampleCreator.php b/src/HttpExamples/HttpExampleCreator.php new file mode 100644 index 00000000..6fb9e3e0 --- /dev/null +++ b/src/HttpExamples/HttpExampleCreator.php @@ -0,0 +1,105 @@ +exampleCreator = $exampleCreator; + $this->requestInspector = $requestInspector; + $this->routeInspector = $routeInspector; + $this->responseInspector = $responseInspector; + $this->sessionInspector = $sessionInspector; + } + + public static function followingRedirect(Closure $callback) + { + static::$followsRedirect = true; + + $response = $callback(); + + static::$followsRedirect = false; + + return $response; + } + + public function createHttpExample(Request $request) + { + $testExample = $this->exampleCreator->getCurrentExample(); + + if (is_null($testExample)) { + return; + } + + $testExample->addRequest( + $this->requestInspector->getDataFrom($request) + ); + } + + public function saveHttpResponseData(Request $request, Response $response) + { + $testExample = $this->exampleCreator->getCurrentExample(); + + if (is_null($testExample)) { + return; + } + + $testExample->setResponse( + $this->responseInspector->getDataFrom($this->normalizeResponse($response)), + static::$followsRedirect, + $this->routeInspector->getInfoFrom($request->route()), + $this->sessionInspector->getData() + ); + } + + private function normalizeResponse(Response $response) + { + if ($response instanceof TestResponse) { + $response = $response->baseResponse; + } + + return $response; + } +} diff --git a/src/HttpExamples/HttpExampleCreatorMiddleware.php b/src/HttpExamples/HttpExampleCreatorMiddleware.php new file mode 100644 index 00000000..40e031de --- /dev/null +++ b/src/HttpExamples/HttpExampleCreatorMiddleware.php @@ -0,0 +1,28 @@ +httpExampleCreator->createHttpExample($request); + + dump('pumasok'); + $response = $next($request); + + dump($response->getContent()); + sleep(2); + + // $this->httpExampleCreator->saveHttpResponseData($request, $response); + + return $response; + } +} diff --git a/src/HttpExamples/RequestInfo.php b/src/HttpExamples/RequestInfo.php new file mode 100644 index 00000000..02682595 --- /dev/null +++ b/src/HttpExamples/RequestInfo.php @@ -0,0 +1,82 @@ +method = $method; + $this->path = $path; + $this->headers = $headers; + $this->queryParameters = $queryParameters; + $this->input = $input; + $this->files = $files; + } + + public function getMethod(): string + { + return $this->method; + } + + public function getPath(): string + { + return $this->path; + } + + public function getHeaders(): array + { + return $this->headers; + } + + public function getQueryParameters(): array + { + return $this->queryParameters; + } + + public function getInput(): array + { + return $this->input; + } + + public function getFiles(): array + { + return $this->files; + } +} diff --git a/src/HttpExamples/RequestInspector.php b/src/HttpExamples/RequestInspector.php new file mode 100644 index 00000000..9c9deee2 --- /dev/null +++ b/src/HttpExamples/RequestInspector.php @@ -0,0 +1,34 @@ +method(), + $request->path(), + $request->headers->all(), + $request->query(), + $request->post(), + $this->getFilesInfo($request->allFiles()), + ); + } + + public function getFilesInfo(array $files): array + { + return collect($files) + ->map(function (UploadedFile $file) { + return [ + 'name' => $file->getClientOriginalName(), + 'type' => $file->getMimeType(), + 'size' => intdiv($file->getSize(), 1024), + ]; + }) + ->all(); + } +} diff --git a/src/HttpExamples/ResponseInfo.php b/src/HttpExamples/ResponseInfo.php new file mode 100644 index 00000000..47186994 --- /dev/null +++ b/src/HttpExamples/ResponseInfo.php @@ -0,0 +1,54 @@ +statusCode = $statusCode; + $this->headers = $headers; + $this->content = $content; + $this->template = $template; + } + + public function getStatusCode(): int + { + return $this->statusCode; + } + + public function getHeaders(): array + { + return $this->headers; + } + + public function getContent(): string + { + return $this->content; + } + + public function getTemplate(): ?string + { + return $this->template; + } +} diff --git a/src/HttpExamples/ResponseInspector.php b/src/HttpExamples/ResponseInspector.php new file mode 100644 index 00000000..87bc3b5a --- /dev/null +++ b/src/HttpExamples/ResponseInspector.php @@ -0,0 +1,29 @@ +getStatusCode(), + $response->headers->all(), + $response->getContent(), + $this->getTemplate($response) + ); + } + + protected function getTemplate(Response $response): ?string + { + if (isset($response->original) && $response->original instanceof View) { + return File::get($response->original->getPath()); + } + + return null; + } +} diff --git a/src/HttpExamples/RouteInfo.php b/src/HttpExamples/RouteInfo.php new file mode 100644 index 00000000..c2d774ca --- /dev/null +++ b/src/HttpExamples/RouteInfo.php @@ -0,0 +1,37 @@ +uri = null; + $this->parameters = null; + } else { + $this->uri = $uri; + $this->parameters = $parameters; + } + } + + public function getUri(): ?string + { + return $this->uri; + } + + public function getParameters(): ?array + { + return $this->parameters; + } +} diff --git a/src/HttpExamples/RouteInspector.php b/src/HttpExamples/RouteInspector.php new file mode 100644 index 00000000..dc37b5d7 --- /dev/null +++ b/src/HttpExamples/RouteInspector.php @@ -0,0 +1,47 @@ +uri(), $this->getParameters($route)); + } + + /** + * Get all the route parameters as keys and the parameter-where conditions as values. + * + * @return array + */ + protected function getParameters(Route $route): array + { + return collect($route->parameterNames()) + ->mapWithKeys(function ($parameter) { + return [$parameter => '*']; + }) + ->merge( + array_intersect_key($route->wheres, $route->originalParameters()) + ) + ->map(function ($pattern, $name) use ($route) { + return [ + 'name' => $name, + 'pattern' => $pattern, + 'optional' => $this->isParameterOptional($route, $name), + ]; + }) + ->values() + ->all(); + } + + protected function isParameterOptional(Route $route, $parameter): bool + { + return (bool) preg_match("/{{$parameter}\?}/", $route->uri()); + } +} diff --git a/src/HttpExamples/SessionInspector.php b/src/HttpExamples/SessionInspector.php new file mode 100644 index 00000000..13c73a8d --- /dev/null +++ b/src/HttpExamples/SessionInspector.php @@ -0,0 +1,32 @@ +session = $session; + } + + public function getData(): array + { + $session = $this->session->all(); + + // Wrap the errors array in a collection so it can be + // exported by calling the toArray method since the + // error bags implement the Arrayable interface. + if (! empty($session['errors'])) { + $session['errors'] = collect($session['errors']->getBags()); + } + + return collect($session)->toArray(); + } +} diff --git a/src/ScribeTestServiceProvider.php b/src/ScribeTestServiceProvider.php new file mode 100644 index 00000000..d8c12cd0 --- /dev/null +++ b/src/ScribeTestServiceProvider.php @@ -0,0 +1,26 @@ +app->environment('testing') && + $this->app->runningInConsole() && + config('scribe.generate_test_examples', true) + ) { + $this->registerMiddleware(); + } + } + + private function registerMiddleware(): void + { + $this->app[HttpKernel::class]->pushMiddleware(HttpExampleCreatorMiddleware::class); + } +} diff --git a/src/Tests/ExampleCreator.php b/src/Tests/ExampleCreator.php new file mode 100644 index 00000000..4716cd70 --- /dev/null +++ b/src/Tests/ExampleCreator.php @@ -0,0 +1,30 @@ + $value) { + $this->{$key} = $value; + } + } + + public static function getCurrentInstance() + { + return static::$currentInstance; + } + + public static function setCurrentInstance($instance) + { + static::$currentInstance = $instance; + } +} diff --git a/src/Tests/ScribeSetup.php b/src/Tests/ScribeSetup.php new file mode 100644 index 00000000..6ef45125 --- /dev/null +++ b/src/Tests/ScribeSetup.php @@ -0,0 +1,44 @@ +app)) { + throw new LaravelNotPresent; + } + + if (config('scribe.generate_test_examples', true)) { + $this->afterApplicationCreated(function () { + $this->makeExample(); + }); + + $this->beforeApplicationDestroyed(function () { + // $this->saveExampleStatus(); + }); + } + } + + private function makeExample(): void + { + $exampleCreator = new ExampleCreator([ + 'test' => $this, + 'methodName' => $this->getName(false), + 'providedData' => $this->getProvidedData(), + 'dataName' => $this->dataName(), + ]); + + ExampleCreator::setCurrentInstance($exampleCreator); + + // $this->app->make(ExampleCreator::class)->makeExample( + // get_class($this), + // $this->getName(false), + // $this->getProvidedData(), + // $this->dataName() + // ); + } +} From d2373737d8e96bd58977cbc876c5238b8e822803 Mon Sep 17 00:00:00 2001 From: ajcastro Date: Fri, 9 Jul 2021 10:33:50 +0800 Subject: [PATCH 3/5] Save --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ecaf6538..5779e4d2 100644 --- a/composer.json +++ b/composer.json @@ -72,7 +72,8 @@ "extra": { "laravel": { "providers": [ - "Knuckles\\Scribe\\ScribeServiceProvider" + "Knuckles\\Scribe\\ScribeServiceProvider", + "Knuckles\\Scribe\\ScribeTestServiceProvider" ] } }, From 4ca17817674295a30dd2d8013c5f87fa0c9af5a8 Mon Sep 17 00:00:00 2001 From: ajcastro Date: Fri, 9 Jul 2021 10:39:51 +0800 Subject: [PATCH 4/5] Save --- src/HttpExamples/HttpExampleCreator.php | 105 ------------------ src/HttpExamples/RequestInfo.php | 82 -------------- src/HttpExamples/RequestInspector.php | 34 ------ src/HttpExamples/ResponseInfo.php | 54 --------- src/HttpExamples/ResponseInspector.php | 29 ----- src/HttpExamples/RouteInfo.php | 37 ------ src/HttpExamples/RouteInspector.php | 47 -------- src/HttpExamples/SessionInspector.php | 32 ------ src/ScribeTestServiceProvider.php | 2 +- .../HttpExampleCreatorMiddleware.php | 5 +- 10 files changed, 2 insertions(+), 425 deletions(-) delete mode 100644 src/HttpExamples/HttpExampleCreator.php delete mode 100644 src/HttpExamples/RequestInfo.php delete mode 100644 src/HttpExamples/RequestInspector.php delete mode 100644 src/HttpExamples/ResponseInfo.php delete mode 100644 src/HttpExamples/ResponseInspector.php delete mode 100644 src/HttpExamples/RouteInfo.php delete mode 100644 src/HttpExamples/RouteInspector.php delete mode 100644 src/HttpExamples/SessionInspector.php rename src/{ => Tests}/HttpExamples/HttpExampleCreatorMiddleware.php (86%) diff --git a/src/HttpExamples/HttpExampleCreator.php b/src/HttpExamples/HttpExampleCreator.php deleted file mode 100644 index 6fb9e3e0..00000000 --- a/src/HttpExamples/HttpExampleCreator.php +++ /dev/null @@ -1,105 +0,0 @@ -exampleCreator = $exampleCreator; - $this->requestInspector = $requestInspector; - $this->routeInspector = $routeInspector; - $this->responseInspector = $responseInspector; - $this->sessionInspector = $sessionInspector; - } - - public static function followingRedirect(Closure $callback) - { - static::$followsRedirect = true; - - $response = $callback(); - - static::$followsRedirect = false; - - return $response; - } - - public function createHttpExample(Request $request) - { - $testExample = $this->exampleCreator->getCurrentExample(); - - if (is_null($testExample)) { - return; - } - - $testExample->addRequest( - $this->requestInspector->getDataFrom($request) - ); - } - - public function saveHttpResponseData(Request $request, Response $response) - { - $testExample = $this->exampleCreator->getCurrentExample(); - - if (is_null($testExample)) { - return; - } - - $testExample->setResponse( - $this->responseInspector->getDataFrom($this->normalizeResponse($response)), - static::$followsRedirect, - $this->routeInspector->getInfoFrom($request->route()), - $this->sessionInspector->getData() - ); - } - - private function normalizeResponse(Response $response) - { - if ($response instanceof TestResponse) { - $response = $response->baseResponse; - } - - return $response; - } -} diff --git a/src/HttpExamples/RequestInfo.php b/src/HttpExamples/RequestInfo.php deleted file mode 100644 index 02682595..00000000 --- a/src/HttpExamples/RequestInfo.php +++ /dev/null @@ -1,82 +0,0 @@ -method = $method; - $this->path = $path; - $this->headers = $headers; - $this->queryParameters = $queryParameters; - $this->input = $input; - $this->files = $files; - } - - public function getMethod(): string - { - return $this->method; - } - - public function getPath(): string - { - return $this->path; - } - - public function getHeaders(): array - { - return $this->headers; - } - - public function getQueryParameters(): array - { - return $this->queryParameters; - } - - public function getInput(): array - { - return $this->input; - } - - public function getFiles(): array - { - return $this->files; - } -} diff --git a/src/HttpExamples/RequestInspector.php b/src/HttpExamples/RequestInspector.php deleted file mode 100644 index 9c9deee2..00000000 --- a/src/HttpExamples/RequestInspector.php +++ /dev/null @@ -1,34 +0,0 @@ -method(), - $request->path(), - $request->headers->all(), - $request->query(), - $request->post(), - $this->getFilesInfo($request->allFiles()), - ); - } - - public function getFilesInfo(array $files): array - { - return collect($files) - ->map(function (UploadedFile $file) { - return [ - 'name' => $file->getClientOriginalName(), - 'type' => $file->getMimeType(), - 'size' => intdiv($file->getSize(), 1024), - ]; - }) - ->all(); - } -} diff --git a/src/HttpExamples/ResponseInfo.php b/src/HttpExamples/ResponseInfo.php deleted file mode 100644 index 47186994..00000000 --- a/src/HttpExamples/ResponseInfo.php +++ /dev/null @@ -1,54 +0,0 @@ -statusCode = $statusCode; - $this->headers = $headers; - $this->content = $content; - $this->template = $template; - } - - public function getStatusCode(): int - { - return $this->statusCode; - } - - public function getHeaders(): array - { - return $this->headers; - } - - public function getContent(): string - { - return $this->content; - } - - public function getTemplate(): ?string - { - return $this->template; - } -} diff --git a/src/HttpExamples/ResponseInspector.php b/src/HttpExamples/ResponseInspector.php deleted file mode 100644 index 87bc3b5a..00000000 --- a/src/HttpExamples/ResponseInspector.php +++ /dev/null @@ -1,29 +0,0 @@ -getStatusCode(), - $response->headers->all(), - $response->getContent(), - $this->getTemplate($response) - ); - } - - protected function getTemplate(Response $response): ?string - { - if (isset($response->original) && $response->original instanceof View) { - return File::get($response->original->getPath()); - } - - return null; - } -} diff --git a/src/HttpExamples/RouteInfo.php b/src/HttpExamples/RouteInfo.php deleted file mode 100644 index c2d774ca..00000000 --- a/src/HttpExamples/RouteInfo.php +++ /dev/null @@ -1,37 +0,0 @@ -uri = null; - $this->parameters = null; - } else { - $this->uri = $uri; - $this->parameters = $parameters; - } - } - - public function getUri(): ?string - { - return $this->uri; - } - - public function getParameters(): ?array - { - return $this->parameters; - } -} diff --git a/src/HttpExamples/RouteInspector.php b/src/HttpExamples/RouteInspector.php deleted file mode 100644 index dc37b5d7..00000000 --- a/src/HttpExamples/RouteInspector.php +++ /dev/null @@ -1,47 +0,0 @@ -uri(), $this->getParameters($route)); - } - - /** - * Get all the route parameters as keys and the parameter-where conditions as values. - * - * @return array - */ - protected function getParameters(Route $route): array - { - return collect($route->parameterNames()) - ->mapWithKeys(function ($parameter) { - return [$parameter => '*']; - }) - ->merge( - array_intersect_key($route->wheres, $route->originalParameters()) - ) - ->map(function ($pattern, $name) use ($route) { - return [ - 'name' => $name, - 'pattern' => $pattern, - 'optional' => $this->isParameterOptional($route, $name), - ]; - }) - ->values() - ->all(); - } - - protected function isParameterOptional(Route $route, $parameter): bool - { - return (bool) preg_match("/{{$parameter}\?}/", $route->uri()); - } -} diff --git a/src/HttpExamples/SessionInspector.php b/src/HttpExamples/SessionInspector.php deleted file mode 100644 index 13c73a8d..00000000 --- a/src/HttpExamples/SessionInspector.php +++ /dev/null @@ -1,32 +0,0 @@ -session = $session; - } - - public function getData(): array - { - $session = $this->session->all(); - - // Wrap the errors array in a collection so it can be - // exported by calling the toArray method since the - // error bags implement the Arrayable interface. - if (! empty($session['errors'])) { - $session['errors'] = collect($session['errors']->getBags()); - } - - return collect($session)->toArray(); - } -} diff --git a/src/ScribeTestServiceProvider.php b/src/ScribeTestServiceProvider.php index d8c12cd0..48fcf058 100644 --- a/src/ScribeTestServiceProvider.php +++ b/src/ScribeTestServiceProvider.php @@ -4,7 +4,7 @@ use Illuminate\Contracts\Http\Kernel as HttpKernel; use Illuminate\Support\ServiceProvider; -use Knuckles\Scribe\HttpExamples\HttpExampleCreatorMiddleware; +use Knuckles\Scribe\Tests\HttpExamples\HttpExampleCreatorMiddleware; class ScribeTestServiceProvider extends ServiceProvider { diff --git a/src/HttpExamples/HttpExampleCreatorMiddleware.php b/src/Tests/HttpExamples/HttpExampleCreatorMiddleware.php similarity index 86% rename from src/HttpExamples/HttpExampleCreatorMiddleware.php rename to src/Tests/HttpExamples/HttpExampleCreatorMiddleware.php index 40e031de..0d9fefce 100644 --- a/src/HttpExamples/HttpExampleCreatorMiddleware.php +++ b/src/Tests/HttpExamples/HttpExampleCreatorMiddleware.php @@ -1,6 +1,6 @@ getContent()); - sleep(2); - // $this->httpExampleCreator->saveHttpResponseData($request, $response); return $response; From 8e1964126d6121b0811ff88dc8c8f9a9adf1404f Mon Sep 17 00:00:00 2001 From: ajcastro Date: Fri, 9 Jul 2021 14:01:59 +0800 Subject: [PATCH 5/5] Save work --- src/ScribeTestServiceProvider.php | 3 +- src/Tests/ExampleCreator.php | 140 +++++++++++++++++- src/Tests/ExampleRequest.php | 39 +++++ .../HttpExampleCreatorMiddleware.php | 13 +- src/Tests/ScribeSetup.php | 16 +- src/Tests/Traits/SetProps.php | 15 ++ 6 files changed, 202 insertions(+), 24 deletions(-) create mode 100644 src/Tests/ExampleRequest.php create mode 100644 src/Tests/Traits/SetProps.php diff --git a/src/ScribeTestServiceProvider.php b/src/ScribeTestServiceProvider.php index 48fcf058..f72fad42 100644 --- a/src/ScribeTestServiceProvider.php +++ b/src/ScribeTestServiceProvider.php @@ -21,6 +21,7 @@ public function boot() private function registerMiddleware(): void { - $this->app[HttpKernel::class]->pushMiddleware(HttpExampleCreatorMiddleware::class); + $this->app[HttpKernel::class]->appendMiddlewareToGroup('web', HttpExampleCreatorMiddleware::class); + $this->app[HttpKernel::class]->appendMiddlewareToGroup('api', HttpExampleCreatorMiddleware::class); } } diff --git a/src/Tests/ExampleCreator.php b/src/Tests/ExampleCreator.php index 4716cd70..3fe2d221 100644 --- a/src/Tests/ExampleCreator.php +++ b/src/Tests/ExampleCreator.php @@ -2,20 +2,37 @@ namespace Knuckles\Scribe\Tests; -class ExampleCreator +use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Routing\Route; +use Illuminate\Support\Str; +use Knuckles\Scribe\Tests\ExampleRequest; +use Knuckles\Scribe\Tests\Traits\SetProps; + +class ExampleCreator implements Arrayable { - public $test; - public $methodName; + use SetProps; + + public $id; + public $testClass; + public $testMethod; public $providedData; public $dataName; + private Route $route; + private $exampleRequests; + private $test; + public static $currentInstance; - public function __construct(array $attributes) + public static $instances; + + public function __construct(array $props) { - foreach ($attributes as $key => $value) { - $this->{$key} = $value; - } + $this->setProps($props); + + $this->id = (string) Str::orderedUuid(); + + $this->testClass = get_class($this->test); } public static function getCurrentInstance() @@ -27,4 +44,113 @@ public static function setCurrentInstance($instance) { static::$currentInstance = $instance; } + + public static function normalizeUriForInstanceKey(Route $route) + { + $parts = [ + str_replace('/', '~', $route->uri) + ]; + $parts = array_merge($parts, $route->methods); + + return implode(',', $parts); + } + + public static function getInstanceForRoute($route) + { + if ($instance = static::$instances[static::normalizeUriForInstanceKey($route)] ?? null) { + return $instance; + } + + $instance = static::getCurrentInstance()->setRoute($route); + + return static::registerInstance($instance); + } + + protected static function registerInstance($instance) + { + return static::$instances[$instance->instanceKey()] = $instance; + } + + public static function getInstances() + { + return static::$instances; + } + + public static function flushInstances() + { + static::$instances = []; + } + + public function addExampleRequest(ExampleRequest $exampleRequest) + { + $this->exampleRequests[] = $exampleRequest; + + return $this; + } + + public function setRoute(Route $route) + { + $this->route = $route; + + return $this; + } + + public function instanceKey() + { + return $this->normalizeUriForInstanceKey($this->route); + } + + public function writeDir() + { + return storage_path('scribe/'.$this->instanceKey()); + } + + public function toArray() + { + return [ + 'id' => $this->id, + 'test_class' => $this->testClass, + 'test_method' => $this->testMethod, + 'provided_data' => $this->providedData, + 'data_name' => $this->dataName, + 'key' => $this->instanceKey(), + 'route' => [ + 'uri' => $this->route->uri, + 'name' => $this->route->getName(), + 'methods' => $this->route->methods, + ], + ]; + } + + public function mergeParamsExample($type) + { + $method = 'get'.ucfirst($type).'ParamsExample'; + $results = []; + + foreach ($exampleRequests as $request) { + $results = array_merge($results, $request->{$method}()); + } + + return $results; + } + + public function writeExampleRequests() + { + return [ + 'urlParam' => [], + 'queryParam' => [], + 'bodyParam' => [], + 'responses' => [ + [ + 'status' => 200, + 'scenario' => 'test_user_should_return_user', + 'data' => [], + ], [ + 'status' => 404, + 'scenario' => 'test_user_not_found', + 'data' => [], + ], + ], + ]; + } } diff --git a/src/Tests/ExampleRequest.php b/src/Tests/ExampleRequest.php new file mode 100644 index 00000000..07ae6eae --- /dev/null +++ b/src/Tests/ExampleRequest.php @@ -0,0 +1,39 @@ +id = (string) Str::orderedUuid(); + $this->request = $request; + $this->response = $response; + } + + public function getUrlParamsExample() + { + return []; + } + + public function getQueryParamsExample() + { + return []; + } + + public function getBodyParamsExample() + { + return $this->request->all(); + } + + public function getResponse() + { + return $this->response->getContent(); + } +} diff --git a/src/Tests/HttpExamples/HttpExampleCreatorMiddleware.php b/src/Tests/HttpExamples/HttpExampleCreatorMiddleware.php index 0d9fefce..b1eb0258 100644 --- a/src/Tests/HttpExamples/HttpExampleCreatorMiddleware.php +++ b/src/Tests/HttpExamples/HttpExampleCreatorMiddleware.php @@ -4,21 +4,18 @@ use Closure; use Illuminate\Testing\TestResponse; -use Styde\Enlighten\Enlighten; +use Knuckles\Scribe\Tests\ExampleCreator; +use Knuckles\Scribe\Tests\ExampleRequest; class HttpExampleCreatorMiddleware { public function handle($request, Closure $next) { - // Create the example and persist the request data before - // running the actual request, so if the HTTP call fails - // we will have information about the original request. - // $this->httpExampleCreator->createHttpExample($request); - - dump('pumasok'); $response = $next($request); - // $this->httpExampleCreator->saveHttpResponseData($request, $response); + $exampleCreator = ExampleCreator::getInstanceForRoute($request->route()); + + $exampleCreator->addExampleRequest(new ExampleRequest($request, $response)); return $response; } diff --git a/src/Tests/ScribeSetup.php b/src/Tests/ScribeSetup.php index 6ef45125..88daf102 100644 --- a/src/Tests/ScribeSetup.php +++ b/src/Tests/ScribeSetup.php @@ -14,10 +14,17 @@ public function setUpScribe(): void if (config('scribe.generate_test_examples', true)) { $this->afterApplicationCreated(function () { + dump('making example...'); $this->makeExample(); }); $this->beforeApplicationDestroyed(function () { + dump('writing examples...'); + $instances = ExampleCreator::getInstances(); + foreach ($instances as $instance) { + dump($instance->toArray()); + } + ExampleCreator::flushInstances(); // $this->saveExampleStatus(); }); } @@ -27,18 +34,11 @@ private function makeExample(): void { $exampleCreator = new ExampleCreator([ 'test' => $this, - 'methodName' => $this->getName(false), + 'testMethod' => $this->getName(false), 'providedData' => $this->getProvidedData(), 'dataName' => $this->dataName(), ]); ExampleCreator::setCurrentInstance($exampleCreator); - - // $this->app->make(ExampleCreator::class)->makeExample( - // get_class($this), - // $this->getName(false), - // $this->getProvidedData(), - // $this->dataName() - // ); } } diff --git a/src/Tests/Traits/SetProps.php b/src/Tests/Traits/SetProps.php new file mode 100644 index 00000000..75398e41 --- /dev/null +++ b/src/Tests/Traits/SetProps.php @@ -0,0 +1,15 @@ + $value) { + $this->{$key} = $value; + } + + return $this; + } +}