Skip to content

Commit

Permalink
add integration test and fix onDeactivate method
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-blom committed Apr 9, 2020
1 parent 5c3952c commit 42d4875
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Core/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function clearAllCarbonCopies(): void
*
* @return string
*/
protected function idnToAscii($idn)
private function idnToAscii($idn)
{
if (function_exists('idn_to_ascii')) {
// for old PHP versions support
Expand Down
28 changes: 21 additions & 7 deletions Core/MultiOrderMailReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function onActivate(): void
{
$dbMetaDataHandler = oxNew(DbMetaDataHandler::class);

if (!$dbMetaDataHandler->fieldExists('PBOWNEREMAILRECEIVER', 'oxshops')) {
if ( ! $dbMetaDataHandler->fieldExists('PBOWNEREMAILRECEIVER', 'oxshops')) {
DatabaseProvider::getDb()->execute(
"ALTER TABLE oxshops ADD PBOWNEREMAILRECEIVER text NOT NULL default '' COMMENT 'Additional recipients for order owner mail';"
);
Expand All @@ -36,13 +36,27 @@ public static function onActivate(): void
public static function onDeactivate(): void
{
$container = ContainerFactory::getInstance()->getContainer();
/** @var QueryBuilderFactoryInterface $queryBuilderFactory */
$queryBuilderFactory = $container->get(QueryBuilderFactoryInterface::class);
$queryBuilder = $queryBuilderFactory->create();

$queryBuilder = $queryBuilderFactory->create();
$queryBuilder->delete('oxtplblocks', 'tpl')
->where('tpl.oxmodule = :moduleId')
->setParameters([
'moduleId' => 'multiordermailreceiver'
]);
$queryBuilder->select('oxid')
->from('oxtplblocks')
->where('oxmodule = :moduleId')
->setParameters([
'moduleId' => 'multiordermailreceiver'
]);

$row = $queryBuilder->execute()->fetch();

// deletes are only allowed by primarykey
if (count($row) > 0 && array_key_exists('oxid', $row)) {
$queryBuilder->delete('oxtplblocks')
->where('oxid = :id')
->setParameters([
'id' => $row['oxid']
]);
$queryBuilder->execute();
}
}
}
79 changes: 79 additions & 0 deletions Tests/Integration/Core/MultiOrderMailReceiverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace PaBlo\MultiOrderMailReceiver\Test\Integration\Core;

use OxidEsales\Eshop\Core\DbMetaDataHandler;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use OxidEsales\TestingLibrary\UnitTestCase;
use PaBlo\MultiOrderMailReceiver\Core\MultiOrderMailReceiver;


/**
* Class MultiOrderMailReceiverTest
* UNIT/INTEGRATION tests for core class MultiOrderMailReceiver.
*
* @package PaBlo\MultiOrderMailReceiver\Test\Integration\Core
*/
class MultiOrderMailReceiverTest extends UnitTestCase
{
/**
* Subject under the test.
*
* @var MultiOrderMailReceiver
*/
protected $SUT;

/**
* Set SUT state before test.
*/
protected function setUp()
{
parent::setUp();

$this->SUT = new MultiOrderMailReceiver();
}

/**
* @covers \PaBlo\MultiOrderMailReceiver\Core\MultiOrderMailReceiver::onActivate
*/
public function testOnActivate_willAddCustomFieldToTheDatabase(): void
{
// ensure field does not exists
$dbMetaDataHandler = oxNew(DbMetaDataHandler::class);
$dbMetaDataHandler->executeSql(['ALTER TABLE oxshops DROP PBOWNEREMAILRECEIVER;']);
$this->assertFalse($dbMetaDataHandler->fieldExists('PBOWNEREMAILRECEIVER', 'oxshops'));

$this->SUT::onActivate();
$this->assertTrue($dbMetaDataHandler->fieldExists('PBOWNEREMAILRECEIVER', 'oxshops'));
}

/**
* @covers \PaBlo\MultiOrderMailReceiver\Core\MultiOrderMailReceiver::onDeactivate
*/
public function testOnDeactivate_willRemoveTemplateBlocks(): void
{
$container = ContainerFactory::getInstance()->getContainer();

// ensure the module is active
/** @var QueryBuilderFactoryInterface $queryBuilderFactory */
$queryBuilderFactory = $container->get(QueryBuilderFactoryInterface::class);

$queryBuilder = $queryBuilderFactory->create();
$queryBuilder->select('oxid')
->from('oxtplblocks', 'tpl')
->where('tpl.oxmodule = :moduleId')
->setParameters([
'moduleId' => 'multiordermailreceiver'
]);

$result = $queryBuilder->execute()->fetchAll();
$this->assertCount(1, $result);

$this->SUT::onDeactivate();

$result = $queryBuilder->execute()->fetchAll();
$this->assertCount(0, $result);
}
}

7 changes: 4 additions & 3 deletions Tests/Unit/Core/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PaBlo\MultiOrderMailReceiver\Test\Unit\Core;


use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\TestingLibrary\UnitTestCase;
use PaBlo\MultiOrderMailReceiver\Core\Email;
Expand Down Expand Up @@ -30,8 +29,8 @@ public function setUp()
parent::setUp();

$this->SUT = $this->getMockBuilder(Email::class)
->setMethods(['__call'])
->getMock();
->setMethods(['__call', 'send', 'getRenderer'])
->getMock();
}

/**
Expand Down Expand Up @@ -168,4 +167,6 @@ public function testIdnToAscii_willReturnNormalString(): void
$result = $reflectedMethod->invokeArgs($this->SUT, ['täst.de']);
$this->assertSame('xn--tst-qla.de', $result);
}


}

0 comments on commit 42d4875

Please sign in to comment.