Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Copy of Unit Testing in Magento

Unit Testing in Magento
by

Den4ik Den4ik

on 29 November 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Copy of Unit Testing in Magento

First test: Hello world test
First test: Hello world test
The issue is that if the tests are from different modules then it wouldn’t be a good idea for a test class to extend a class from other module (as that module might be removed in time). Not only that, but it wouldn’t really be easy to follow your tests if that happens.
So we can create a test helper class , which would have test helper methods for any test class which needs them. In this way we also separate the actual test methods from test helper methods.
7th and 8th test: Using Test helpers
The way we checked the value in the test was:
$this->assertEquals($expectedValue, $n->getValue());
If the method getValue() is not correct, then the test for the method __construct() is not correct.

In order to not make the test for the method __construct dependent on the getValue method we could try to write the above code like this:
$this->assertEquals($expectedValue, $n->_value);
But we can’t access the _value property because it is protected.

Here it comes in help the reflection. Using reflection of the object you can access its private and protected properties and methods and other info which you might need in a test. Here’s the code for our situation:
$value = EcomDev_Utils_Reflection::getRestrictedPropertyValue($n, '_value');
$this->assertEquals($expectedValue, $value);
Try to not use it too much though, it has the special ability to make tests run slower.
Third test: Data Providers
An introduction to unit testing in Php, examples and helpful tips for Magento developers
Unit Testing in Magento
Questions?
To get all the files used in this presentation e-mail marcel.moldovan@freshbyte.ro .
Unit Testing in Magento
I doubt you guess what this does when running:
phpunit --debug --group testNow UnitTests.php
Final test …
We’re ready now to make the changes to our 5th and 6th test files.
First, make the Folder3 test class extend from the new abstract class, instead of EcomDev_PHPUnit_Test_Case.
Remember Test1 (5th test) and Test2 (6th test) extend the Folder3 test class, so we can call the method getTestHelper from each of those tests. The method getTestHelper() returns a singleton object of the helper class for the module the test is written in.
Note the @method annotation helps the IDE know the returned type of the method getTestHelper.
7th and 8th test: Using Test helpers
How to do all this efficiently?
Create a new module PHPUnit
Add a new file which will be the default helper
Add in the config node config/phpunit/suite/testhelper/default the default helper class
7th and 8th test: Using Test helpers
This is the abstract class having the setUp method.
5th and 6th tests: Using an Abstract test case
For more details and examples of how-to-tests read the PHPUnit manual: http://www.phpunit.de/manual/current/en/
and the EcomDev manual : http://www.ecomdev.org/wp-content/uploads/2011/05/EcomDev_PHPUnit-0.2.0-Manual.pdf
More testing
Add method test___construct_WhenParameterIsNotIntOrString in the test class.
4th test: Expected exception
Model class methods: __construct, getValue
Third test: Data Providers
It’s good to know what assertions you can use, but you don’t have to learn them, just know where to look for them.
Full list: http://otroblogmas.com/wp-content/uploads/2011/06/PHPUnit-Cheat-Sheet.pdf
Assertions
Test class methods: onNotSuccessfulTest, assertPostConditions, tearDown, tearDownAfterclass
Second test: Template methods
phpunit --group Aaanamespace_Mymodule UnitTests.php
The test passed.
First test: Hello world test
Your test model class. Use assertEquals to check if the expected value equals the real value.
First test: Hello world test
Config.xml
For letting system know that your module contains unit tests you just need to add few lines (lines 15-21) into your module config.xml file.
First test: Hello world test
Folder and files structure of your module
After installing EcomDev, you should have the namespace EcomDev in your community folder
also UnitTests.php is probably somewhere in your project folder
EcomDev
After installing PHPUnit you should have a similar structure of folders and files like this :
PHPUnit
Because bugs should be discovered as soon as possible.
The cost to eliminate bugs is greater when the bugs are discovered afterwards in the next stages of the project.
Why unit testing?
Wikipedia: “is a method of testing that verifies the individual units of source code are working properly”
What is unit testing?
Add the test helper for the module Mymodule and a new method. Note that the test helper class extends the default test helper class.
Any test from the module Mymodule can get the helper instance by simply calling the method getTestHelper().
7th and 8th test: Using Test helpers
How to do all this efficiently?

Add a new abstract class extending EcomDev_PHPUnit_Test_Case. All your future tests should extend this new class.
Add method _getDefaultHelper()
Add methods getTestHelperForModule() and getTestHelper()
7th and 8th test: Using Test helpers
How to do all this efficiently?

Add in the config node config/phpunit/suite/testhelper/default the default helper class
Add a new abstract class extending EcomDev_PHPUnit_Test_Case. All your future tests should extend this new class.
Add method _getDefaultHelper()
7th and 8th test: Using Test helpers
How to do all this efficiently?
Create a new module PHPUnit
Add a new file which will be the default helper
7th and 8th test: Using Test helpers
How to do all this efficiently?
Create a new module PHPUnit
7th and 8th test: Using Test helpers
The abstract class has a protected “helper” method _prepareObject().
Usually, in writing tests we might have some protected methods which are called from more test methods, but also we might need these methods from other test classes.
What we can do is use the same idea like before having an abstract class and all tests needing those methods to extend it.
7th and 8th test: Using Test helpers
For the next two tests, it’s not something really EcomDev related, it’s just some tips how to write test cases.
Let’s say you have two test classes Test1.php and Test2.php, and both of them have the same setUp and some other common behaviour.
5th and 6th tests: Using an Abstract test case
phpunit --debug --group Aaanamespace_Mymodule UnitTests.php
Result : all passed.
4th test: Expected exception
Same model class from 3rd test.
4th test: Expected exception
Third test: Data Providers
Test class methods: test___construct_UsingDataProvider, dataProviderTest___construct_UsingDataProvider
Third test: Data Providers
Test class methods: test1, test2, test3
Second test: Template methods
Test class methods: setUpBeforeClass, setUp and assertPreConditions
Second test: Template methods
Your model class
EcomDev is PHPUnit + few more files for Magento
EcomDev ≈ unit testing in Magento
EcomDev
Part of xUnit family (JUNIt, Sunit, …)
PHPUnit ≈ unit testing in php
Created by Sebastian Bergmann
PHPUnit
Why?
But this is the real output:
Second test: Template methods
But this is the real output:
I would expect this:
Second test: Template methods
Test2 uses the prepareObject() method from the test helper.
Test1 uses the prepareObject() method from the test helper.
7th and 8th test: Using Test helpers
Test2 uses the _prepareObject() method from the abstract class.
Test1 uses the _prepareObject() method from the abstract class.
7th and 8th test: Using Test helpers
Test2 extends Folder3 class.
Test1 extends Folder3 class.
5th and 6th tests: Using an Abstract test case
Question: How to efficiently write the test cases in order to write the setUp only once?
Solution: Create a new folder PHPUnit/Abstract under Test folder, and create the structure of the folders where both tests are in, then create an abstract test case with the name of the last common folder there.
Before:
After:
5th and 6th tests: Using an Abstract test case
Test2 class has the same setUp and another test method.
Test1 class has the setUp method and a test method.
5th and 6th tests: Using an Abstract test case
class Mynamespace_PHPUnit_Test_Suite_Group extends EcomDev_PHPUnit_Test_Suite_Group
{
// rewrite any methods …
}
<phpunit>
<suite>
<test_suite>MyModule_PHPUnit_Test_Suite_Group</test_suite>
</phpunit>
<phpunit>
<suite>
<!-- The names of directories inside Test for recognizion of tests per group -->
<groups>
<models>Model</models>
<helpers>Helper</helpers>
<blocks>Block</blocks>
<config>Config</config>
<controllers>Controller</controllers>
</groups>
<!-- Test suite that will be used for creation of each of the tests -->
<test_suite>EcomDev_PHPUnit_Test_Suite_Group</test_suite>
….
</phpunit>
Contains the configuration of the PHPUnit module
In Magento, you can rewrite any config node and use your own values for it. This means, all the configuration settings for PHPUnit can be rewritten and easily changed.
Config.xml
For example, in the config.xml of some other module from your project you could rewrite the config node phpunit/suite/test_suite with the name of other class and add change its behaviour. Make sure to extend the old class if needed.







'
Full transcript