From d716f24af9abcd40c1d69410b5f4902be000aa61 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Wed, 14 Aug 2019 23:15:05 +0200 Subject: [PATCH] add last tests --- src/Application/Model/d3totp.php | 8 - src/Setup/Events.php | 4 + src/Setup/Installation.php | 12 +- .../unit/Application/Model/d3totpTest.php | 7 +- src/tests/unit/Setup/InstallationTest.php | 228 ++++++++++++++++++ 5 files changed, 246 insertions(+), 13 deletions(-) create mode 100644 src/tests/unit/Setup/InstallationTest.php diff --git a/src/Application/Model/d3totp.php b/src/Application/Model/d3totp.php index a049927..76a8334 100644 --- a/src/Application/Model/d3totp.php +++ b/src/Application/Model/d3totp.php @@ -18,7 +18,6 @@ namespace D3\Totp\Application\Model; use BaconQrCode\Renderer\Image\Svg; use BaconQrCode\Renderer\RendererInterface; use BaconQrCode\Writer; -use D3\ModCfg\Application\Model\d3database; use D3\Totp\Application\Model\Exceptions\d3totp_wrongOtpException; use Doctrine\DBAL\DBALException; use OTPHP\TOTP; @@ -59,17 +58,10 @@ class d3totp extends BaseModel public function loadByUserId($userId) { $this->userId = $userId; - //$oQB = d3database::getInstance()->getQueryBuilder(); $oDb = $this->d3GetDb(); if ($oDb->getOne("SHOW TABLES LIKE '".$this->tableName."'")) { $query = "SELECT oxid FROM ".$this->getViewName().' WHERE oxuserid = '.$oDb->quote($userId).' LIMIT 1'; - /*$oQB->select('oxid') - ->from($this->getViewName()) - ->where("oxuserid = " . $oQB->createNamedParameter($userId)) - ->setMaxResults(1); - */ - //$this->load($oDb->getOne($oQB->getSQL(), $oQB->getParameters())); $this->load($oDb->getOne($query)); } } diff --git a/src/Setup/Events.php b/src/Setup/Events.php index 9f974ee..0d5805d 100644 --- a/src/Setup/Events.php +++ b/src/Setup/Events.php @@ -28,6 +28,7 @@ use OxidEsales\Eshop\Core\Exception\SystemComponentException; class Events { /** + * @codeCoverageIgnore * @throws d3ShopCompatibilityAdapterException * @throws DBALException * @throws DatabaseConnectionException @@ -42,6 +43,9 @@ class Events } } + /** + * @codeCoverageIgnore + */ public static function onDeactivate() { } diff --git a/src/Setup/Installation.php b/src/Setup/Installation.php index 03d0a31..ba90199 100644 --- a/src/Setup/Installation.php +++ b/src/Setup/Installation.php @@ -18,6 +18,7 @@ namespace D3\Totp\Setup; use D3\ModCfg\Application\Model\d3database; use D3\ModCfg\Application\Model\Install\d3install_updatebase; use Doctrine\DBAL\DBALException; +use OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface; use OxidEsales\Eshop\Core\DatabaseProvider; use OxidEsales\Eshop\Core\Exception\ConnectionException; use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException; @@ -257,7 +258,16 @@ class Installation extends d3install_updatebase { $query = "SELECT 1 FROM " . getViewName('oxseo') . " WHERE oxstdurl = 'index.php?cl=d3_account_totp'"; - return !DatabaseProvider::getDb()->getOne($query); + return !$this->d3GetDb()->getOne($query); + } + + /** + * @return DatabaseInterface + * @throws DatabaseConnectionException + */ + public function d3GetDb() + { + return DatabaseProvider::getDb(); } /** diff --git a/src/tests/unit/Application/Model/d3totpTest.php b/src/tests/unit/Application/Model/d3totpTest.php index ce3dbc3..5c157e1 100644 --- a/src/tests/unit/Application/Model/d3totpTest.php +++ b/src/tests/unit/Application/Model/d3totpTest.php @@ -19,15 +19,12 @@ namespace D3\Totp\tests\unit\Application\Model; use BaconQrCode\Renderer\Image\Svg; use BaconQrCode\Writer; -use D3\Totp\Application\Model\d3backupcode; use D3\Totp\Application\Model\d3backupcodelist; use D3\Totp\Application\Model\d3totp; use D3\Totp\Application\Model\Exceptions\d3totp_wrongOtpException; use D3\Totp\tests\unit\d3TotpUnitTestCase; use OTPHP\TOTP; -use OxidEsales\Eshop\Application\Controller\FrontendController; use OxidEsales\Eshop\Application\Model\User; -use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database; use OxidEsales\Eshop\Core\Registry; use PHPUnit_Framework_MockObject_MockObject; @@ -105,9 +102,11 @@ class d3totpTest extends d3TotpUnitTestCase { /** @var Database|PHPUnit_Framework_MockObject_MockObject $oDbMock */ $oDbMock = $this->getMock(Database::class, array( - 'getOne' + 'getOne', + 'quote' ), array(), '', false); $oDbMock->expects($this->exactly(2))->method('getOne')->willReturnOnConsecutiveCalls(true, true); + $oDbMock->method('quote')->willReturn(true); /** @var d3totp|PHPUnit_Framework_MockObject_MockObject $oModelMock */ $oModelMock = $this->getMock(d3totp::class, array( diff --git a/src/tests/unit/Setup/InstallationTest.php b/src/tests/unit/Setup/InstallationTest.php new file mode 100644 index 0000000..23b6e59 --- /dev/null +++ b/src/tests/unit/Setup/InstallationTest.php @@ -0,0 +1,228 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\Totp\tests\unit\Setup; + +use D3\Totp\Setup\Installation; +use D3\Totp\tests\unit\d3TotpUnitTestCase; +use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database; +use PHPUnit_Framework_MockObject_MockObject; +use ReflectionException; + +class InstallationTest extends d3TotpUnitTestCase +{ + /** @var Installation */ + protected $_oModel; + + /** + * setup basic requirements + */ + public function setUp() + { + parent::setUp(); + + $this->_oModel = oxNew(Installation::class); + } + + public function tearDown() + { + parent::tearDown(); + + unset($this->_oModel); + } + + /** + * @test + * @throws ReflectionException + */ + public function doesTotpTableNotExistCallCheckMethod() + { + /** @var Installation|PHPUnit_Framework_MockObject_MockObject $oModelMock */ + $oModelMock = $this->getMock(Installation::class, array( + '_checkTableNotExist', + )); + $oModelMock->expects($this->once())->method('_checkTableNotExist')->with('d3totp')->willReturn('testReturn'); + + $this->_oModel = $oModelMock; + + $this->assertSame( + 'testReturn', + $this->callMethod($this->_oModel, 'doesTotpTableNotExist') + ); + } + + /** + * @test + * @throws ReflectionException + */ + public function addTotpTableNotExistingTable() + { + /** @var Installation|PHPUnit_Framework_MockObject_MockObject $oModelMock */ + $oModelMock = $this->getMock(Installation::class, array( + 'doesTotpTableNotExist', + '_addTable2', + )); + $oModelMock->method('doesTotpTableNotExist')->willReturn(true); + $oModelMock->expects($this->once())->method('_addTable2')->willReturn('testReturn'); + + $this->_oModel = $oModelMock; + + $this->assertSame( + 'testReturn', + $this->callMethod($this->_oModel, 'addTotpTable') + ); + } + + /** + * @test + * @throws ReflectionException + */ + public function addTotpTableExistingTable() + { + /** @var Installation|PHPUnit_Framework_MockObject_MockObject $oModelMock */ + $oModelMock = $this->getMock(Installation::class, array( + 'doesTotpTableNotExist', + '_addTable2', + )); + $oModelMock->method('doesTotpTableNotExist')->willReturn(false); + $oModelMock->expects($this->never())->method('_addTable2')->willReturn('testReturn'); + + $this->_oModel = $oModelMock; + + $this->assertFalse( + $this->callMethod($this->_oModel, 'addTotpTable') + ); + } + + /** + * @test + * @throws ReflectionException + */ + public function doesTotpBCTableNotExistCallCheckMethod() + { + /** @var Installation|PHPUnit_Framework_MockObject_MockObject $oModelMock */ + $oModelMock = $this->getMock(Installation::class, array( + '_checkTableNotExist', + )); + $oModelMock->expects($this->once())->method('_checkTableNotExist')->with('d3totp_backupcodes')->willReturn('testReturn'); + + $this->_oModel = $oModelMock; + + $this->assertSame( + 'testReturn', + $this->callMethod($this->_oModel, 'doesTotpBCTableNotExist') + ); + } + + /** + * @test + * @throws ReflectionException + */ + public function addTotpBCTableNotExistingTable() + { + /** @var Installation|PHPUnit_Framework_MockObject_MockObject $oModelMock */ + $oModelMock = $this->getMock(Installation::class, array( + 'doesTotpBCTableNotExist', + '_addTable2', + )); + $oModelMock->method('doesTotpBCTableNotExist')->willReturn(true); + $oModelMock->expects($this->once())->method('_addTable2')->willReturn('testReturn'); + + $this->_oModel = $oModelMock; + + $this->assertSame( + 'testReturn', + $this->callMethod($this->_oModel, 'addTotpBCTable') + ); + } + + /** + * @test + * @throws ReflectionException + */ + public function addTotpBCTableExistingTable() + { + /** @var Installation|PHPUnit_Framework_MockObject_MockObject $oModelMock */ + $oModelMock = $this->getMock(Installation::class, array( + 'doesTotpBCTableNotExist', + '_addTable2', + )); + $oModelMock->method('doesTotpBCTableNotExist')->willReturn(false); + $oModelMock->expects($this->never())->method('_addTable2')->willReturn('testReturn'); + + $this->_oModel = $oModelMock; + + $this->assertFalse( + $this->callMethod($this->_oModel, 'addTotpBCTable') + ); + } + + /** + * @test + * @throws ReflectionException + */ + public function d3GetDbReturnsRightInstance() + { + $this->assertInstanceOf( + Database::class, + $this->callMethod($this->_oModel, 'd3GetDb') + ); + } + + /** + * @test + * @throws ReflectionException + */ + public function checkSEONotExistsPass() + { + /** @var Database|PHPUnit_Framework_MockObject_MockObject $oDbMock */ + $oDbMock = $this->getMock(Database::class, array( + 'getOne' + ), array(), '', false); + $oDbMock->expects($this->once())->method('getOne')->willReturn(true); + + /** @var Installation|PHPUnit_Framework_MockObject_MockObject $oModelMock */ + $oModelMock = $this->getMock(Installation::class, array( + 'd3GetDb' + )); + $oModelMock->method('d3GetDb')->willReturn($oDbMock); + + $this->_oModel = $oModelMock; + + $this->assertFalse($this->callMethod($this->_oModel, 'checkSEONotExists')); + } + + /** + * @test + * @throws ReflectionException + */ + public function addSEOPass() + { + /** @var Installation|PHPUnit_Framework_MockObject_MockObject $oModelMock */ + $oModelMock = $this->getMock(Installation::class, array( + '_executeMultipleQueries' + )); + $oModelMock->expects($this->once())->method('_executeMultipleQueries')->willReturn('testReturn'); + + $this->_oModel = $oModelMock; + + $this->assertSame( + 'testReturn', + $this->callMethod($this->_oModel, 'addSEO') + ); + } +} \ No newline at end of file