From df41ef1ac5b4c3a54803d930552f376c7fab7951 Mon Sep 17 00:00:00 2001 From: Marton Baranyai Date: Thu, 7 Jan 2021 22:31:28 +0000 Subject: [PATCH 1/6] =?UTF-8?q?bugfix=20create=20and=20edit=20pages=20read?= =?UTF-8?q?=20from=20=E2=80=9Cfields=E2=80=9D=20not=20from=20=E2=80=9Creso?= =?UTF-8?q?urce.fields=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit index — resources.0.fields detail — resource.fields create, edit — fields --- src/Assert/AssertFields.php | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Assert/AssertFields.php b/src/Assert/AssertFields.php index 7123fed..7d2d7fb 100644 --- a/src/Assert/AssertFields.php +++ b/src/Assert/AssertFields.php @@ -10,7 +10,7 @@ trait AssertFields { public function assertFieldCount($amount) { - $path = isset($this->original['resources']) ? 'resources.0.fields' : 'resource.fields'; + $path = isset($this->original[ 'resources' ]) ? 'resources.0.fields' : $this->getFieldsPath(); $this->assertJsonCount($amount, $path); @@ -19,15 +19,7 @@ public function assertFieldCount($amount) public function assertFields(closure $callable) { - if (isset($this->original['resources'][0]['fields'])) { - $path = 'resources.*.fields'; - } elseif (isset($this->original['resource']['fields'])) { - $path = 'resource.fields'; - } elseif (isset($this->original['fields'])) { - $path = 'fields'; - } - - $fields = collect(json_decode(json_encode(data_get($this->original, $path, []), true))); + $fields = collect(json_decode(json_encode(data_get($this->original, $this->getFieldsPath(), []), true))); PHPUnit::assertTrue($callable($fields)); @@ -123,4 +115,18 @@ public function assertFieldKeyValue($attribute, $method) return $this; } + + /** + * @return string + */ + protected function getFieldsPath() + { + if ( isset($this->original[ 'resources' ][ 0 ][ 'fields' ]) ) { + return 'resources.*.fields'; + } elseif ( isset($this->original[ 'resource' ][ 'fields' ]) ) { + return 'resource.fields'; + } + + return 'fields'; + } } From 13cd0e77a59dd6eb56d8c97069a7b57dbf981e15 Mon Sep 17 00:00:00 2001 From: Marton Baranyai Date: Fri, 29 Oct 2021 01:32:33 +0200 Subject: [PATCH 2/6] optional param order --- src/Assert/AssertFields.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Assert/AssertFields.php b/src/Assert/AssertFields.php index 7d2d7fb..f5d8ccf 100644 --- a/src/Assert/AssertFields.php +++ b/src/Assert/AssertFields.php @@ -26,48 +26,48 @@ public function assertFields(closure $callable) return $this; } - public function assertFieldsInclude($attribute, $value=null) + public function assertFieldsInclude($attribute, $value = null) { return $this->fieldCheck($attribute, $value, 'assertJsonFragment'); } - public function assertFieldsExclude($attribute, $value=null) + public function assertFieldsExclude($attribute, $value = null) { return $this->fieldCheck($attribute, $value, 'assertJsonMissing'); } - protected function fieldCheck($attribute, $value = null, $method) + protected function fieldCheck($attribute, $value = null, $method = null) { - if ($attribute instanceof Collection) { + if ( $attribute instanceof Collection ) { $attribute = $attribute->toArray(); } - if ($value instanceof Collection) { + if ( $value instanceof Collection ) { $value = $value->toArray(); } // ->assertFieldsInclude('id') - if (!is_array($attribute) && is_null($value)) { + if ( ! is_array($attribute) && is_null($value) ) { return $this->assertFieldAttribute($attribute, $method); } // ->assertFieldsInclude('id', [1,2,3]) - if (!is_array($attribute) && is_array($value)) { + if ( ! is_array($attribute) && is_array($value) ) { return $this->assertFieldManyValues($attribute, $value, $method); } // ->assertFieldsInclude('id', 1) - if (!is_array($attribute) && !is_null($value)) { + if ( ! is_array($attribute) && ! is_null($value) ) { return $this->assertFieldValue($attribute, $value, $method); } // ->assertFieldsInclude(['id', 'email']) - if (is_array($attribute) && is_numeric(array_keys($attribute)[0])) { + if ( is_array($attribute) && is_numeric(array_keys($attribute)[ 0 ]) ) { return $this->assertFieldKeys($attribute, $method); } // ->assertFieldsInclude(['id' => 1]) - if (is_array($attribute)) { + if ( is_array($attribute) ) { return $this->assertFieldKeyValue($attribute, $method); } @@ -85,7 +85,7 @@ public function assertFieldValue($attribute, $value, $method) { return $this->$method([ 'attribute' => $attribute, - 'value' => $value, + 'value' => $value, ]); } From 6288ab9522dc8e2c0f2e42865dff4c031b2094f1 Mon Sep 17 00:00:00 2001 From: imkdenis Date: Thu, 17 Aug 2023 21:52:45 +0200 Subject: [PATCH 3/6] feat: novaStore and novaUpdate methods, some fixes and enhancements --- src/Assert/AssertFields.php | 74 +++++++++++++++++++++------------- src/Assert/AssertResources.php | 9 +++++ src/NovaAssertions.php | 36 +++++++++++++++-- 3 files changed, 87 insertions(+), 32 deletions(-) diff --git a/src/Assert/AssertFields.php b/src/Assert/AssertFields.php index f5d8ccf..743fdc3 100644 --- a/src/Assert/AssertFields.php +++ b/src/Assert/AssertFields.php @@ -10,7 +10,7 @@ trait AssertFields { public function assertFieldCount($amount) { - $path = isset($this->original[ 'resources' ]) ? 'resources.0.fields' : $this->getFieldsPath(); + $path = isset($this->original['resources']) ? 'resources.0.fields' : 'resource.fields'; $this->assertJsonCount($amount, $path); @@ -19,55 +19,65 @@ public function assertFieldCount($amount) public function assertFields(closure $callable) { - $fields = collect(json_decode(json_encode(data_get($this->original, $this->getFieldsPath(), []), true))); + if (isset($this->original['resources'][0]['fields'])) { + $path = 'resources.*.fields'; + } elseif (isset($this->original['resource']['fields'])) { + $path = 'resource.fields'; + } elseif (isset($this->original['fields'])) { + $path = 'fields'; + } + + $fields = collect(json_decode(json_encode(data_get($this->original, $path, []), true))); + + $isCallingOk = $callable($fields) ? true : false; - PHPUnit::assertTrue($callable($fields)); + PHPUnit::assertTrue($isCallingOk); return $this; } - public function assertFieldsInclude($attribute, $value = null) + public function assertFieldsInclude($attribute, $value=null) { return $this->fieldCheck($attribute, $value, 'assertJsonFragment'); } - public function assertFieldsExclude($attribute, $value = null) + public function assertFieldsExclude($attribute, $value=null) { return $this->fieldCheck($attribute, $value, 'assertJsonMissing'); } - protected function fieldCheck($attribute, $value = null, $method = null) + protected function fieldCheck($attribute, $value = null, $method) { - if ( $attribute instanceof Collection ) { + if ($attribute instanceof Collection) { $attribute = $attribute->toArray(); } - if ( $value instanceof Collection ) { + if ($value instanceof Collection) { $value = $value->toArray(); } // ->assertFieldsInclude('id') - if ( ! is_array($attribute) && is_null($value) ) { + if (!is_array($attribute) && is_null($value)) { return $this->assertFieldAttribute($attribute, $method); } // ->assertFieldsInclude('id', [1,2,3]) - if ( ! is_array($attribute) && is_array($value) ) { + if (!is_array($attribute) && is_array($value)) { return $this->assertFieldManyValues($attribute, $value, $method); } // ->assertFieldsInclude('id', 1) - if ( ! is_array($attribute) && ! is_null($value) ) { + if (!is_array($attribute) && !is_null($value)) { return $this->assertFieldValue($attribute, $value, $method); } // ->assertFieldsInclude(['id', 'email']) - if ( is_array($attribute) && is_numeric(array_keys($attribute)[ 0 ]) ) { + if (is_array($attribute) && is_numeric(array_keys($attribute)[0])) { return $this->assertFieldKeys($attribute, $method); } // ->assertFieldsInclude(['id' => 1]) - if ( is_array($attribute) ) { + if (is_array($attribute)) { return $this->assertFieldKeyValue($attribute, $method); } @@ -80,12 +90,34 @@ public function assertFieldAttribute($attribute, $method) 'attribute' => $attribute ]); } + public function assertFieldOptions($attribute, $options,$method) + { + $formattedOptions = collect($options)->map(function ($value, $label) { + return [ + 'label' => $label, + 'value' => $value, + ]; + })->values()->all(); + + return $this->$method([ + 'attribute' => $attribute, + 'options' => $formattedOptions + ]); + } + + public function assertFieldRequired($attribute, $required, $method) + { + return $this->$method([ + 'attribute' => $attribute, + 'required' => $required + ]); + } public function assertFieldValue($attribute, $value, $method) { return $this->$method([ 'attribute' => $attribute, - 'value' => $value, + 'value' => $value, ]); } @@ -115,18 +147,4 @@ public function assertFieldKeyValue($attribute, $method) return $this; } - - /** - * @return string - */ - protected function getFieldsPath() - { - if ( isset($this->original[ 'resources' ][ 0 ][ 'fields' ]) ) { - return 'resources.*.fields'; - } elseif ( isset($this->original[ 'resource' ][ 'fields' ]) ) { - return 'resource.fields'; - } - - return 'fields'; - } } diff --git a/src/Assert/AssertResources.php b/src/Assert/AssertResources.php index c70e95c..0b1616d 100644 --- a/src/Assert/AssertResources.php +++ b/src/Assert/AssertResources.php @@ -24,4 +24,13 @@ public function assertResources(closure $callable) return $this; } + + + public function assertTotal($amount){ + $total = collect(json_decode(json_encode(Arr::get($this->original, 'total', []), true))); + + PHPUnit::assertEquals($amount, (int) $total[0]); + + return $this; + } } diff --git a/src/NovaAssertions.php b/src/NovaAssertions.php index ae3c1c5..8efadf9 100644 --- a/src/NovaAssertions.php +++ b/src/NovaAssertions.php @@ -8,12 +8,24 @@ trait NovaAssertions { - public function novaIndex($resource, $filters = []) + public function novaIndex($resource, $filters = [], $search = []) { $resource = $this->resolveUriKey($resource); - $filters = $this->makeNovaFilters($resource, $filters); $endpoint = "nova-api/$resource"; - $json = $this->getJson("$endpoint?$filters"); + + if (!empty($filters) && !empty($search)) { + $filterQuery = $this->makeNovaFilters($resource, $filters); + $searchQuery = http_build_query($search); + $queryString = "$filterQuery&$searchQuery"; + } elseif (!empty($filters)) { + $queryString = $this->makeNovaFilters($resource, $filters); + } elseif (!empty($search)) { + $queryString = http_build_query($search); + } else { + $queryString = ''; + } + + $json = $this->getJson("$endpoint?$queryString"); return new NovaResponse($json, compact('endpoint', 'resource'), $this); } @@ -56,6 +68,22 @@ public function novaLens($resource, $lens, $filters = []) return new NovaResponse($json, compact('endpoint', 'resource', 'lens'), $this); } + public function novaStore($resource, $data){ + $resource = $this->resolveUriKey($resource); + $endpoint = "/nova-api/$resource?editMode=create&editing=true"; + $response = $this->post($endpoint, $data); + + return new NovaResponse($response, compact('endpoint', 'resource'), $this); + } + + public function novaUpdate($resource, $data, $resourceId){ + $resource = $this->resolveUriKey($resource); + $endpoint = "/nova-api/$resource/$resourceId?editMode=update&editing=true"; + $response = $this->put($endpoint, $data); + + return new NovaResponse($response, compact('endpoint', 'resource'), $this); + } + public function makeNovaFilters($resource, $filters) { if (empty($filters)) { @@ -64,7 +92,7 @@ public function makeNovaFilters($resource, $filters) $encoded = base64_encode(json_encode( collect($filters)->map(function ($value, $key) { - return ['class' => $key, 'value' => $value]; + return [$key => $value]; })->values() )); From a1c167f46c9d7ce967858b688f15d95273f28c48 Mon Sep 17 00:00:00 2001 From: imkdenis Date: Fri, 18 Aug 2023 12:32:45 +0200 Subject: [PATCH 4/6] feat: novaStore and novaUpdate methods, some fixes and enhancements --- src/Assert/AssertResources.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Assert/AssertResources.php b/src/Assert/AssertResources.php index 0b1616d..fcc0c4d 100644 --- a/src/Assert/AssertResources.php +++ b/src/Assert/AssertResources.php @@ -26,7 +26,7 @@ public function assertResources(closure $callable) } - public function assertTotal($amount){ + public function assertTotalData($amount){ $total = collect(json_decode(json_encode(Arr::get($this->original, 'total', []), true))); PHPUnit::assertEquals($amount, (int) $total[0]); From 42554b5e5e18f8a42722c278a4f7b460387931fa Mon Sep 17 00:00:00 2001 From: Marton Baranyai Date: Wed, 23 Aug 2023 01:18:30 +0100 Subject: [PATCH 5/6] put back fix that was overwritten --- src/Assert/AssertFields.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Assert/AssertFields.php b/src/Assert/AssertFields.php index 743fdc3..284dbb2 100644 --- a/src/Assert/AssertFields.php +++ b/src/Assert/AssertFields.php @@ -10,7 +10,7 @@ trait AssertFields { public function assertFieldCount($amount) { - $path = isset($this->original['resources']) ? 'resources.0.fields' : 'resource.fields'; + $path = isset($this->original['resources']) ? 'resources.0.fields' : $this->getFieldsPath(); $this->assertJsonCount($amount, $path); @@ -147,4 +147,18 @@ public function assertFieldKeyValue($attribute, $method) return $this; } + + /** + * @return string + */ + protected function getFieldsPath() + { + if ( isset($this->original[ 'resources' ][ 0 ][ 'fields' ]) ) { + return 'resources.*.fields'; + } elseif ( isset($this->original[ 'resource' ][ 'fields' ]) ) { + return 'resource.fields'; + } + + return 'fields'; + } } From 9d8bf92c14d056102cb5c440fc1cdb6d59bb4023 Mon Sep 17 00:00:00 2001 From: imkdenis Date: Fri, 15 Sep 2023 12:49:37 +0200 Subject: [PATCH 6/6] feat: novaAction method for action handler tests --- src/NovaAssertions.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/NovaAssertions.php b/src/NovaAssertions.php index 8efadf9..5c870fc 100644 --- a/src/NovaAssertions.php +++ b/src/NovaAssertions.php @@ -76,6 +76,14 @@ public function novaStore($resource, $data){ return new NovaResponse($response, compact('endpoint', 'resource'), $this); } + public function novaAction($resource, $action, $data){ + $resource = $this->resolveUriKey($resource); + $endpoint = "/nova-api/$resource/action?action=$action"; + $response = $this->post($endpoint, $data); + + return new NovaResponse($response, compact('endpoint', 'resource'), $this); + } + public function novaUpdate($resource, $data, $resourceId){ $resource = $this->resolveUriKey($resource); $endpoint = "/nova-api/$resource/$resourceId?editMode=update&editing=true";