-
Notifications
You must be signed in to change notification settings - Fork 2
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
Feature/json api output #24
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the approach to have each resource be a separate object, but I feel like the current ResourceObject
(in the commented out functions) is gaining a lot of responsibilities that feel like they belong to the renderer. Furthermore, we might not need things like a relationship object, as we already have Association
.
Let's discuss how to move forward tomorrow.
phpstan.neon
Outdated
@@ -10,3 +10,5 @@ parameters: | |||
# This class does string-building with class-strings, which is more hassle | |||
# to type-hint and annotate than it's worth. | |||
- src/Apitizer/Support/ClassFilter.php | |||
|
|||
checkMissingIterableValueType: false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not disable this, as it forces us to think about the content of our arrays/iterables, rather than having undocumented arrays with "something" in them. If the array is too complex to easily document, make it an object (or array of objects).
if ($this->isSingleRowOfData($data)) { | ||
$data = collect([$data]); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems incorrect, as it makes it impossible to return a single object from the API, while that is supported by the JSON-API spec:
Primary data MUST be either:
- a single resource object, a single resource identifier object, or null, for requests that target single resources
- an array of resource objects, an array of resource identifier objects, or an empty array ([]), for requests that target resource collections
src/Apitizer/JsonApi/Document.php
Outdated
$data = [ | ||
'data' => [] | ||
]; | ||
|
||
foreach ($this->resources as $resource) { | ||
$data['data'][] = $resource->toArray(); | ||
} | ||
|
||
return $data; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would probably simplify this to something like:
return [
'data' => collect($this->resources)->map->toArray()->all(),
];
If we add the Illuminate\Contracts\Support\Arrayable
interface to the ResourceObject
(which is already implemented with toArray
), then we can simplify it even further to:
return [
'data' => collect($this->resources)->toArray(),
];
Because the Collection class will then take care of calling toArray for us. This advice holds for all container objects we introduce by the way.
// need to do this because DB unreliable since it doesn't always delete entire DB before test | ||
$user = User::updateOrCreate(['id' => 1], [ | ||
'name' => 'Daan Hage', | ||
'email' => '[email protected]', | ||
]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason a factory wasn't used for this?
Also, the database isn't deleted because the feature tests uses DatabaseTransactions
to rollback changes, rather than completely rebuilding the database for all the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Nizari It shouldn't be unreliable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really possible when I return an ID. Need a way to keep ID the same every time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is there so much uncommented code?
Haven't tested the code yet, just looked at it.
$request = $this->request()->fields('name, email')->make(); | ||
$result = UserBuilder::make($request)->setRenderer(new JsonApiRenderer)->render($collection); | ||
|
||
$this->assertJsonStringEqualsJsonFile( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to assert on json? And maybe put it inline so we can directly see what the result should be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but assertJsonStringEqualsJsonFile looks so hot!
// need to do this because DB unreliable since it doesn't always delete entire DB before test | ||
$user = User::updateOrCreate(['id' => 1], [ | ||
'name' => 'Daan Hage', | ||
'email' => '[email protected]', | ||
]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Nizari It shouldn't be unreliable?
class JsonApiTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_renders_existing_eloquent_model_to_jsonapi_format() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest
public function we_can_use_the_json_api_renderer_to_format_the_builder_response_in_json_api_format()
Mainly because I am looking at what this PR does from the tests. Same goes for the one below.
@@ -8,6 +8,9 @@ | |||
return [ | |||
'name' => $faker->name, | |||
'email' => $faker->email, | |||
'should_reset_password' => rand(0, 1), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't use random in the factories. In another project, after using it for a while, it can lead to failures in x % of the time for certain tests.
It's probably not needed to be set here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? this value is always true or false so this should work. If a test is dependant on this value then it should be set inside the test and never be hidden inside factory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thus it can be removed from here.
*/ | ||
public static function factory(array $attributes, string $type, string $id) | ||
{ | ||
$resourceObject = new self($type, $id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the contstructor? Or factory(ResourceObject::class)->create($attributes);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
je can use the factory pattern also just for initing your object if you have some specific create logic you dont always want inside your constructor. I remember we had this discussion before ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We didn't finish that discussion
Because I'm not done :). ANd its inspiration code |
Codecov Report
@@ Coverage Diff @@
## master #24 +/- ##
============================================
- Coverage 81.94% 81.71% -0.24%
- Complexity 851 875 +24
============================================
Files 105 108 +3
Lines 1983 2040 +57
============================================
+ Hits 1625 1667 +42
- Misses 358 373 +15
Continue to review full report at Codecov.
|
@drtheuns het aantal commits is nu onduidelijk terug te zien door php cs fixer. Ik adviseer dat we die snel mergen ivm leesbaarheid. Benieuwd wat je voor de rest van me initiële opzet vindt!
@jorisatabix