Skip to content
This repository was archived by the owner on Nov 6, 2021. It is now read-only.

Commit 9b558db

Browse files
author
Florian Horn
committed
Added unit tests
1 parent 16f51e4 commit 9b558db

19 files changed

+1397
-40
lines changed

Console/Command/FuzzyfyrCommand.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
*/
2626
class FuzzyfyrCommand extends Command
2727
{
28+
/**
29+
* Codes
30+
*/
31+
const SUCCESS = 0;
32+
const ERROR_PRODUCTION_MODE = 127;
33+
2834
/**
2935
* Flags
3036
*/
@@ -174,7 +180,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
174180
// this may mess up to much if it would run without any safety checks
175181
$output->writeln('You really should not use this command in production mode.');
176182
$output->writeln('If it is really what you want, switch to default or developer mode first.');
177-
return false;
183+
return self::ERROR_PRODUCTION_MODE;
178184
}
179185

180186
// Set area code
@@ -194,6 +200,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
194200
$this->eventManager->dispatch('aid_content_fuzzyfyr_event', ['configuration' => $configuration]);
195201

196202
$output->writeln('Finished content fuzzyfy');
203+
204+
return self::SUCCESS;
197205
}
198206

199207
/**

Model/ConfigurationFactory.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* This file is part of the Content Fuzzyfyr module for Magento2.
4+
*
5+
* (c) All.In Data GmbH
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace AllInData\ContentFuzzyfyr\Model;
12+
13+
/**
14+
* Class ConfigurationFactory
15+
* @package AllInData\ContentFuzzyfyr\Model
16+
*/
17+
class ConfigurationFactory
18+
{
19+
/**
20+
* Object Manager instance
21+
*
22+
* @var \Magento\Framework\ObjectManagerInterface
23+
*/
24+
protected $_objectManager = null;
25+
26+
/**
27+
* Instance name to create
28+
*
29+
* @var string
30+
*/
31+
protected $_instanceName = null;
32+
33+
/**
34+
* Factory constructor
35+
*
36+
* @param \Magento\Framework\ObjectManagerInterface $objectManager
37+
* @param string $instanceName
38+
*/
39+
public function __construct(
40+
\Magento\Framework\ObjectManagerInterface $objectManager,
41+
$instanceName = '\\AllInData\\ContentFuzzyfyr\\Model\\Configuration'
42+
) {
43+
$this->_objectManager = $objectManager;
44+
$this->_instanceName = $instanceName;
45+
}
46+
47+
/**
48+
* Create class instance with specified parameters
49+
*
50+
* @param array $data
51+
* @return \AllInData\ContentFuzzyfyr\Model\Configuration
52+
*/
53+
public function create(array $data = array())
54+
{
55+
return $this->_objectManager->create($this->_instanceName, $data);
56+
}
57+
}

Observer/CustomersObserver.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ protected function run(Configuration $configuration)
6868
/**
6969
* @param Configuration $configuration
7070
* @param \Magento\Customer\Model\Customer $customer
71-
* @return \Magento\Customer\Model\Customer
7271
*/
7372
protected function doUpdate(Configuration $configuration, \Magento\Customer\Api\Data\CustomerInterface $customer)
7473
{
@@ -81,14 +80,12 @@ protected function doUpdate(Configuration $configuration, \Magento\Customer\Api\
8180

8281
$customer->setLastName($configuration->getDummyContentText());
8382

84-
$addresses = $customer->getAddressesCollection();
83+
$addresses = $customer->getAddresses();
8584
foreach ($addresses as $address) {
8685
/** @var AddressInterface $address */
8786
$address->setStreet([$configuration->getDummyContentText()]);
8887
$address->setCity($configuration->getDummyContentText());
8988
}
90-
$customer->setAddresses($address);
91-
92-
return $customer;
89+
$customer->setAddresses($addresses);
9390
}
9491
}

Observer/FuzzyfyrObserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected function isValid(Configuration $configuration)
4343
public function execute(\Magento\Framework\Event\Observer $observer)
4444
{
4545
/** @var Configuration $configuration */
46-
$configuration = $observer->getData('configuration');
46+
$configuration = $this->getConfigurationByEvent($observer);
4747

4848
if (!$this->isValid($configuration)) {
4949
return;

Observer/ProductsObserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(ProductCollectionFactory $productCollectionFactory,
4343
*/
4444
public function isValid(Configuration $configuration)
4545
{
46-
return $configuration->isApplyToCategories();
46+
return $configuration->isApplyToProducts();
4747
}
4848

4949
/**

Observer/UsersObserver.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ protected function run(Configuration $configuration)
6767
/**
6868
* @param Configuration $configuration
6969
* @param \Magento\User\Model\User $user
70-
* @return \Magento\User\Model\User
7170
*/
7271
protected function doUpdate(Configuration $configuration, \Magento\User\Model\User $user)
7372
{
@@ -79,7 +78,5 @@ protected function doUpdate(Configuration $configuration, \Magento\User\Model\Us
7978
);
8079

8180
$user->setLastName($configuration->getDummyContentText());
82-
83-
return $user;
8481
}
8582
}

Test/Unit/AbstractTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* This file is part of the Content Fuzzyfyr module for Magento2.
4+
*
5+
* (c) All.In Data GmbH
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace AllInData\ContentFuzzyfyr\Test\Unit;
12+
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Class AbstractTest
17+
* @package AllInData\ContentFuzzyfyr\Test\Unit
18+
*/
19+
abstract class AbstractTest extends TestCase
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function getTestEntity($className, array $arguments = [])
25+
{
26+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
27+
return $objectManager->getObject($className, $arguments);
28+
}
29+
30+
/**
31+
* @param string $instanceName
32+
* @param mixed $mock
33+
* @return \PHPUnit\Framework\MockObject\MockObject
34+
*/
35+
public function getFactoryAsMock($instanceName, $mock = null)
36+
{
37+
$factoryFullName = $instanceName . 'Factory';
38+
$parts = explode('\\', $factoryFullName);
39+
$factoryClassName = array_pop($parts);
40+
41+
if (!$mock) {
42+
$mock = $this->getMockBuilder($instanceName)
43+
->disableOriginalConstructor()
44+
->getMock();
45+
}
46+
47+
$factory = $this->getMockBuilder($factoryFullName)
48+
->disableOriginalConstructor()
49+
->setMockClassName($factoryClassName)
50+
->setMethods(['create'])
51+
->getMock();
52+
53+
$factory->expects($this->any())
54+
->method('create')
55+
->willReturn($mock);
56+
57+
return $factory;
58+
}
59+
}

Test/Unit/Console/Command/FuzzyfyrCommandTest.php

Lines changed: 99 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,123 @@
1010

1111
namespace AllInData\ContentFuzzyfyr\Test\Unit\Console\Command;
1212

13-
use Symfony\Component\Console\Tester\CommandTester;
13+
use AllInData\ContentFuzzyfyr\Model\Configuration;
14+
use AllInData\ContentFuzzyfyr\Test\Unit\AbstractTest;
15+
use Magento\Framework\App\State;
16+
use Magento\Framework\EntityManager\EventManager;
17+
use AllInData\ContentFuzzyfyr\Model\ConfigurationFactory;
1418
use AllInData\ContentFuzzyfyr\Console\Command\FuzzyfyrCommand;
19+
use PHPUnit\Framework\MockObject\MockObject;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Console\Output\OutputInterface;
1522

16-
class FuzzyfyrCommandTest extends \PHPUnit\Framework\TestCase
23+
/**
24+
* Class FuzzyfyrCommandTest
25+
* @package AllInData\ContentFuzzyfyr\Test\Unit\Console\Command
26+
*/
27+
class FuzzyfyrCommandTest extends AbstractTest
1728
{
1829
/**
19-
* @var FuzzyfyrCommand
30+
* @test
2031
*/
21-
private $command;
32+
public function runSuccessfully()
33+
{
34+
$state = $this->getState();
35+
36+
$eventManager = $this->getEventManager();
37+
38+
$configuration = $this->getMockBuilder(Configuration::class)->getMock();
39+
$configurationFactory = $this->getConfigurationFactory();
40+
$configurationFactory->expects($this->once())
41+
->method('create')
42+
->willReturn($configuration);
43+
44+
$command = new FuzzyfyrCommand(
45+
$state,
46+
$eventManager,
47+
$configurationFactory
48+
);
49+
50+
$input = $this->getInput();
51+
$output = $this->getOutput();
2252

23-
public function setUp()
53+
$this->assertEquals(FuzzyfyrCommand::SUCCESS, $command->run($input, $output));
54+
}
55+
/**
56+
* @test
57+
*/
58+
public function runFailsInProductionMode()
2459
{
25-
$this->command = new FuzzyfyrCommand();
60+
$state = $this->getState();
61+
$state->expects($this->any())
62+
->method('getMode')
63+
->willReturn(\Magento\Framework\App\State::MODE_PRODUCTION);
64+
65+
$eventManager = $this->getEventManager();
66+
67+
$configurationFactory = $this->getConfigurationFactory();
68+
$configurationFactory->expects($this->never())
69+
->method('create');
70+
71+
$command = new FuzzyfyrCommand(
72+
$state,
73+
$eventManager,
74+
$configurationFactory
75+
);
76+
77+
$input = $this->getInput();
78+
$output = $this->getOutput();
79+
80+
$this->assertEquals(FuzzyfyrCommand::ERROR_PRODUCTION_MODE, $command->run($input, $output));
2681
}
2782

28-
public function testExecuteAnonymous()
83+
/**
84+
* @return MockObject|State
85+
*/
86+
private function getState()
2987
{
30-
$commandTester = new CommandTester($this->command);
31-
$commandTester->execute(
32-
[
33-
'-a' => true
34-
]
35-
);
88+
return $this->getMockBuilder(State::class)
89+
->disableOriginalConstructor()
90+
->getMock();
91+
}
3692

37-
$this->assertContains('Hello Anonymous!', $commandTester->getDisplay());
93+
/**
94+
* @return MockObject|EventManager
95+
*/
96+
private function getEventManager()
97+
{
98+
return $this->getMockBuilder(EventManager::class)
99+
->disableOriginalConstructor()
100+
->getMock();
38101
}
39102

40-
public function testExecuteName()
103+
/**
104+
* @return MockObject|ConfigurationFactory
105+
*/
106+
private function getConfigurationFactory()
41107
{
42-
$commandTester = new CommandTester($this->command);
43-
$commandTester->execute(
44-
[
45-
FuzzyfyrCommand::NAME_ARGUMENT => 'Test'
46-
]
47-
);
108+
return $this->getMockBuilder(ConfigurationFactory::class)
109+
->disableOriginalConstructor()
110+
->getMock();
111+
}
48112

49-
$this->assertContains('Hello Test!', $commandTester->getDisplay());
113+
/**
114+
* @return MockObject|InputInterface
115+
*/
116+
private function getInput()
117+
{
118+
return $this->getMockBuilder(InputInterface::class)
119+
->disableOriginalConstructor()
120+
->getMock();
50121
}
51122

52123
/**
53-
* @expectedException \InvalidArgumentException
54-
* @expectedExceptionMessage Argument name is missing
124+
* @return MockObject|OutputInterface
55125
*/
56-
public function testExecuteError()
126+
private function getOutput()
57127
{
58-
$commandTester = new CommandTester($this->command);
59-
$commandTester->execute([]);
128+
return $this->getMockBuilder(OutputInterface::class)
129+
->disableOriginalConstructor()
130+
->getMock();
60131
}
61-
}
132+
}

0 commit comments

Comments
 (0)