Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add more unit tests
  • Loading branch information
shochdoerfer committed Mar 20, 2019
1 parent ffe33e3 commit 5a11505
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 41 deletions.
143 changes: 143 additions & 0 deletions Test/Unit/Controller/Adminhtml/Manage/DeleteUnitTest.php
@@ -0,0 +1,143 @@
<?php

/*
* This file is part of the Force Login module for Magento2.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace BitExpert\ForceCustomerLogin\Test\Unit\Controller\Adminhtml\Manage;

use BitExpert\ForceCustomerLogin\Api\Repository\WhitelistRepositoryInterface;
use BitExpert\ForceCustomerLogin\Controller\Adminhtml\Manage\Delete;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\RedirectFactory;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ViewInterface;
use Magento\Framework\HTTP\PhpEnvironment\Response;
use Magento\Framework\Message\ManagerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Class ModuleCheckUnitTest
*
* @package BitExpert\ForceCustomerLogin\Test\Unit\Controller
*/
class DeleteUnitTest extends TestCase
{
/**
* @var MockObject|WhitelistRepositoryInterface
*/
protected $whitelist;
/**
* @var MockObject|Redirect
*/
protected $redirect;
/***
* @var MockObject|RedirectFactory
*/
protected $redirectFactory;
/**
* @var MockObject|RequestInterface
*/
protected $request;
/**
* @var MockObject|ManagerInterface
*/
protected $messageManager;
/**
* @var MockObject|Context
*/
protected $context;

/**
* @inheritdoc
*/
public function setUp()
{
$this->whitelist = $this->createMock(WhitelistRepositoryInterface::class);
$this->redirect = $this->getMockBuilder(Redirect::class)
->disableOriginalConstructor()
->setMethodsExcept(['getHttpResponseCode', 'setHttpResponseCode', 'renderResult'])
->getMock();
$this->redirectFactory = $this->createMock(RedirectFactory::class);
$this->redirectFactory->expects($this->any())
->method('create')
->willReturn($this->redirect);
$this->request = $this->createMock(RequestInterface::class);
$this->messageManager = $this->createMock(ManagerInterface::class);
$this->context = $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
->getMock();
$this->context->expects($this->any())
->method('getResultRedirectFactory')
->willReturn($this->redirectFactory);
$this->context->expects($this->any())
->method('getRequest')
->willReturn($this->request);
$this->context->expects($this->any())
->method('getMessageManager')
->willReturn($this->messageManager);
}

/**
* @test
*/
public function successfullyDeletingWhitelistEntryReturn200Statuscode()
{
$this->whitelist->expects($this->once())
->method('deleteEntry')
->willReturn(true);

$action = new Delete(
$this->whitelist,
$this->context
);
/** @var Redirect $result */
$result = $action->execute();

$this->assertSame($result->getHttpResponseCode(), 200);
}

/**
* @test
*/
public function failingToDeletingWhitelistEntryWillReturn500Statuscode()
{
$this->whitelist->expects($this->once())
->method('deleteEntry')
->willReturn(false);

$action = new Delete(
$this->whitelist,
$this->context
);
/** @var Redirect $result */
$result = $action->execute();

$this->assertSame($result->getHttpResponseCode(), \Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR);
}

/**
* @test
*/
public function exceptionsWillReturn500Statuscode()
{
$this->whitelist->expects($this->once())
->method('deleteEntry')
->willThrowException(new \RuntimeException());

$action = new Delete(
$this->whitelist,
$this->context
);
/** @var Redirect $result */
$result = $action->execute();

$this->assertSame($result->getHttpResponseCode(), \Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR);
}
}
58 changes: 58 additions & 0 deletions Test/Unit/Controller/Adminhtml/Manage/EditUnitTest.php
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Force Login module for Magento2.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace BitExpert\ForceCustomerLogin\Test\Unit\Controller\Adminhtml\Manage;

use BitExpert\ForceCustomerLogin\Controller\Adminhtml\Manage\Edit;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\ViewInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Class ModuleCheckUnitTest
*
* @package BitExpert\ForceCustomerLogin\Test\Unit\Controller
*/
class EditUnitTest extends TestCase
{
/**
* @test
*/
public function executeSuccessfully()
{
$view = $this->createMock(ViewInterface::class);
$view->expects($this->once())
->method('loadLayout');
$view->expects($this->once())
->method('renderLayout');

$context = $this->getContext();
$context->expects($this->atLeastOnce())
->method('getView')
->willReturn($view);

$action = new Edit(
$context
);
$action->execute();
}

/**
* @return MockObject|Context
*/
private function getContext()
{
return $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
->getMock();
}
}
58 changes: 58 additions & 0 deletions Test/Unit/Controller/Adminhtml/Manage/IndexUnitTest.php
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Force Login module for Magento2.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace BitExpert\ForceCustomerLogin\Test\Unit\Controller\Adminhtml\Manage;

use BitExpert\ForceCustomerLogin\Controller\Adminhtml\Manage\Index;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\ViewInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Class ModuleCheckUnitTest
*
* @package BitExpert\ForceCustomerLogin\Test\Unit\Controller
*/
class IndexUnitTest extends TestCase
{
/**
* @test
*/
public function executeSuccessfully()
{
$view = $this->createMock(ViewInterface::class);
$view->expects($this->once())
->method('loadLayout');
$view->expects($this->once())
->method('renderLayout');

$context = $this->getContext();
$context->expects($this->atLeastOnce())
->method('getView')
->willReturn($view);

$action = new Index(
$context
);
$action->execute();
}

/**
* @return MockObject|Context
*/
private function getContext()
{
return $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
->getMock();
}
}
23 changes: 23 additions & 0 deletions Test/Unit/Controller/Adminhtml/Manage/Redirect.php
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Force Login module for Magento2.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace BitExpert\ForceCustomerLogin\Test\Unit\Controller\Adminhtml\Manage;

/**
* Helper class to be able to retrieve the response code set in the action classes.
*/
class Redirect extends \Magento\Backend\Model\View\Result\Redirect
{
public function getHttpResponseCode()
{
return $this->httpResponseCode;
}
}
64 changes: 23 additions & 41 deletions phpunit.xml
@@ -1,52 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true">

<testsuites>
<testsuite name="unit">
<directory suffix="UnitTest.php">./Test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./</directory>
<exclude>
<directory>./vendor</directory>
<directory>./var</directory>
<directory>./build</directory>
<directory>./Model/ResourceModel/WhitelistEntry/Collection.php</directory>
<directory>./Setup</directory>
<directory>./Test</directory>
<directory>./logs</directory>
<file>./registration.php</file>
<file>./validate_m2_package.php</file>
</exclude>
</whitelist>
</filter>

<logging>
<log type="coverage-text"
target="php://stdout"
showUncoveredFiles="false"
showOnlySummary="true"
logIncompleteSkipped="false" />

<log type="coverage-html"
target="build/coverage"
charset="UTF-8"
showUncoveredFiles="true"
showOnlySummary="false"
lowUpperBound="35"
highLowerBound="70"
logIncompleteSkipped="false" />
</logging>
<filter>
<whitelist>
<directory suffix=".php">./</directory>
<exclude>
<directory>./vendor</directory>
<directory>./var</directory>
<directory>./build</directory>
<directory>./Model/ResourceModel/WhitelistEntry/Collection.php</directory>
<directory>./Setup</directory>
<directory>./Test</directory>
<directory>./logs</directory>
<file>./registration.php</file>
<file>./validate_m2_package.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>

0 comments on commit 5a11505

Please sign in to comment.