diff --git a/src/Assert/AssertFields.php b/src/Assert/AssertFields.php index b28cf3c..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); @@ -29,7 +29,9 @@ public function assertFields(closure $callable) $fields = collect(json_decode(json_encode(data_get($this->original, $path, []), true))); - PHPUnit::assertTrue($callable($fields)); + $isCallingOk = $callable($fields) ? true : false; + + PHPUnit::assertTrue($isCallingOk); return $this; } @@ -44,7 +46,7 @@ public function assertFieldsExclude($attribute, $value=null) return $this->fieldCheck($attribute, $value, 'assertJsonMissing'); } - protected function fieldCheck($attribute, $value, $method = null) + protected function fieldCheck($attribute, $value = null, $method) { if ($attribute instanceof Collection) { $attribute = $attribute->toArray(); @@ -88,6 +90,28 @@ 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) { @@ -123,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'; + } } diff --git a/src/Assert/AssertResources.php b/src/Assert/AssertResources.php index c70e95c..fcc0c4d 100644 --- a/src/Assert/AssertResources.php +++ b/src/Assert/AssertResources.php @@ -24,4 +24,13 @@ public function assertResources(closure $callable) return $this; } + + + public function assertTotalData($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..5c870fc 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,30 @@ 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 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"; + $response = $this->put($endpoint, $data); + + return new NovaResponse($response, compact('endpoint', 'resource'), $this); + } + public function makeNovaFilters($resource, $filters) { if (empty($filters)) { @@ -64,7 +100,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() ));