Skip to content

Commit

Permalink
#111. Add more coverage on JwtTrait/FsmTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Jun 10, 2018
1 parent 69167be commit 2e75bba
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/extension/FsmTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace rjapi\extension;


Expand All @@ -21,15 +22,14 @@ private function checkFsmCreate(array &$jsonProps) : void
{
$stateMachine = new StateMachine($this->entity);
$stateField = $stateMachine->getField();
if(empty($jsonProps[$stateField])) {
if (empty($jsonProps[$stateField])) {
$stateMachine->setInitial($stateField);
$jsonProps[$stateField] = $stateMachine->getInitial();
}
else {
foreach($jsonProps as $k => $v) {
if($stateMachine->isStatedField($k) === true) {
} else {
foreach ($jsonProps as $k => $v) {
if ($stateMachine->isStatedField($k) === true) {
$stateMachine->setStates($k);
if($stateMachine->isInitial($v) === false) {
if ($stateMachine->isInitial($v) === false) {
// the field is under state machine rules and it is not initial state
Json::outputErrors(
[
Expand All @@ -56,7 +56,7 @@ private function checkFsmUpdate(array $jsonProps, $model) : void
$stateMachine = new StateMachine($this->entity);
$field = $stateMachine->getField();
$stateMachine->setStates($field);
if(empty($jsonProps[$field]) === false
if (empty($jsonProps[$field]) === false
&& $stateMachine->isStatedField($field) === true
&& $stateMachine->isTransitive($model->$field, $jsonProps[$field]) === false
) {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function outputErrors(array $errors, bool $return = false)
}
// errors and codes must be clear with readable json
$encoded = self::encode($arr, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
if (false === $return) {
if (false === $return && env('APP_ENV') !== 'dev') {
echo $encoded;
exit(JSONApiInterface::EXIT_STATUS_ERROR);
}
Expand Down
38 changes: 38 additions & 0 deletions tests/_data/ArticleFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace rjapitest\_data;


use Modules\V2\Entities\Article;

class ArticleFixture
{
/**
* @return Article
*/
public static function createAndGet() : Article
{
$article = new Article();
$article->id = '124ea1e595ed11225727e7730d653669';
$article->title = 'Foo bar Foo bar Foo bar Foo bar';
$article->description = 'The quick brovn fox jumped ower the lazy dogg';
$article->url = 'http://example.com/articles_feed_123';
$article->topic_id = 1;
$article->rate = 5.0;
$article->status = 'draft';
$article->show_in_top = 1;
$article->date_posted = date('Y-m-d');
$article->time_to_live = date('H:i:s');
$article->save();

return $article;
}

/**
* @param $id
*/
public static function delete($id) : void
{
Article::where('id', $id)->first(['*'])->delete();
}
}
31 changes: 31 additions & 0 deletions tests/_data/UserFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace rjapitest\_data;

use Modules\V2\Entities\User;

class UserFixture
{
/**
* @return User
*/
public static function createAndGet() : User
{
$user = new User();
$user->first_name = 'Linus';
$user->last_name = 'Gates';
$user->password = 'secret';
$user->jwt = 'jwt';
$user->permissions = 2;
$user->save();

return $user;
}

/**
* @param $id
*/
public static function delete($id) : void
{
User::where('id', $id)->first(['*'])->delete();
}
}
71 changes: 71 additions & 0 deletions tests/unit/extension/JwtTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace rjapitest\unit\extensions;

use rjapi\extension\BaseModel;
use rjapi\extension\JWTTrait;
use rjapi\types\ModelsInterface;
use rjapitest\_data\UserFixture;
use rjapitest\unit\TestCase;

class User extends BaseModel
{
// >>>props>>>
protected $primaryKey = 'id';
protected $table = 'user';
public $timestamps = false;
// <<<props<<<
// >>>methods>>>

// <<<methods<<<
}

class JwtTraitTest extends TestCase
{
use JWTTrait;

private $model;

public function setUp()
{
parent::setUp();
$this->model = new User();
$this->model->id = 1;
$this->model->password = 'secret';
$_SERVER['HTTP_HOST'] = 'example.com';
}

/**
* @uses getEntity
* @test
*/
public function it_creates_jwt_user()
{
$this->createJwtUser();
$this->assertEmpty($this->model->password);
// 2nd call with empty password to emulate error
$this->createJwtUser();
}

/**
* @test
*/
public function it_updates_jwt_user()
{
$user = $this->getEntity(1); // fake id
$this->assertInstanceOf(BaseModel::class, $user);
$this->updateJwtUser($user, ['password' => 'secret']);
UserFixture::delete($user->id);
}

/**
* Params needed for internal calls
* @param $id
* @param array $data
* @return \Modules\V2\Entities\User
*/
private function getEntity($id, array $data = ModelsInterface::DEFAULT_DATA)
{
return UserFixture::createAndGet();
}
}
1 change: 1 addition & 0 deletions tests/unit/extension/SpellCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function setUp()
public function it_checks_text()
{
$this->assertEmpty($this->spellCheck->check('It checks the text correctness.'));
$this->assertArraySubset(['correctnesss'], $this->spellCheck->check('It checks the text correctnesss.'));
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/extension/StateMachineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace rjapitest\unit\extensions;

use rjapi\exceptions\AttributesException;
use rjapi\extension\FsmTrait;
use rjapi\extension\StateMachine;
use rjapitest\_data\ArticleFixture;
use rjapitest\unit\TestCase;

/**
Expand All @@ -14,11 +16,15 @@
*/
class StateMachineTest extends TestCase
{
use FsmTrait;

private $stateMachine;
private $entity;

public function setUp()
{
parent::setUp();
$this->entity = 'article';
$this->stateMachine = new StateMachine('article');
try {
$this->stateMachine->setStates('status');
Expand Down Expand Up @@ -63,4 +69,40 @@ public function it_gets_field()
{
$this->assertEquals('status', $this->stateMachine->getField());
}

/**
* @test
* @throws AttributesException
*/
public function it_checks_fsm_create()
{
$jsonProps = [];
$this->checkFsmCreate($jsonProps);
$this->assertArraySubset(['status' => 'draft'], $jsonProps);
$jsonProps = [
'status' => 'published'
];
$this->checkFsmCreate($jsonProps);
}

/**
* @test
* @throws AttributesException
*/
public function it_checks_fsm_update()
{
$model = ArticleFixture::createAndGet();
$jsonProps = [
'status' => 'published'
];
$this->checkFsmUpdate($jsonProps, $model);
// simulate err - non-transitive state
$jsonProps = [
'status' => 'archived'
];
$this->checkFsmUpdate($jsonProps, $model);
$this->stateMachine->setInitial('status');
$this->assertEquals($model->status, $this->stateMachine->getInitial());
ArticleFixture::delete($model->id);
}
}
2 changes: 2 additions & 0 deletions tests/unit/helpers/JwtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Token;
use rjapi\extension\BaseJwt;
use rjapi\extension\BaseModel;
use rjapi\extension\JWTTrait;
use rjapi\helpers\Jwt;
use rjapitest\unit\TestCase;

Expand Down

0 comments on commit 2e75bba

Please sign in to comment.