Skip to content

Commit

Permalink
#111. Add more coverage on BaseRelationsTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Jun 10, 2018
1 parent 9a89c3d commit f20bb70
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 171 deletions.
308 changes: 152 additions & 156 deletions clover.xml

Large diffs are not rendered by default.

15 changes: 0 additions & 15 deletions src/blocks/Logger.php

This file was deleted.

12 changes: 12 additions & 0 deletions src/extension/BaseRelationsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ private function savePivot(string $pivotEntity, string $lowEntity, string $entit
}

/**
* Saves model with related id from linked table full duplex
* @param string $ucEntity
* @param string $lowEntity
* @param int|string $eId
Expand All @@ -245,7 +246,18 @@ private function saveModel(string $ucEntity, string $lowEntity, $eId, $rId) : vo
{
$relEntity = Classes::getModelEntity($ucEntity);
$model = $this->getModelEntity($relEntity, $rId);
// swap table and field trying to find rels with inverse
if (!property_exists($model, $lowEntity . PhpInterface::UNDERSCORE . RamlInterface::RAML_ID)) {
$ucTmp = $ucEntity;
$ucEntity = ucfirst($lowEntity);
$relEntity = Classes::getModelEntity($ucEntity);
$model = $this->getModelEntity($relEntity, $eId);
$lowEntity = strtolower($ucTmp);

$model->{$lowEntity . PhpInterface::UNDERSCORE . RamlInterface::RAML_ID} = $rId;
$model->save();
return;
}
$model->{$lowEntity . PhpInterface::UNDERSCORE . RamlInterface::RAML_ID} = $eId;
$model->save();
}
Expand Down
27 changes: 27 additions & 0 deletions tests/_data/TagFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace rjapitest\_data;

use Modules\V2\Entities\Tag;

class TagFixture
{
/**
* @return Tag
*/
public static function createAndGet() : Tag
{
$user = new Tag();
$user->title = 'Foo Bar Baz';
$user->save();

return $user;
}

/**
* @param $id
*/
public static function delete($id) : void
{
Tag::destroy($id);
}
}
28 changes: 28 additions & 0 deletions tests/_data/TopicFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace rjapitest\_data;

use Modules\V2\Entities\Topic;

class TopicFixture
{
/**
* @return Topic
*/
public static function createAndGet() : Topic
{
$user = new Topic();
$user->title = 'Foo Bar Baz';
$user->save();

return $user;
}

/**
* @param $id
*/
public static function delete($id) : void
{
Topic::destroy($id);
}
}
57 changes: 57 additions & 0 deletions tests/unit/extension/BaseRelationsTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace rjapitest\unit\extensions;


use Modules\V2\Entities\Article;
use rjapi\extension\BaseRelationsTrait;
use rjapi\types\RamlInterface;
use rjapitest\_data\ArticleFixture;
use rjapitest\_data\TopicFixture;
use rjapitest\unit\TestCase;


class BaseRelationsTraitTest extends TestCase
{
use BaseRelationsTrait;

private $article;
private $entity;
private $topic;
private $modelEntity;

public function setUp()
{
parent::setUp();
$this->article = ArticleFixture::createAndGet();
$this->topic = TopicFixture::createAndGet();
$this->entity = 'Article';
$this->modelEntity = Article::class;
}

/**
* @test
*/
public function it_sets_relationships()
{
$this->setRelationships([
RamlInterface::RAML_DATA => [
'type' => 'article',
'id' => $this->article->id,
RamlInterface::RAML_RELATIONSHIPS => [
'topic' => [
'data' => ['type' => 'topic', 'id' => $this->topic->id]
],
],
]], $this->article->id);
$article = $this->getEntity($this->article->id);
$this->assertEquals($this->topic->id, $article->topic_id);
}

public function tearDown()
{
ArticleFixture::delete($this->article->id);
TopicFixture::delete($this->topic->id);
parent::tearDown();
}
}

0 comments on commit f20bb70

Please sign in to comment.