Commit 3dee94c0 authored by matthieu-rolland's avatar matthieu-rolland
Browse files

create new test case class ContextStateTestCase and use it

parent c921514f
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
namespace Tests\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
use Shop;
abstract class ContextStateTestCase extends TestCase
{
/**
* @param array $contextFields
*
* @return MockObject|Context
*/
protected function createContextMock(array $contextFields)
{
$contextMock = $this->getMockBuilder(\Context::class)
->disableOriginalConstructor()
->getMock();
foreach ($contextFields as $fieldName => $contextValue) {
$contextMock->$fieldName = $contextValue;
}
LegacyContext::setInstanceForTesting($contextMock);
return $contextMock;
}
/**
* @param string $className
* @param int $objectId
*
* @return MockObject|Cart|Country|Currency|Customer|Language|Shop
*/
protected function createContextFieldMock(string $className, int $objectId)
{
$contextField = $this->getMockBuilder($className)
->disableOriginalConstructor()
->getMock();
$contextField->id = $objectId;
return $contextField;
}
}
......@@ -26,12 +26,10 @@
namespace Tests\Unit\Adapter {
use Cart;
use Context;
use Country;
use Currency;
use Customer;
use Language;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use PrestaShop\PrestaShop\Adapter\ContextStateManager;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
......@@ -555,42 +553,6 @@ namespace Tests\Unit\Adapter {
$this->assertEquals(42, $context->language->id);
$this->assertNull($contextStateManager->getContextFieldsStack());
}
/**
* @param string $className
* @param int $objectId
*
* @return MockObject|Cart|Country|Currency|Customer|Language|Shop
*/
private function createContextFieldMock(string $className, int $objectId)
{
$contextField = $this->getMockBuilder($className)
->disableOriginalConstructor()
->getMock();
$contextField->id = $objectId;
return $contextField;
}
/**
* @param array $contextFields
*
* @return MockObject|Context
*/
private function createContextMock(array $contextFields)
{
$contextMock = $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
->getMock();
foreach ($contextFields as $fieldName => $contextValue) {
$contextMock->$fieldName = $contextValue;
}
LegacyContext::setInstanceForTesting($contextMock);
return $contextMock;
}
}
}
......
......@@ -28,31 +28,32 @@ declare(strict_types=1);
namespace Tests\Unit\Core\Addon\Theme;
use Phake;
use PHPUnit\Framework\TestCase;
use PrestaShop\PrestaShop\Adapter\Configuration;
use PrestaShop\PrestaShop\Core\Addon\Theme\ThemeRepository;
use Shop;
use Symfony\Component\Filesystem\Filesystem;
use Tests\TestCase\ContextStateTestCase;
class ThemeRepositoryTest extends TestCase
class ThemeRepositoryTest extends ContextStateTestCase
{
const NOTICE = '[ThemeRepository] ';
private $repository;
protected function setUp()
{
$shop = Phake::mock('Shop');
$shop->id = 1;
$shop->name = 'Demo shop';
$context = $this->createContextMock([
'shop' => $this->createContextFieldMock(Shop::class, 1),
]);
Shop::setContext(Shop::CONTEXT_SHOP, 1);
$configuration = new Configuration();
$configuration->restrictUpdatesTo($shop);
$configuration->restrictUpdatesTo($context->shop);
/* @var \PrestaShop\PrestaShop\Core\Addon\Theme\ThemeRepository */
$this->repository = new ThemeRepository(
$configuration,
new Filesystem(),
$shop
$context->shop
);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment