Permalink
Browse files

add serializer test case

  • Loading branch information...
1 parent 7a769f5 commit 335b43c806300350b27bc48a2ad910d7a11d26d7 @tuyakhov committed Mar 22, 2016
Showing with 155 additions and 12 deletions.
  1. +13 −0 phpunit.xml.dist
  2. +2 −2 src/ResourceInterface.php
  3. +5 −7 src/ResourceTrait.php
  4. +1 −3 src/Serializer.php
  5. +33 −0 tests/SerializerTest.php
  6. +59 −0 tests/TestCase.php
  7. +11 −0 tests/bootstrap.php
  8. +31 −0 tests/data/ResourceModel.php
View
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<phpunit bootstrap="./tests/bootstrap.php"
+ colors="true"
+ convertErrorsToExceptions="true"
+ convertNoticesToExceptions="true"
+ convertWarningsToExceptions="true"
+ stopOnFailure="false">
+ <testsuites>
+ <testsuite name="Yii2tech Test Suite">
+ <directory>./tests</directory>
+ </testsuite>
+ </testsuites>
+</phpunit>
@@ -8,9 +8,9 @@
interface ResourceInterface extends ResourceIdentifierInterface
{
- public function getAttributes(array $fields = []);
+ public function getResourceAttributes(array $fields = []);
- public function getRelationships();
+ public function getResourceRelationships();
public function getLinks();
View
@@ -1,8 +1,6 @@
<?php
/**
- * @link http://www.stombox.com/
- * @copyright Copyright (c) 2015 Stombox LLC
- * @license http://www.stombox.com/license/
+ * @author Anton Tuyakhov <atuyakhov@gmail.com>
*/
namespace tuyakhov\jsonapi;
@@ -40,7 +38,7 @@ public function getType()
* @param array $fields
* @return array
*/
- public function getAttributes(array $fields = [])
+ public function getResourceAttributes(array $fields = [])
{
$attributes = [];
@@ -53,7 +51,7 @@ public function getAttributes(array $fields = [])
/**
* @return array
*/
- public function getRelationships()
+ public function getResourceRelationships()
{
$relationships = [];
@@ -86,8 +84,8 @@ public function toArray(array $fields = [], array $expand = [], $recursive = tru
'id' => $this->getId(),
'type' => $this->getType(),
];
- $attributes = $this->getAttributes($fields);
- $relationships = $this->getRelationships();
+ $attributes = $this->getResourceAttributes($fields);
+ $relationships = $this->getResourceRelationships();
if (!empty($attributes)) {
$data['attributes'] = $attributes;
}
View
@@ -1,8 +1,6 @@
<?php
/**
- * @link http://www.stombox.com/
- * @copyright Copyright (c) 2015 Stombox LLC
- * @license http://www.stombox.com/license/
+ * @author Anton Tuyakhov <atuyakhov@gmail.com>
*/
namespace tuyakhov\jsonapi;
@@ -0,0 +1,33 @@
+<?php
+/**
+ * @author Anton Tuyakhov <atuyakhov@gmail.com>
+ */
+namespace tuyakhov\jsonapi\tests;
+
+use tuyakhov\jsonapi\tests\data\ResourceModel;
+use tuyakhov\jsonapi\Serializer;
+
+class SerializerTest extends TestCase
+{
+ public function testSerializeModel()
+ {
+ $serializer = new Serializer();
+ $model = new ResourceModel();
+ $result = $serializer->serializeModel($model);
+ $this->assertArrayHasKey('data', $result);
+ $serializedModel = [
+ 'type' => 'resource-models',
+ 'attributes' => [
+ 'testAttribute' => 'testAttribute'
+ ],
+ 'relationships' => [
+ 'testRelation' => [
+ 'data' => [
+ 'type' => 'resource-models',
+ ]
+ ]
+ ]
+ ];
+ $this->assertArraySubset($serializedModel, $result['data']);
+ }
+}
View
@@ -0,0 +1,59 @@
+<?php
+/**
+ * @author Anton Tuyakhov <atuyakhov@gmail.com>
+ */
+namespace tuyakhov\jsonapi\tests;
+
+use \yii\helpers\ArrayHelper;
+
+class TestCase extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ parent::setUp();
+ $this->mockApplication();
+ }
+
+ protected function tearDown()
+ {
+ $this->destroyApplication();
+ }
+ /**
+ * Populates Yii::$app with a new application
+ * The application will be destroyed on tearDown() automatically.
+ * @param array $config The application configuration, if needed
+ * @param string $appClass name of the application class to create
+ */
+ protected function mockApplication($config = [], $appClass = '\yii\web\Application')
+ {
+ new $appClass(ArrayHelper::merge([
+ 'id' => 'testapp',
+ 'basePath' => __DIR__,
+ 'components' => [
+ 'request' => [
+ 'parsers' => [
+ 'application/vnd.api+json' => '\tuyakhov\jsonapi\JsonApiParser'
+ ],
+ 'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
+ 'scriptFile' => __DIR__ .'/index.php',
+ 'scriptUrl' => '/index.php',
+ ]
+ ],
+ 'vendorPath' => $this->getVendorPath(),
+ ], $config));
+ }
+ /**
+ * @return string vendor path
+ */
+ protected function getVendorPath()
+ {
+ return dirname(dirname(__DIR__)) . '/vendor';
+ }
+ /**
+ * Destroys application in Yii::$app by setting it to null.
+ */
+ protected function destroyApplication()
+ {
+ \Yii::$app = null;
+ }
+}
View
@@ -0,0 +1,11 @@
+<?php
+// ensure we get report on all possible php errors
+error_reporting(-1);
+define('YII_ENABLE_ERROR_HANDLER', false);
+define('YII_DEBUG', true);
+$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
+$_SERVER['SCRIPT_FILENAME'] = __FILE__;
+require_once(__DIR__ . '/../vendor/autoload.php');
+require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
+Yii::setAlias('@tuyakhov/tests', __DIR__);
+Yii::setAlias('@tuyakhov/jsonapi', dirname(__DIR__));
@@ -0,0 +1,31 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: admin
+ * Date: 3/22/16
+ * Time: 10:39 AM
+ */
+
+namespace tuyakhov\jsonapi\tests\data;
+
+
+use tuyakhov\jsonapi\ResourceIdentifierInterface;
+use tuyakhov\jsonapi\ResourceTrait;
+use yii\base\Model;
+
+class ResourceModel extends Model implements ResourceIdentifierInterface
+{
+ use ResourceTrait;
+
+ public $testAttribute = 'testAttribute';
+
+ public function getTestRelation()
+ {
+ return new self;
+ }
+
+ public function extraFields()
+ {
+ return ['testRelation'];
+ }
+}

0 comments on commit 335b43c

Please sign in to comment.