Skip to content

Commit

Permalink
Merge pull request #577 from extcode/task/move-service-functional-tests
Browse files Browse the repository at this point in the history
[TASK] Move service functional tests to unit tests
  • Loading branch information
extcode authored Oct 8, 2024
2 parents 67ea555 + 6b83966 commit 105a312
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Extcode\Cart\Tests\Functional\Service;
namespace Extcode\Cart\Tests\Unit\Service;

/*
* This file is part of the package extcode/cart.
Expand All @@ -13,20 +13,12 @@
use Extcode\Cart\Service\AbstractConfigurationFromTypoScriptService;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

#[CoversClass(AbstractConfigurationFromTypoScriptService::class)]
class AbstractConfigurationFromTypoScriptServiceTest extends FunctionalTestCase
class AbstractConfigurationFromTypoScriptServiceTest extends UnitTestCase
{
public function setUp(): void
{
$this->testExtensionsToLoad[] = 'typo3conf/ext/cart';

parent::setUp();
}

#[Test]
public function getTypePluginSettingsReturnsTypeCountrySettings(): void
{
Expand Down Expand Up @@ -86,14 +78,7 @@ public function getTypePluginSettingsReturnsTypeCountrySettings(): void
],
];

$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);

$paymentMethodsService = new class ($configurationManager, new ServiceFactory()) extends AbstractConfigurationFromTypoScriptService {};

$reflection = new \ReflectionClass($paymentMethodsService);
$reflection_property = $reflection->getProperty('configurations');
$reflection_property->setAccessible(true);
$reflection_property->setValue($paymentMethodsService, $configurations);
$paymentMethodsService = $this->createSubject($configurations);

$parsedData = $paymentMethodsService->getConfigurationsForType($type, $country);

Expand Down Expand Up @@ -174,14 +159,7 @@ public function getTypeZonePluginSettingsReturnsTypeZoneSettings(): void
],
];

$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);

$paymentMethodsService = new class ($configurationManager, new ServiceFactory()) extends AbstractConfigurationFromTypoScriptService {};

$reflection = new \ReflectionClass($paymentMethodsService);
$reflection_property = $reflection->getProperty('configurations');
$reflection_property->setAccessible(true);
$reflection_property->setValue($paymentMethodsService, $configurations);
$paymentMethodsService = $this->createSubject($configurations);

$parsedData = $paymentMethodsService->getConfigurationsForType($type, $country);

Expand All @@ -199,4 +177,12 @@ public function getTypeZonePluginSettingsReturnsTypeZoneSettings(): void
$parsedData['options']
);
}

private function createSubject(array $configurations)
{
$configurationManager = $this->createStub(ConfigurationManagerInterface::class);
$configurationManager->method('getConfiguration')->willReturn($configurations);

return new class ($configurationManager, new ServiceFactory()) extends AbstractConfigurationFromTypoScriptService {};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Extcode\Cart\Tests\Functional\Service;
namespace Extcode\Cart\Tests\Unit\Service;

/*
* This file is part of the package extcode/cart.
Expand All @@ -10,28 +10,17 @@
*/

use Extcode\Cart\Domain\Model\Cart\TaxClass;
use Extcode\Cart\Domain\Model\Cart\TaxClassFactory;
use Extcode\Cart\Service\TaxClassService;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

#[CoversClass(FunctionalTestCase::class)]
class TaxClassServiceTest extends FunctionalTestCase
#[CoversClass(TaxClassService::class)]
class TaxClassServiceTest extends UnitTestCase
{
protected TaxClassService $taxClassService;

public function setUp(): void
{
$this->testExtensionsToLoad[] = 'extcode/cart';

parent::setUp();

$this->taxClassService = GeneralUtility::makeInstance(
TaxClassService::class
);
}

#[Test]
public function parsingTaxClassesFromTypoScriptWithoutCountryCodeReturnsDirectlyConfiguredArrayOfTaxClasses(): void
{
Expand All @@ -55,14 +44,9 @@ public function parsingTaxClassesFromTypoScriptWithoutCountryCodeReturnsDirectly
],
];

$reflection = new \ReflectionClass($this->taxClassService);
$reflection_property = $reflection->getProperty('settings');
$reflection_property->setAccessible(true);
$reflection_property->setValue($this->taxClassService, $settings);

$countryCode = '';

$taxClasses = $this->taxClassService->getTaxClasses($countryCode);
$taxClasses = $this->createSubject($settings)->getTaxClasses($countryCode);

self::assertIsArray(
$taxClasses
Expand Down Expand Up @@ -127,14 +111,9 @@ public function parsingTaxClassesFromTypoScriptWithCountryCodeReturnsCountrySpec
],
];

$reflection = new \ReflectionClass($this->taxClassService);
$reflection_property = $reflection->getProperty('settings');
$reflection_property->setAccessible(true);
$reflection_property->setValue($this->taxClassService, $settings);

$countryCode = 'AT';

$taxClasses = $this->taxClassService->getTaxClasses($countryCode);
$taxClasses = $this->createSubject($settings)->getTaxClasses($countryCode);

self::assertIsArray(
$taxClasses
Expand Down Expand Up @@ -216,14 +195,9 @@ public function parsingTaxClassesFromTypoScriptWithNotConfiguredCountryCodeRetur
],
];

$reflection = new \ReflectionClass($this->taxClassService);
$reflection_property = $reflection->getProperty('settings');
$reflection_property->setAccessible(true);
$reflection_property->setValue($this->taxClassService, $settings);

$countryCode = 'CH';

$taxClasses = $this->taxClassService->getTaxClasses($countryCode);
$taxClasses = $this->createSubject($settings)->getTaxClasses($countryCode);

self::assertIsArray(
$taxClasses
Expand Down Expand Up @@ -259,16 +233,24 @@ public function parsingTaxClassesFromTypoScriptWithIntegerZeroAsCalcIsValid(): v
],
];

$reflection = new \ReflectionClass($this->taxClassService);
$reflection_property = $reflection->getProperty('settings');
$reflection_property->setAccessible(true);
$reflection_property->setValue($this->taxClassService, $settings);

$taxClasses = $this->taxClassService->getTaxClasses();
$taxClasses = $this->createSubject($settings)->getTaxClasses();

self::assertEquals(
$taxClasses[1]->getCalc(),
0
);
}

private function createSubject(array $settings): TaxClassService
{
$configurationManager = $this->createStub(ConfigurationManagerInterface::class);
$configurationManager->method('getConfiguration')->willReturn($settings);

return new TaxClassService(
$configurationManager,
new TaxClassFactory(
$this->createStub(LoggerInterface::class)
)
);
}
}

0 comments on commit 105a312

Please sign in to comment.