Skip to content

Commit

Permalink
#111. Add 100% coverage on BitMaskTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Jun 10, 2018
1 parent ba161bb commit 9a89c3d
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/extension/BitMaskTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function setFlagsIndex(Collection $data)
$flags = $this->bitMask->getFlags();
$mask = $v[$field];
foreach($flags as $flag => $fVal) {
$v[$flag] = ($fVal & $mask) ? true : false;
$v[$flag] = (bool)($fVal & $mask);
}
}
return $v;
Expand Down Expand Up @@ -81,7 +81,7 @@ protected function setFlagsCreate()
$flags = $this->bitMask->getFlags();
$mask = $this->model->$field;
foreach($flags as $flag => $fVal) {
$this->model->$flag = ($fVal & $mask) ? true : false;
$this->model->$flag = (bool)($fVal & $mask);
}
}
return $this->model;
Expand Down Expand Up @@ -115,14 +115,14 @@ protected function setMaskUpdate(&$model, array $jsonProps)
* @param $model
* @throws \rjapi\exceptions\AttributesException
*/
protected function setFlagsUpdate(&$model)
protected function setFlagsUpdate(&$model) : void
{
$field = $this->bitMask->getField();
if(isset($model[$field])) {
$flags = $this->bitMask->getFlags();
$mask = $model[$field];
foreach($flags as $flag => $fVal) {
$model[$flag] = ($fVal & $mask) ? true : false;
$model[$flag] = (bool)($fVal & $mask);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/_data/UserFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public static function createAndGet() : User
*/
public static function delete($id) : void
{
User::where('id', $id)->first(['*'])->delete();
User::destroy($id);
}
}
127 changes: 127 additions & 0 deletions tests/unit/extension/BitMaskTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace rjapitest\unit\extensions;

use Illuminate\Database\Eloquent\Collection;
use rjapi\extension\BitMask;
use rjapi\extension\BitMaskTrait;
use rjapitest\_data\UserFixture;
use rjapitest\unit\TestCase;

/**
* Class BitMaskTest
* @package rjapitest\unit\extensions
*
* @property BitMask bitMask
*/
class BitMaskTraitTest extends TestCase
{
use BitMaskTrait;

private const FIELD = 'permissions';
private $bitMask;
private $users;
private $user;
private $model;

public function setUp()
{
parent::setUp();
$this->bitMask = new BitMask([
self::FIELD => [
'enabled' => true,
'flags' => [
'publisher' => 1,
'editor' => 2,
'manager' => 4,
'photo_reporter' => 8,
'admin' => 16,
],
],
]);
$this->users[] = UserFixture::createAndGet();
$this->user = UserFixture::createAndGet();
$this->model = $this->user;
}

/**
* @test
* @throws \rjapi\exceptions\AttributesException
*/
public function it_sets_flags_index()
{
$maskData = $this->setFlagsIndex(new Collection($this->users));
$this->assertInstanceOf(Collection::class, $maskData);
$this->assertEquals(1, $maskData[0]['editor']);
}

/**
* @test
* @throws \rjapi\exceptions\AttributesException
*/
public function it_sets_flags_view()
{
$maskData = $this->setFlagsView($this->user);
$this->assertEquals(1, $maskData['editor']);
}

/**
* @test
* @throws \rjapi\exceptions\AttributesException
*/
public function it_sets_flags_create()
{
$maskData = $this->setFlagsCreate();
$this->assertEquals(1, $maskData['editor']);
}

/**
* @test
* @throws \rjapi\exceptions\AttributesException
*/
public function it_sets_flags_update()
{
$this->setFlagsUpdate($this->model);
$this->assertEquals(1, $this->model['editor']);
}

/**
* @test
* @throws \rjapi\exceptions\AttributesException
*/
public function it_sets_mask_create()
{
$this->setMaskCreate([
'publisher' => 0,
'editor' => 0,
'manager' => 0,
'photo_reporter' => 0,
'admin' => 1,
]);
$this->assertEquals(16, $this->model->permissions);
}

/**
* @test
* @throws \rjapi\exceptions\AttributesException
*/
public function it_sets_mask_update()
{
$this->setMaskUpdate($this->model,
[
'publisher' => 0,
'editor' => 0,
'manager' => 0,
'photo_reporter' => 0,
'admin' => 1,
]);
$this->assertEquals(16, $this->model->permissions);
}

public function tearDown()
{
UserFixture::delete($this->user->id);
UserFixture::delete($this->users[0]->id);
parent::tearDown();
}
}

0 comments on commit 9a89c3d

Please sign in to comment.