Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[testing] Add test for the AbstractAction class #3061

Merged
merged 5 commits into from
Apr 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions tests/Unit/Actions/AbstractActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace TCG\Voyager\Tests\Unit\Actions;

use TCG\Voyager\Actions\AbstractAction;
use TCG\Voyager\Tests\TestCase;
use TCG\Voyager\Facades\Voyager;
use TCG\Voyager\Models\User;

class AbstractActionTest extends TestCase
{
/**
* The users DataType instance.
*
* @var \TCG\Voyager\Models\DataType
*/
protected $userDataType;

/**
* A dummy user instance.
*
* @var \TCG\Voyager\Models\User
*/
protected $user;

public function setUp()
{
parent::setUp();

$this->withFactories(__DIR__.'/../../database/factories');

$this->userDataType = Voyager::model('DataType')->where('name', 'users')->first();
$this->user = factory(User::class)->create();
}

/**
* This test checks that `getRoute` method calls the `getDefaultRoute`
* method if the given key is empty.
*/
public function testGetRouteWithEmptyKey()
{
$stub = $this->getMockBuilder(AbstractAction::class)
->disableOriginalConstructor()
->setMethods(['getDefaultRoute'])
->getMockForAbstractClass();

// The `getDefaultRoute` method is called as default inside the
// `getRoute` method to retrieve the route.
$stub->expects($this->any())
->method('getDefaultRoute')
->will($this->returnValue(true));

$this->assertTrue($stub->getRoute($this->userDataType->name));
}

/**
* This test checks that `getRoute` method calls the expected method when a
* key is given.
*/
public function testGetRouteWithCustomKey()
{
$stub = $this->getMockBuilder(AbstractAction::class)
->disableOriginalConstructor()
->setMethods(['getCustomRoute'])
->getMockForAbstractClass();

// The key that's passed to the `getRoute` method will be capitalized
// and putted between 'get' and 'Route'. Calling `getRoute('custom')`
// will call the `getCustomRoute` method if it's defined.
$stub->expects($this->any())
->method('getCustomRoute')
->will($this->returnValue(true));

$this->assertTrue($stub->getRoute('custom'));
}

/**
* This test checks that `getAttributes` method will give us the expected
* output.
*/
public function testConvertAttributesToHtml()
{
$stub = $this->getMockBuilder(AbstractAction::class)
->disableOriginalConstructor()
->setMethods(['getAttributes'])
->getMockForAbstractClass();

$stub->expects($this->any())
->method('getAttributes')
->will($this->returnValue([
'class' => 'class1 class2',
'data-id' => 5,
'id' => 'delete-5',
]));

$this->assertEquals('class="class1 class2"data-id="5"id="delete-5"', $stub->convertAttributesToHtml());
}

/**
* This test checks that `shouldActionDisplayOnDataType` method returns true
* if the action should be displayed for every data type.
*/
public function testShouldActionDisplayOnDataTypeWithDefaultDataType()
{
$stub = $this->getMockBuilder(AbstractAction::class)
->setConstructorArgs([$this->userDataType, $this->user])
->getMockForAbstractClass();

$this->assertTrue($stub->shouldActionDisplayOnDataType());
}

/**
* This test checks that `shouldActionDisplayOnDataType` method returns true
* if the action should only be displayed for a specific data type.
*/
public function testTrueIsReturnedIfDataTypeMatchesTheOneWhereTheActionWasCreatedFor()
{
$stub = $this->getMockBuilder(AbstractAction::class)
->setConstructorArgs([$this->userDataType, $this->user])
->setMethods(['getDataType'])
->getMockForAbstractClass();

$stub->expects($this->any())
->method('getDataType')
->will($this->returnValue($this->userDataType->name));

$this->assertTrue($stub->shouldActionDisplayOnDataType());
}

/**
* This test checks that `shouldActionDisplayOnDataType` method returns false
* if the action should only be displayed for a specific data type.
*/
public function testFalseIsReturnedIfDataTypeDoesNotMatchesTheOneWhereTheActionWasCreatedFor()
{
$stub = $this->getMockBuilder(AbstractAction::class)
->setConstructorArgs([$this->userDataType, $this->user])
->setMethods(['getDataType'])
->getMockForAbstractClass();

$stub->expects($this->any())
->method('getDataType')
->will($this->returnValue('not users')); // different data type

$this->assertFalse($stub->shouldActionDisplayOnDataType());
}
}