Skip to content

Commit

Permalink
#60 Fixes on class resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Horn committed Jun 28, 2017
1 parent a8b2ecb commit 807d2ec
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 56 deletions.
6 changes: 4 additions & 2 deletions Block/Adminhtml/Manage/Create/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Form extends \Magento\Backend\Block\Widget\Form\Generic
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Data\FormFactory $formFactory
* @param WhitelistEntryFactoryInterface $entityFactory
* @param StrategyManager $strategyManager
* @param array $data
*/
public function __construct(
Expand All @@ -46,6 +47,7 @@ public function __construct(
array $data
) {
$this->entityFactory = $entityFactory;
$this->strategyManager = $strategyManager;
parent::__construct($context, $registry, $formFactory, $data);
}

Expand Down Expand Up @@ -105,11 +107,11 @@ protected function _prepareForm()
'required' => true
]);

$fieldsetBase->addField('strategy', 'text', [
$fieldsetBase->addField('strategy', 'select', [
'name' => 'strategy',
'label' => __('Strategy'),
'title' => __('Strategy'),
'value' => $whitelistEntry->getUrlRule(),
'value' => $whitelistEntry->getStrategy(),
'options' => $this->strategyManager->getStrategyNames(),
'required' => true
]);
Expand Down
2 changes: 2 additions & 0 deletions Controller/Adminhtml/Manage/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function execute()
$this->getRequest()->getParam('whitelist_entry_id'),
$this->getRequest()->getParam('label'),
$this->getRequest()->getParam('url_rule'),
$this->getRequest()->getParam('strategy'),
$this->getRequest()->getParam('store_id', 0)
);

Expand Down Expand Up @@ -106,6 +107,7 @@ public function execute()
[
'label' => \base64_encode($this->getRequest()->getParam('label')),
'url_rule' => \base64_encode($this->getRequest()->getParam('url_rule')),
'strategy' => \base64_encode($this->getRequest()->getParam('strategy')),
'store_id' => \base64_encode($this->getRequest()->getParam('store_id', 0))
]);
}
Expand Down
22 changes: 22 additions & 0 deletions Helper/Strategy/RegExAllMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@
*/
class RegExAllMatcher implements StrategyInterface
{
/**+
* @var string
*/
private $name;

/**
* RegExAllMatcher constructor.
* @param string $name
*/
public function __construct($name)
{
$this->name = $name;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}

/**
* {@inheritdoc}
*/
Expand Down
22 changes: 22 additions & 0 deletions Helper/Strategy/StaticMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@
*/
class StaticMatcher implements StrategyInterface
{
/**+
* @var string
*/
private $name;

/**
* RegExAllMatcher constructor.
* @param string $name
*/
public function __construct($name)
{
$this->name = $name;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}

/**
* {@inheritdoc}
*/
Expand Down
5 changes: 5 additions & 0 deletions Helper/Strategy/StrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ interface StrategyInterface
* @return bool
*/
public function isMatch($url, WhitelistEntry $rule);

/**
* @return string
*/
public function getName();
}
6 changes: 3 additions & 3 deletions Helper/Strategy/StrategyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class StrategyManager

/**
* LoginRequiredOnVisitorInitObserver constructor.
* @param array $strategies
* @param StrategyInterface[] $strategies
*/
public function __construct(array $strategies)
{
foreach ($strategies as $identifier => $strategyEntry) {
$this->strategies[$identifier] = $strategyEntry['instance'];
$this->strategyNames[$identifier] = $strategyEntry['name'];
$this->strategies[$identifier] = $strategyEntry;
$this->strategyNames[$identifier] = $strategyEntry->getName();
}
}

Expand Down
2 changes: 1 addition & 1 deletion Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function runUpgrade210(SchemaSetupInterface $setup, ModuleContextInter
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'size' => 255,
'nullable' => false,
'nullable' => true,
'comment' => 'strategy matcher identifier',
]
);
Expand Down
10 changes: 6 additions & 4 deletions Test/Unit/Helper/Strategy/RegExAllMatcherUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class RegExAllMatcherUnitTest extends \PHPUnit\Framework\TestCase
*/
public function matchStaticRulesCorrectly()
{
$matcher = new RegExAllMatcher();
$matcher = new RegExAllMatcher('foobar');

$this->assertEquals('foobar', $matcher->getName());

/* @var $rule \bitExpert\ForceCustomerLogin\Model\WhitelistEntry */
$rule = $this->getMockBuilder('\bitExpert\ForceCustomerLogin\Model\WhitelistEntry')
Expand All @@ -50,7 +52,7 @@ public function matchStaticRulesCorrectly()
*/
public function matchCatchAllRuleCorrectly()
{
$matcher = new RegExAllMatcher();
$matcher = new RegExAllMatcher('foobar');

/* @var $rule \bitExpert\ForceCustomerLogin\Model\WhitelistEntry */
$rule = $this->getMockBuilder('\bitExpert\ForceCustomerLogin\Model\WhitelistEntry')
Expand All @@ -76,7 +78,7 @@ public function matchCatchAllRuleCorrectly()
*/
public function matchCatchAllWithLineEndIdentifierRuleCorrectly()
{
$matcher = new RegExAllMatcher();
$matcher = new RegExAllMatcher('foobar');

/* @var $rule \bitExpert\ForceCustomerLogin\Model\WhitelistEntry */
$rule = $this->getMockBuilder('\bitExpert\ForceCustomerLogin\Model\WhitelistEntry')
Expand All @@ -102,7 +104,7 @@ public function matchCatchAllWithLineEndIdentifierRuleCorrectly()
*/
public function matchHomepageRuleCorrectly()
{
$matcher = new RegExAllMatcher();
$matcher = new RegExAllMatcher('foobar');

/* @var $rule \bitExpert\ForceCustomerLogin\Model\WhitelistEntry */
$rule = $this->getMockBuilder('\bitExpert\ForceCustomerLogin\Model\WhitelistEntry')
Expand Down
4 changes: 3 additions & 1 deletion Test/Unit/Helper/Strategy/StaticMatcherUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class StaticMatcherUnitTest extends \PHPUnit\Framework\TestCase
*/
public function matchStaticRulesCorrectly()
{
$matcher = new StaticMatcher();
$matcher = new StaticMatcher('foobar');

$this->assertEquals('foobar', $matcher->getName());

/* @var $rule \bitExpert\ForceCustomerLogin\Model\WhitelistEntry */
$rule = $this->getMockBuilder('\bitExpert\ForceCustomerLogin\Model\WhitelistEntry')
Expand Down
76 changes: 40 additions & 36 deletions Test/Unit/Helper/Strategy/StrategyManagerUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ class StrategyManagerUnitTest extends \PHPUnit\Framework\TestCase
*/
public function getStrategyNamesSuccessfully()
{
/* @var $strategy1 StrategyInterface */
/* @var $strategy1 StrategyInterface|\PHPUnit_Framework_MockObject_MockObject */
$strategy1 = $this->createMock('\bitExpert\ForceCustomerLogin\Helper\Strategy\StrategyInterface');
$strategy1->expects($this->once())
->method('getName')
->willReturn('Static');
/* @var $strategy2 StrategyInterface|\PHPUnit_Framework_MockObject_MockObject */
$strategy2 = $this->createMock('\bitExpert\ForceCustomerLogin\Helper\Strategy\StrategyInterface');
$strategy2->expects($this->once())
->method('getName')
->willReturn('RegEx (All)');

$manager = new StrategyManager([
'default' => [
'name' => 'Static',
'instance' => $strategy1
],
'regex-all' => [
'name' => 'RegEx (All)',
'instance' => $strategy2
]
'default' => $strategy1,
'regex-all' => $strategy2
]);

$this->assertEquals(
Expand All @@ -53,19 +54,20 @@ public function getStrategyNamesSuccessfully()
*/
public function getStrategyInstancesSuccessfully()
{
/* @var $strategy1 StrategyInterface */
/* @var $strategy1 StrategyInterface|\PHPUnit_Framework_MockObject_MockObject */
$strategy1 = $this->createMock('\bitExpert\ForceCustomerLogin\Helper\Strategy\StrategyInterface');
$strategy1->expects($this->once())
->method('getName')
->willReturn('Static');
/* @var $strategy2 StrategyInterface|\PHPUnit_Framework_MockObject_MockObject */
$strategy2 = $this->createMock('\bitExpert\ForceCustomerLogin\Helper\Strategy\StrategyInterface');
$strategy2->expects($this->once())
->method('getName')
->willReturn('RegEx (All)');

$manager = new StrategyManager([
'default' => [
'name' => 'Static',
'instance' => $strategy1
],
'regex-all' => [
'name' => 'RegEx (All)',
'instance' => $strategy2
]
'default' => $strategy1,
'regex-all' => $strategy2
]);

$this->assertEquals(
Expand All @@ -82,19 +84,20 @@ public function getStrategyInstancesSuccessfully()
*/
public function getStrategyInstanceSuccessfully()
{
/* @var $strategy1 StrategyInterface */
/* @var $strategy1 StrategyInterface|\PHPUnit_Framework_MockObject_MockObject */
$strategy1 = $this->createMock('\bitExpert\ForceCustomerLogin\Helper\Strategy\StrategyInterface');
$strategy1->expects($this->once())
->method('getName')
->willReturn('Static');
/* @var $strategy2 StrategyInterface|\PHPUnit_Framework_MockObject_MockObject */
$strategy2 = $this->createMock('\bitExpert\ForceCustomerLogin\Helper\Strategy\StrategyInterface');
$strategy2->expects($this->once())
->method('getName')
->willReturn('RegEx (All)');

$manager = new StrategyManager([
'default' => [
'name' => 'Static',
'instance' => $strategy1
],
'regex-all' => [
'name' => 'RegEx (All)',
'instance' => $strategy2
]
'default' => $strategy1,
'regex-all' => $strategy2
]);

$this->assertFalse($manager->has('foo'));
Expand All @@ -107,19 +110,20 @@ public function getStrategyInstanceSuccessfully()
*/
public function getDefaultStrategyInstanceSuccessfully()
{
/* @var $strategy1 StrategyInterface */
/* @var $strategy1 StrategyInterface|\PHPUnit_Framework_MockObject_MockObject */
$strategy1 = $this->createMock('\bitExpert\ForceCustomerLogin\Helper\Strategy\StrategyInterface');
$strategy1->expects($this->once())
->method('getName')
->willReturn('Static');
/* @var $strategy2 StrategyInterface|\PHPUnit_Framework_MockObject_MockObject */
$strategy2 = $this->createMock('\bitExpert\ForceCustomerLogin\Helper\Strategy\StrategyInterface');
$strategy2->expects($this->once())
->method('getName')
->willReturn('RegEx (All)');

$manager = new StrategyManager([
'default' => [
'name' => 'Static',
'instance' => $strategy1
],
'regex-all' => [
'name' => 'RegEx (All)',
'instance' => $strategy2
]
'default' => $strategy1,
'regex-all' => $strategy2
]);

$this->assertFalse($manager->has('foo'));
Expand Down
15 changes: 14 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@ services:
- phpfpm
volumes: &appvolumes
- appdata:/var/www/html
- .:/var/www/html/app/code/bitExpert/ForceCustomerLogin:cached
- ./Api:/var/www/html/app/code/bitExpert/ForceCustomerLogin/Api:cached
- ./Block:/var/www/html/app/code/bitExpert/ForceCustomerLogin/Block:cached
- ./Controller:/var/www/html/app/code/bitExpert/ForceCustomerLogin/Controller:cached
- ./etc:/var/www/html/app/code/bitExpert/ForceCustomerLogin/etc:cached
- ./Helper:/var/www/html/app/code/bitExpert/ForceCustomerLogin/Helper:cached
- ./Model:/var/www/html/app/code/bitExpert/ForceCustomerLogin/Model:cached
- ./Observer:/var/www/html/app/code/bitExpert/ForceCustomerLogin/Observer:cached
- ./Plugin:/var/www/html/app/code/bitExpert/ForceCustomerLogin/Plugin:cached
- ./Repository:/var/www/html/app/code/bitExpert/ForceCustomerLogin/Repository:cached
- ./Setup:/var/www/html/app/code/bitExpert/ForceCustomerLogin/Setup:cached
- ./Ui:/var/www/html/app/code/bitExpert/ForceCustomerLogin/Ui:cached
- ./Validator:/var/www/html/app/code/bitExpert/ForceCustomerLogin/Validator:cached
- ./view:/var/www/html/app/code/bitExpert/ForceCustomerLogin/view:cached
- ./registration.php:/var/www/html/app/code/bitExpert/ForceCustomerLogin/registration.php
- ./var:/var/www/html/var
ports:
- 8000:80
Expand Down
20 changes: 12 additions & 8 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,21 @@
type="bitExpert\ForceCustomerLogin\Repository\WhitelistRepository" />

<!-- Helper -->
<type name="bitExpert\ForceCustomerLogin\Helper\Strategy\StaticMatcher">
<arguments>
<argument name="name" xsi:type="string">Static</argument>
</arguments>
</type>
<type name="bitExpert\ForceCustomerLogin\Helper\Strategy\RegExAllMatcher">
<arguments>
<argument name="name" xsi:type="string">RegEx (All)</argument>
</arguments>
</type>
<type name="bitExpert\ForceCustomerLogin\Helper\Strategy\StrategyManager">
<arguments>
<argument name="strategies" xsi:type="array">
<item name="default" xsi:type="array">
<item name="name" xsi:type="string">Static</item>
<item name="instance" xsi:type="object">bitExpert\ForceCustomerLogin\Helper\Strategy\StaticMatcher</item>
</item>
<item name="regex-all" xsi:type="array">
<item name="name" xsi:type="string">RegEx (All)</item>
<item name="instance" xsi:type="object">bitExpert\ForceCustomerLogin\Helper\Strategy\RegExAllMatcher</item>
</item>
<item name="default" xsi:type="object">\bitExpert\ForceCustomerLogin\Helper\Strategy\StaticMatcher</item>
<item name="regex-all" xsi:type="object">\bitExpert\ForceCustomerLogin\Helper\Strategy\RegExAllMatcher</item>
</argument>
</arguments>
</type>
Expand Down

0 comments on commit 807d2ec

Please sign in to comment.