Skip to main content
17 votes
Accepted

What is the proper way for unit testing PHP7 code with PHPUnit 4.1 in Magento 2?

Using the bundled PHPUnit version, even if it's ancient, is probably the best way to go since that will allow running the tests for all modules together during CI. I think writing tests in a manner ...
Vinai's user avatar
  • 14k
16 votes
Accepted

Unit testing source models

This is a great thread, and I really like both Answers from @KAndy and @fschmengler. I would like to add some additional thoughts that I find valuable when asking a question such as "Should I test X?" ...
Vinai's user avatar
  • 14k
16 votes

Best practice for unit tests in Magento 1.9

Installation Since Magento 1 doesn't use composer out of the box, I don't think it makes a big difference if you install phpunit using composer or just download the phar version. If you already use ...
Vinai's user avatar
  • 14k
13 votes
Accepted

Best practice for unit tests in Magento 1.9

I faced the same problem a while back. I considered using the Ecomdev PHPUnit module but I find it hard to use and poorly documented (but I still love what Ivan does and his great contribution to the ...
Raphael at Digital Pianism's user avatar
12 votes
Accepted

How to get ScopeConfigInterface through the object manager of unit tests in magento 2?

I may be wrong here, but I think for unit tests you don't have to retrieve values from the data base. You can assume that the implementations of \Magento\Framework\App\Config\ScopeConfigInterface are ...
Marius's user avatar
  • 198k
12 votes
Accepted

Magento 2 : When to use Object Manager in Unit tests?

The short answer is, yes, I would mock the entire Context class. $mockScopeConfig = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class); $mockContext = $this->getMock(\...
Vinai's user avatar
  • 14k
11 votes
Accepted

How to properly use getCollectionMock

TL;DR You are using the getCollectionMock() method correctly. The problem is that it only mocks the getIterator() method. Your code however calls toOptionArray() on the collection, not getIterator(). ...
Vinai's user avatar
  • 14k
9 votes
Accepted

Unit test Issue on getting value

The problem lies in this line in setUp: $this->requestMock->method('getHeader')->willReturn($this->httpHeader); Here you instruct the request mock to return your HttpHeaderInterface ...
Fabian Schmengler's user avatar
8 votes

Simulating Order Data For a Unit Test

You should distinguish between unit and integration tests. Unit Test In a unit test the class under test should get the order data from a collaborator that you can inject as a mock, for example ...
Vinai's user avatar
  • 14k
7 votes
Accepted

Magento 2 : Unit test with mock data don't work. Why?

Given the information you posted, I suspect that the class \Custom\Shipping\Helper\Config implements (or inherits) public function __call() from a parent class and that the method getProductTypes() is ...
Vinai's user avatar
  • 14k
7 votes
Accepted

Generate factory or proxy in unit tests: "ReflectionException: Class ...Factory does not exist"

The easiest way to deal with that is to run compilation before running tests: bin/magento setup:di:compile The other way is to explicitly define methods for the factory mock eg. instead of doing ...
Wojtek Naruniec's user avatar
7 votes
Accepted

How to mock a Context object

You can test your class like this: public function testSomething() { $contextMock = $this->getMockBuilder(\Magento\Framework\App\Helper\Context::class) ->...
Marius's user avatar
  • 198k
7 votes

Magento2 unit tests doesn't run using vendor/bin/phpunit or vendor/phpunit/phpunit/phpunit

./vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/VendorName/ModuleName/Test/Unit Try Below Command instead of yours ./vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml....
Gaurav Manwani's user avatar
6 votes

Unit testing source models

In my opinion, there is no general answer to "write unit tests for source models, yes or no" I've written unit tests for source models, but those were dynamic source models that fetched external data ...
Fabian Schmengler's user avatar
6 votes
Accepted

Unit Test or Integration Test for this kind of functions?

In addition to @raphael-at-digital-pianism's great response. I find the resolve method rather complex, so I would want test coverage. $inputParams = $this->serviceInputProcessor->process( \...
Vinai's user avatar
  • 14k
5 votes
Accepted

Magento 2 - run phpunit to test

The error probably is caused by some other test initializing an object with the object manager during construction, which is trying to mock the arguments. This shouldn't happen unless the tests ...
Vinai's user avatar
  • 14k
5 votes

Generate factory or proxy in unit tests: "ReflectionException: Class ...Factory does not exist"

Also you may use something like this private function getMockupFactory($instanceName) { /** Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ $objectManager = $this->...
Eduard Melnyk's user avatar
5 votes

Generate factory or proxy in unit tests: "ReflectionException: Class ...Factory does not exist"

The problem originates from PHPUnit mocking library, as it cannot autoload the needed class. If you take a look into Magento dev repo, it setups Autoloader catcher, that generates a class when it is ...
Ivan Chepurnyi's user avatar
5 votes

Unit testing source models

How should I unit test those? I think you should not. Add code to system increase support and maintenance cost but testing process should be LEAN. More over this code should not exists. I believe ...
KAndy's user avatar
  • 20.9k
5 votes
Accepted

Instantiating models for unit tests

Instantiating the class under test Unit tests should be a simple as possible because that helps with understanding the code being tested as well as debugging. Lets have a look at the purpose of the \...
Vinai's user avatar
  • 14k
5 votes
Accepted

How to write tests when closures are involved

To mock callables, I usually mock __invoke: $callbackMock = $this->getMockBuilder(\stdClass::class) ->setMethods(['__invoke']) ->getMock(); $callbackMock->expects($this->once()...
Fabian Schmengler's user avatar
4 votes
Accepted

Unit tests custom module not executed - Magento 2

Well your phpunit.xml file specifies a suffix: <directory suffix="Test.php"> So every tests you run must have this suffix and it doesn't seem like your test does. To fix that, rename Tagalys\...
Raphael at Digital Pianism's user avatar
4 votes
Accepted

How to write Unit Test for a function which uses a variable defined in Constructor

In unit tests you test components isolated. That means that you should construct the objects under test yourself and provide input by the test, not by some external method. Example: /** * @...
Fabian Schmengler's user avatar
4 votes

Magento 2 : When to use Object Manager in Unit tests?

To answer your question I will describe why ObjectManagerhelper class was developed by Magento team: When M2 development started all classes use Mage::* methods and we start increase coverage with ...
KAndy's user avatar
  • 20.9k
4 votes
Accepted

Magento Test Framework(MTF) vs Selenium with Java - Magento 2.0.1

The MTF uses Selenium under the hood so it's not really an either/or question. The biggest benefit of using the MTF is that you can use the tests that someone else has written already vs. if you used ...
Kristof at Fooman's user avatar
4 votes
Accepted

Magento 2 : mocking `getParam()` in unit test not working

The getParam method accepts 2 parameters, the second one defaulting to null public function getParam($key, $defaultValue = null); returnValueMap works like this. the first N-1 values in the array ...
Marius's user avatar
  • 198k
4 votes

Magento 2: how to use the CommandTester

You are not getting the real behavior because the \Magento\Framework\TestFramework\Unit\Helper\ObjectManager creates a mock for each constructor argument dependency, not the real thing. The ...
Vinai's user avatar
  • 14k
4 votes
Accepted

Trying to mock Store getLocale, getting "Method cannot be configured..."

You have a typo in the method name. The mocked method is getLocaleCode (with an e in Locale), and the expectation is set for a method getLocalCode (without the e). Small side note, somewhat unrelated ...
Vinai's user avatar
  • 14k
4 votes

How to get base url in Unit Test?

I think you have to look into the core Magento unit test code how they use and get base URL. I am not sure about this is what I shared is exactly you want, but I think it will help you. Look into ...
Chirag Patel's user avatar
  • 6,136
3 votes
Accepted

How to mock a class property in Magento 2 Unit Test

You would need to address this one level up. So if for example <?php namespace Fooman/Namespace; class Example { protected $product; public function __construct( Magento\...
Kristof at Fooman's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible