8000 [Tests] Add non-eloquent tests · martynling/laravel-json-api@14f1d43 · GitHub
[go: up one dir, main page]

Skip to content

Commit 14f1d43

Browse files
committed
[Tests] Add non-eloquent tests
1 parent 77f0163 commit 14f1d43

File tree

11 files changed

+611
-10
lines changed

11 files changed

+611
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. This projec
44

55
## Unreleased
66

7+
### Changed
8+
- Tests helpers are no longer in the Browser Kit testing style, and instead use a `TestResponse` class that extends
9+
the standard Laravel test response.
10+
- The `InteractsWithResources` test helper trait has been merged into `MakesJsonApiRequests`.
11+
712
### Fixed
813
- [#66] Content negotiation no longer sends a `415` response if a request does not have body content.
914

tests/Entities/Site.php

Lines changed: 142 additions & 0 deletions
B41A
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace CloudCreativity\LaravelJsonApi\Tests\Entities;
4+
5+
use InvalidArgumentException;
6+
7+
class Site
8+
{
9+
10+
/**
11+
* @var string
12+
*/
13+
private $slug;
14+
15+
/**
16+
* @var string|null
17+
*/
18+
private $domain;
19+
20+
/**
21+
* @var string|null
22+
*/
23+
private $name;
24+
25+
/**
26+
* @param string $slug
27+
* @param array $values
28+
* @return Site
29+
*/
30+
public static function create($slug, array $values)
31+
{
32+
$site = new self($slug);
33+
$site->exchangeArray($values);
34+
35+
return $site;
36+
}
37+
38+
/**
39+
* Site constructor.
40+
*
41+
* @param string $slug
42+
*/
43+
public function __construct($slug)
44+
{
45+
if (empty($slug)) {
46+
throw new InvalidArgumentException('Expecting a non-empty slug');
47+
}
48+
49+
$this->slug = $slug;
50+
}
51+
52+
/**
53+
* @return string
54+
*/
55+
public function getSlug()
56+
{
57+
return $this->slug;
58+
}
59+
60+
/**
61+
* @param mixed $domain
62+
* @return $this
63+
*/
64+
public function setDomain($domain)
65+
{
66+
$this->domain = $domain;
67+
68+
return $this;
69+
}
70+
71+
/**
72+
* @return string
73+
*/
74+
public function getDomain()
75+
{
76+
return (string) $this->domain;
77+
}
78+
79+
/**
80+
* @param string $name
81+
* @return $this
82+
*/
83+
public function setName($name)
84+
{
85+
$this->name = $name;
86+
87+
return $this;
88+
}
89+
90+
/**
91+
* @return string
92+
*/
93+
public function getName()
94+
{
95+
return (string) $this->name;
96+
}
97+
98+
/**
99+
* @param array $values
100+
* @return $this
101+
*/
102+
public function exchangeArray(array $values)
103+
{
104+
if ($domain = array_get($values, 'domain')) {
105+
$this->setDomain($domain);
106+
}
107+
108+
if ($name = array_get($values, 'name')) {
109+
$this->setName($name);
110+
}
111+
112+
return $this;
113+
}
114+
115+
/**
116+
* @return array
117+
*/
118+
public function toArray()
119+
{
120+
return [
121+
'domain' => $this->getDomain(),
122+
'name' => $this->getName(),
123+
];
124+
}
125+
126+
/**
127+
* @return void
128+
*/
129+
public function save()
130+
{
131+
app(SiteRepository::class)->store($this);
132+
}
133+
134+
/**
135+
* @return void
136+
*/
137+
public function delete()
138+
{
139+
app(SiteRepository::class)->remove($this);
140+
}
141+
142+
}

tests/Entities/SiteRepository.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace 10000 CloudCreativity\LaravelJsonApi\Tests\Entities;
4+
5+
use Generator;
6+
use IteratorAggregate;
7+
8+
class SiteRepository implements IteratorAggregate
9+
{
10+
11+
/**
12+
* @var array
13+
*/
14+
private $sites = [];
15+
16+
/**
17+
* @param $slug
18+
* @return Site|null
19+
*/
20+
public function find($slug)
21+
{
22+
if (!isset($this->sites[$slug])) {
23+
return null;
24+
}
25+
26+
return Site::create($slug, $this->sites[$slug]);
27+
}
28+
29+
/**
30+
* @return array
31+
*/
32+
public function findAll()
33+
{
34+
return iterator_to_array($this->all());
35+
}
36+
37+
/**
38+
* @param Site $site
39+
* @return void
40+
*/
41+
public function store(Site $site)
42+
{
43+
$this->sites[$site->getSlug()] = $site->toArray();
44+
}
45+
46+
/**
47+
* @param Site|string $site
48+
* @return void
49+
*/
50+
public function remove($site)
51+
{
52+
$slug = ($site instanceof Site) ? $site->getSlug() : $site;
53+
54+
unset($this->sites[$slug]);
55+
}
56+
57+
/**
58+
* @return array
59+
*/
60+
public function all()
61+
{
62+
return iterator_to_array($this);
63+
}
64+
65+
/**
66+
* @return Generator
67+
*/
68+
public function getIterator()
69+
{
70+
foreach ($this->sites as $slug => $values) {
71+
yield $slug => Site::create($slug, $values);
72+
}
73+
}
74+
75+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace CloudCreativity\LaravelJsonApi\Tests\Http\Controllers;
4+
5+
use CloudCreativity\JsonApi\Contracts\Http\ApiInterface;
6+
use CloudCreativity\JsonApi\Contracts\Http\Requests\RequestInterface as JsonApiRequest;
7+
use CloudCreativity\LaravelJsonApi\Http\Responses\ReplyTrait;
8+
use CloudCreativity\LaravelJsonApi\Tests\Entities\Site;
9+
use CloudCreativity\LaravelJsonApi\Tests\JsonApi\Sites;
10+
use Illuminate\Routing\Controller;
11+
12+
class SitesController extends Controller
13+
{
14+
15+
use ReplyTrait;
16< F438 span class="diff-text-marker">+
17+
/**
18+
* @var Sites\Hydrator
19+
*/
20+
private $hydrator;
21+
22+
/**
23+
* SitesController constructor.
24+
*
25+
* @param Sites\Hydrator $hydrator
26+
*/
27+
public function __construct(Sites\Hydrator $hydrator)
28+
{
29+
$this->hydrator = $hydrator;
30+
}
31+
32+
/**
33+
* @param ApiInterface $api
34+
* @param JsonApiRequest $request
35+
* @return mixed
36+
*/
37+
public function index(ApiInterface $api, JsonApiRequest $request)
38+
{
39+
$store = $api->getStore();
40+
41+
return $this->reply()->content($store->query(
42+
$request->getResourceType(),
43+
$request->getParameters()
44+
));
45+
}
46+
47+
/**
48+
* @param JsonApiRequest $request
49+
* @return mixed
50+
*/
51+
public function create(JsonApiRequest $request)
52+
{
53+
$resource = $request->getDocument()->getResource();
54+
$record = new Site($resource->getId()); // client generated id.
55+
$this->hydrator->hydrate($request->getDocument()->getResource(), $record);
56+
$record->save();
57+
58+
return $this->reply()->created($record);
59+
}
60+
61+
/**
62+
* @param JsonApiRequest $request
63+
* @return mixed
64+
*/
65+
public function read(JsonApiRequest $request)
66+
{
67+
return $this->reply()->content($request->getRecord());
68+
}
69+
70+
/**
71+
* @param JsonApiRequest $request
72+
* @return mixed
73+
*/
74+
public function update(JsonApiRequest $request)
75+
{
76+
/** @var Site $record */
77+
$record = $request->getRecord();
78+
$resource = $request->getDocument()->getResource();
79+
$this->hydrator->hydrate($resource, $record);
80+
$record->save();
81+
82+
return $this->reply()->content($record);
83+
}
84+
85+
/**
86+
* @param JsonApiRequest $request
87+
* @return mixed
88+
*/
89+
public function delete(JsonApiRequest $request)
90+
{
91+
/** @var Site $record */
92+
$record = $request->getRecord();
93+
$record->delete();
94+
95+
return $this->reply()->noContent();
96+
}
97+
}

tests/Integration/Eloquent/TestCase.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,4 @@ protected function setUp()
3232
$api->resource('users');
3333
});
3434
}
35-
36-
/**
37-
* Return the prefix for route names for the resources we're testing...
38-
*
39-
* @return string
40-
*/
41-
protected function getRoutePrefix()
42-
{
43-
return 'api-v1::';
44-
}
4535
}

0 commit comments

Comments
 (0)
0