Skip to content

Commit 335b43c

Browse files
committed
add serializer test case
1 parent 7a769f5 commit 335b43c

File tree

8 files changed

+155
-12
lines changed

8 files changed

+155
-12
lines changed

phpunit.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit bootstrap="./tests/bootstrap.php"
3+
colors="true"
4+
convertErrorsToExceptions="true"
5+
convertNoticesToExceptions="true"
6+
convertWarningsToExceptions="true"
7+
stopOnFailure="false">
8+
<testsuites>
9+
<testsuite name="Yii2tech Test Suite">
10+
<directory>./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

src/ResourceInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
interface ResourceInterface extends ResourceIdentifierInterface
1010
{
11-
public function getAttributes(array $fields = []);
11+
public function getResourceAttributes(array $fields = []);
1212

13-
public function getRelationships();
13+
public function getResourceRelationships();
1414

1515
public function getLinks();
1616

src/ResourceTrait.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22
/**
3-
* @link http://www.stombox.com/
4-
* @copyright Copyright (c) 2015 Stombox LLC
5-
* @license http://www.stombox.com/license/
3+
* @author Anton Tuyakhov <atuyakhov@gmail.com>
64
*/
75

86
namespace tuyakhov\jsonapi;
@@ -40,7 +38,7 @@ public function getType()
4038
* @param array $fields
4139
* @return array
4240
*/
43-
public function getAttributes(array $fields = [])
41+
public function getResourceAttributes(array $fields = [])
4442
{
4543
$attributes = [];
4644

@@ -53,7 +51,7 @@ public function getAttributes(array $fields = [])
5351
/**
5452
* @return array
5553
*/
56-
public function getRelationships()
54+
public function getResourceRelationships()
5755
{
5856
$relationships = [];
5957

@@ -86,8 +84,8 @@ public function toArray(array $fields = [], array $expand = [], $recursive = tru
8684
'id' => $this->getId(),
8785
'type' => $this->getType(),
8886
];
89-
$attributes = $this->getAttributes($fields);
90-
$relationships = $this->getRelationships();
87+
$attributes = $this->getResourceAttributes($fields);
88+
$relationships = $this->getResourceRelationships();
9189
if (!empty($attributes)) {
9290
$data['attributes'] = $attributes;
9391
}

src/Serializer.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22
/**
3-
* @link http://www.stombox.com/
4-
* @copyright Copyright (c) 2015 Stombox LLC
5-
* @license http://www.stombox.com/license/
3+
* @author Anton Tuyakhov <atuyakhov@gmail.com>
64
*/
75

86
namespace tuyakhov\jsonapi;

tests/SerializerTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* @author Anton Tuyakhov <atuyakhov@gmail.com>
4+
*/
5+
namespace tuyakhov\jsonapi\tests;
6+
7+
use tuyakhov\jsonapi\tests\data\ResourceModel;
8+
use tuyakhov\jsonapi\Serializer;
9+
10+
class SerializerTest extends TestCase
11+
{
12+
public function testSerializeModel()
13+
{
14+
$serializer = new Serializer();
15+
$model = new ResourceModel();
16+
$result = $serializer->serializeModel($model);
17+
$this->assertArrayHasKey('data', $result);
18+
$serializedModel = [
19+
'type' => 'resource-models',
20+
'attributes' => [
21+
'testAttribute' => 'testAttribute'
22+
],
23+
'relationships' => [
24+
'testRelation' => [
25+
'data' => [
26+
'type' => 'resource-models',
27+
]
28+
]
29+
]
30+
];
31+
$this->assertArraySubset($serializedModel, $result['data']);
32+
}
33+
}

tests/TestCase.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* @author Anton Tuyakhov <atuyakhov@gmail.com>
4+
*/
5+
namespace tuyakhov\jsonapi\tests;
6+
7+
use \yii\helpers\ArrayHelper;
8+
9+
class TestCase extends \PHPUnit_Framework_TestCase
10+
{
11+
protected function setUp()
12+
{
13+
parent::setUp();
14+
$this->mockApplication();
15+
}
16+
17+
protected function tearDown()
18+
{
19+
$this->destroyApplication();
20+
}
21+
/**
22+
* Populates Yii::$app with a new application
23+
* The application will be destroyed on tearDown() automatically.
24+
* @param array $config The application configuration, if needed
25+
* @param string $appClass name of the application class to create
26+
*/
27+
protected function mockApplication($config = [], $appClass = '\yii\web\Application')
28+
{
29+
new $appClass(ArrayHelper::merge([
30+
'id' => 'testapp',
31+
'basePath' => __DIR__,
32+
'components' => [
33+
'request' => [
34+
'parsers' => [
35+
'application/vnd.api+json' => '\tuyakhov\jsonapi\JsonApiParser'
36+
],
37+
'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
38+
'scriptFile' => __DIR__ .'/index.php',
39+
'scriptUrl' => '/index.php',
40+
]
41+
],
42+
'vendorPath' => $this->getVendorPath(),
43+
], $config));
44+
}
45+
/**
46+
* @return string vendor path
47+
*/
48+
protected function getVendorPath()
49+
{
50+
return dirname(dirname(__DIR__)) . '/vendor';
51+
}
52+
/**
53+
* Destroys application in Yii::$app by setting it to null.
54+
*/
55+
protected function destroyApplication()
56+
{
57+
\Yii::$app = null;
58+
}
59+
}

tests/bootstrap.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
// ensure we get report on all possible php errors
3+
error_reporting(-1);
4+
define('YII_ENABLE_ERROR_HANDLER', false);
5+
define('YII_DEBUG', true);
6+
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
7+
$_SERVER['SCRIPT_FILENAME'] = __FILE__;
8+
require_once(__DIR__ . '/../vendor/autoload.php');
9+
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
10+
Yii::setAlias('@tuyakhov/tests', __DIR__);
11+
Yii::setAlias('@tuyakhov/jsonapi', dirname(__DIR__));

tests/data/ResourceModel.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: admin
5+
* Date: 3/22/16
6+
* Time: 10:39 AM
7+
*/
8+
9+
namespace tuyakhov\jsonapi\tests\data;
10+
11+
12+
use tuyakhov\jsonapi\ResourceIdentifierInterface;
13+
use tuyakhov\jsonapi\ResourceTrait;
14+
use yii\base\Model;
15+
16+
class ResourceModel extends Model implements ResourceIdentifierInterface
17+
{
18+
use ResourceTrait;
19+
20+
public $testAttribute = 'testAttribute';
21+
22+
public function getTestRelation()
23+
{
24+
return new self;
25+
}
26+
27+
public function extraFields()
28+
{
29+
return ['testRelation'];
30+
}
31+
}

0 commit comments

Comments
 (0)