From a0ff52581dc5df4f0f1d6d2a9b8159f5dba0fe3c Mon Sep 17 00:00:00 2001 From: Vincenzo Petrucci Date: Tue, 5 Dec 2023 21:41:16 +0100 Subject: [PATCH 1/8] feat(methods): fixed error message text --- src/Plugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin.php b/src/Plugin.php index f80ec9c..6cac836 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -68,7 +68,7 @@ public function handleArguments(array $arguments): array View::render('components.badge', [ 'type' => 'ERROR', 'content' => 'Invalid JSON payload. Please provide a valid JSON payload.'. - 'Example: --payload=\'{"name": "Nuno"}\'', + 'Example: --post=\'{"name": "Nuno"}\'', ]); exit(0); From 179790c01db45274c2ffaed5fb07fd7fabb2b056 Mon Sep 17 00:00:00 2001 From: Vincenzo Petrucci Date: Tue, 5 Dec 2023 21:52:56 +0100 Subject: [PATCH 2/8] feat(methods): adds extractPayload method --- src/Plugin.php | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index 6cac836..ebc7b80 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -61,22 +61,31 @@ public function handleArguments(array $arguments): array } if (str_starts_with($argument, '--post=')) { - try { - $payload = (array) json_decode(str_replace('--post=', '', $argument), - true, 512, JSON_THROW_ON_ERROR); - } catch (\JsonException) { - View::render('components.badge', [ - 'type' => 'ERROR', - 'content' => 'Invalid JSON payload. Please provide a valid JSON payload.'. - 'Example: --post=\'{"name": "Nuno"}\'', - ]); - - exit(0); - } - $run->post($payload); + $run->post($this->extractPayload('post', $argument)); } } $run->dd(); } + + /** + * Extracts the payload from the argument. + * + * @return array + */ + private function extractPayload(string $method, string $argument): array + { + try { + return (array) json_decode(str_replace("--{$method}=", '', $argument), + true, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException) { + View::render('components.badge', [ + 'type' => 'ERROR', + 'content' => 'Invalid JSON payload. Please provide a valid JSON payload. '. + "Example: --{$method}='{\"name\": \"Nuno\"}'", + ]); + + exit(0); + } + } } From 90f7baddc68a492ee60fcfd6040aa0e86437ce00 Mon Sep 17 00:00:00 2001 From: Vincenzo Petrucci Date: Tue, 5 Dec 2023 22:38:31 +0100 Subject: [PATCH 3/8] feat(methods): adds support to other http verbs in run.js --- bin/run.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/bin/run.js b/bin/run.js index 1dd80da..dac430b 100644 --- a/bin/run.js +++ b/bin/run.js @@ -6,17 +6,42 @@ export default () => { let userAgent = 'Pest Plugin Stressless (https://pestphp.com) + K6 (https://k6.io)'; let url = __ENV.PEST_STRESS_TEST_URL; - let payload = options.payload ? JSON.stringify(options.payload) : JSON.stringify({}); + let payload = options.payload ? options.payload : {}; let method = options.method ? options.method : 'get'; switch (method) { + case 'delete': + http.del(url, null, { + headers: { 'user-agent': userAgent }, + }); + break; case 'get': http.get(url, { headers: { 'user-agent': userAgent }, }); break; + case 'head': + http.head(url, { + headers: { 'user-agent': userAgent }, + }); + break; + case 'options': + http.options(url, Object.keys(payload).length ? JSON.stringify(payload) : null, { + headers: { 'user-agent': userAgent }, + }); + break; + case 'patch': + http.patch(url, Object.keys(payload).length ? JSON.stringify(payload) : null, { + headers: { 'user-agent': userAgent }, + }); + break; + case 'put': + http.put(url, Object.keys(payload).length ? JSON.stringify(payload) : null, { + headers: { 'user-agent': userAgent }, + }); + break; case 'post': - http.post(url, payload, { + http.post(url, JSON.stringify(payload), { headers: { 'user-agent': userAgent }, }); break; From 5d255a9086e5293fb889b3f3d814c017fb310554 Mon Sep 17 00:00:00 2001 From: Vincenzo Petrucci Date: Tue, 5 Dec 2023 22:39:55 +0100 Subject: [PATCH 4/8] feat(methods): adds methods for remaining http verbs --- src/Factory.php | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/Factory.php b/src/Factory.php index b366369..efc2678 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -71,6 +71,16 @@ public function duration(int $seconds): self return $this; } + /** + * Force the test to use delete method + */ + public function delete(): self + { + $this->method = 'delete'; + + return $this; + } + /** * Force the test to use get method */ @@ -81,6 +91,55 @@ public function get(): self return $this; } + /** + * Force the test to use head method + */ + public function head(): self + { + $this->method = 'head'; + + return $this; + } + + /** + * Force the test to use options method + * + * @param array $payload The payload to send with the OPTIONS request + */ + public function options(array $payload = []): self + { + $this->method = 'options'; + $this->payload = $payload; + + return $this; + } + + /** + * Force the test to use patch method + * + * @param array $payload The payload to send with the PATCH request + */ + public function patch(array $payload = []): self + { + $this->method = 'patch'; + $this->payload = $payload; + + return $this; + } + + /** + * Force the test to use put method + * + * @param array $payload The payload to send with the PUT request + */ + public function put(array $payload = []): self + { + $this->method = 'put'; + $this->payload = $payload; + + return $this; + } + /** * Force the test to use post method * From 9438053defa312e6aaa975332d4a52167956d97c Mon Sep 17 00:00:00 2001 From: Vincenzo Petrucci Date: Tue, 5 Dec 2023 22:40:39 +0100 Subject: [PATCH 5/8] feat(methods): adds command line options for remaining http verbs --- src/Plugin.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Plugin.php b/src/Plugin.php index ebc7b80..a13eb0d 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -56,10 +56,36 @@ public function handleArguments(array $arguments): array $run->concurrently((int) str_replace('--concurrency=', '', $argument)); } + if ($argument === '--delete') { + $run->delete(); + } + if ($argument === '--get') { $run->get(); } + if ($argument === '--head') { + $run->head(); + } + + if (str_starts_with($argument, '--options=')) { + $run->options($this->extractPayload('options', $argument)); + } elseif ($argument === '--options') { + $run->options(); + } + + if (str_starts_with($argument, '--patch=')) { + $run->patch($this->extractPayload('patch', $argument)); + } elseif ($argument === '--patch') { + $run->patch(); + } + + if (str_starts_with($argument, '--put=')) { + $run->put($this->extractPayload('put', $argument)); + } elseif ($argument === '--put') { + $run->put(); + } + if (str_starts_with($argument, '--post=')) { $run->post($this->extractPayload('post', $argument)); } From 7fde60e708d417a95ca4c757d354d08a0397ef85 Mon Sep 17 00:00:00 2001 From: Vincenzo Petrucci Date: Wed, 6 Dec 2023 11:28:48 +0100 Subject: [PATCH 6/8] feat(methods): adds getter for method and payload properties --- src/Factory.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Factory.php b/src/Factory.php index efc2678..8e6e64a 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -153,6 +153,24 @@ public function post(array $payload): self return $this; } + /** + * Getter for the method property + */ + public function method(): string + { + return $this->method; + } + + /** + * Getter for the payload property + * + * @return array + */ + public function payload(): array + { + return $this->payload; + } + /** * Specifies that run should run with the given number of concurrent requests. */ From 1ccb112e40bcbae32f6f3a2fb2c4ba876fe9b937 Mon Sep 17 00:00:00 2001 From: Vincenzo Petrucci Date: Wed, 6 Dec 2023 11:29:01 +0100 Subject: [PATCH 7/8] feat(methods): adds unit test for Factory class --- tests/Unit/Factory.php | 72 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/Unit/Factory.php diff --git a/tests/Unit/Factory.php b/tests/Unit/Factory.php new file mode 100644 index 0000000..59867fe --- /dev/null +++ b/tests/Unit/Factory.php @@ -0,0 +1,72 @@ +stress->method())->toBe('get'); +}); + +it('correctly sets the delete method', function() { + $this->stress->delete(); + + expect($this->stress->method())->toBe('delete'); +}); + +it('correctly sets the get method', function() { + $this->stress->get(); + + expect($this->stress->method())->toBe('get'); +}); + +it('correctly sets the head method', function() { + $this->stress->head(); + + expect($this->stress->method())->toBe('head'); +}); + +it('correctly sets the options method without payload', function() { + $this->stress->options(); + + expect($this->stress->method())->toBe('options') + ->and($this->stress->payload())->toBe([]); +}); + +it('correctly sets the options method with payload', function() { + $this->stress->options(['foo' => 'bar']); + + expect($this->stress->method())->toBe('options') + ->and($this->stress->payload())->toBe(['foo' => 'bar']); +}); + +it('correctly sets the patch method without payload', function() { + $this->stress->patch(); + + expect($this->stress->method())->toBe('patch') + ->and($this->stress->payload())->toBe([]); +}); + +it('correctly sets the patch method with payload', function() { + $this->stress->patch(['foo' => 'bar']); + + expect($this->stress->method())->toBe('patch') + ->and($this->stress->payload())->toBe(['foo' => 'bar']); +}); + +it('correctly sets the put method without payload', function() { + $this->stress->put(); + + expect($this->stress->method())->toBe('put') + ->and($this->stress->payload())->toBe([]); +}); + +it('correctly sets the put method with payload', function() { + $this->stress->put(['foo' => 'bar']); + + expect($this->stress->method())->toBe('put') + ->and($this->stress->payload())->toBe(['foo' => 'bar']); +}); + +it('correctly sets the post method', function() { + $this->stress->post(['foo' => 'bar']); + + expect($this->stress->method())->toBe('post') + ->and($this->stress->payload())->toBe(['foo' => 'bar']); +}); \ No newline at end of file From ad9ebeb5a12d37ec9df2f54df461785be7b4e272 Mon Sep 17 00:00:00 2001 From: Vincenzo Petrucci Date: Wed, 6 Dec 2023 11:29:57 +0100 Subject: [PATCH 8/8] feat(methods): declare strict_types --- tests/Unit/Factory.php | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/tests/Unit/Factory.php b/tests/Unit/Factory.php index 59867fe..993354e 100644 --- a/tests/Unit/Factory.php +++ b/tests/Unit/Factory.php @@ -1,72 +1,74 @@ stress->method())->toBe('get'); }); -it('correctly sets the delete method', function() { +it('correctly sets the delete method', function () { $this->stress->delete(); expect($this->stress->method())->toBe('delete'); }); -it('correctly sets the get method', function() { +it('correctly sets the get method', function () { $this->stress->get(); expect($this->stress->method())->toBe('get'); }); -it('correctly sets the head method', function() { +it('correctly sets the head method', function () { $this->stress->head(); expect($this->stress->method())->toBe('head'); }); -it('correctly sets the options method without payload', function() { +it('correctly sets the options method without payload', function () { $this->stress->options(); expect($this->stress->method())->toBe('options') ->and($this->stress->payload())->toBe([]); }); -it('correctly sets the options method with payload', function() { +it('correctly sets the options method with payload', function () { $this->stress->options(['foo' => 'bar']); expect($this->stress->method())->toBe('options') ->and($this->stress->payload())->toBe(['foo' => 'bar']); }); -it('correctly sets the patch method without payload', function() { +it('correctly sets the patch method without payload', function () { $this->stress->patch(); expect($this->stress->method())->toBe('patch') ->and($this->stress->payload())->toBe([]); }); -it('correctly sets the patch method with payload', function() { +it('correctly sets the patch method with payload', function () { $this->stress->patch(['foo' => 'bar']); expect($this->stress->method())->toBe('patch') ->and($this->stress->payload())->toBe(['foo' => 'bar']); }); -it('correctly sets the put method without payload', function() { +it('correctly sets the put method without payload', function () { $this->stress->put(); expect($this->stress->method())->toBe('put') ->and($this->stress->payload())->toBe([]); }); -it('correctly sets the put method with payload', function() { +it('correctly sets the put method with payload', function () { $this->stress->put(['foo' => 'bar']); expect($this->stress->method())->toBe('put') ->and($this->stress->payload())->toBe(['foo' => 'bar']); }); -it('correctly sets the post method', function() { +it('correctly sets the post method', function () { $this->stress->post(['foo' => 'bar']); expect($this->stress->method())->toBe('post') ->and($this->stress->payload())->toBe(['foo' => 'bar']); -}); \ No newline at end of file +});