Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix create and edit requests did not work with assertFieldCount #21

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
44 changes: 41 additions & 3 deletions src/Assert/AssertFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}
Expand All @@ -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();
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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';
}
}
9 changes: 9 additions & 0 deletions src/Assert/AssertResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
44 changes: 40 additions & 4 deletions src/NovaAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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()
));

Expand Down