phpunit with magento

35
Introduction to PHPUnit with Magento

Upload: tu-hoang

Post on 16-Jul-2015

267 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: PHPUnit with Magento

Introduction to PHPUnit with Magento

Page 2: PHPUnit with Magento

Hello!I am Hoang Ngoc Tu

This presentation will introduce fundamental concepts of unit testing in PHP and Magento ecommerce platform.

April 2015@hoangngoctu

Page 3: PHPUnit with Magento

Main Content

◎ 1. Unit Testing

◎ 2. PHPUnit

◎ 3. PHPUnit with Magento

Page 4: PHPUnit with Magento

1.Unit TestingWhat is it? Why should I need to know?

Page 5: PHPUnit with Magento

“All code is guilty, until proven

innocent.

Page 6: PHPUnit with Magento

What is Unit Testing?

◎ Is a software testing method by which individual units of source code are tested to determine whether they are fit for use.

◎ A unit is the smallest testable part of an application.

Page 7: PHPUnit with Magento

Unit Test Writing

◎ The general aim is to make sure every possible path through your code is executed at least once in a test.

◎ This means you need to write tests that exercise error conditions too.

◎ One test method per expected outcome per method tested.

Page 8: PHPUnit with Magento

Unit Testis an automated tool for

programmers

Page 9: PHPUnit with Magento

Unit Testcan’t replace

integration testing

Page 10: PHPUnit with Magento

Unit Testthe core of TDD - Test Driven Development

Page 11: PHPUnit with Magento

Test Driven Development

◎ Step 1: Write a test that expresses how you’ll use the code and what you need it to do.

◎ Step 2: Write enough code to get the test to pass, but no more. If you need more code, for example, to check for errors, first write another test to demonstrate that feature.

◎ Step 3: Refactor to clean up the code to remove redundancy and improve the design. Then re-run the tests to make sure you didn’t break anything.

Page 12: PHPUnit with Magento
Page 13: PHPUnit with Magento

At first sight of sceptical developers

◎ Time consuming

◎ Awkward, not intuitive approach

◎ Change the way we thought, the methods we worked

◎ It’s probably not in our “comfort zone”

Page 14: PHPUnit with Magento

In our experience

◎ Unit testing is generally not worth to bother for small, short term, one-and-done projects.

◎ Unit testing is crucial for enterprise system maintenance and long term projects.

Page 15: PHPUnit with Magento

Why should I know it?

◎ Unit tests prove that your code actually works.

◎ Find problems early

Page 16: PHPUnit with Magento

Why should I know it?

◎ You get a low-level regression-test suite.

◎ It allows you to make big changes to code quickly.

Page 17: PHPUnit with Magento

Why should I know it?

◎ Test-first forces you to plan before you code.

Page 18: PHPUnit with Magento

Why should I know it?

◎ In many cases, it’s faster than writing code without tests.

◎ Most of the effort we spend on code, we spend fixing it.

Page 19: PHPUnit with Magento
Page 20: PHPUnit with Magento

TDD for Legacy Code

◎ Capture behaviour of current code by adding black-box tests

◎ Refactor and add unit tests to legacy code bit-by-bit

◎ Increase test coverage over time

◎ All your new coding can be TDD

Page 21: PHPUnit with Magento

2.PHPUnitde-facto standard unit testing framework for PHP

Page 22: PHPUnit with Magento

PHPUnit

◎ Created by Sebastian Bergmann

◎ Homepage: https://phpunit.de/

◎ GitHub: https://github.com/sebastianbergmann/phpunit

Page 23: PHPUnit with Magento

PHPUnit Installation

# Manually way:

wget https://phar.phpunit.de/phpunit.phar

chmod +x phpunit.phar

mv phpunit.phar /usr/local/bin/phpunit

# Ubuntu way:

sudo apt-get install phpunit

# Composer way: check documentation for details

php composer.phar install

# Verify

phpunit --version

Page 24: PHPUnit with Magento

PHPUnit Fundamental Techniques

◎ Assertions

◎ Mocking○ Please note that final, private and static methods

cannot be mocked.

Page 25: PHPUnit with Magento

DEMO TIMEPlease check my github for code

https://github.com/tuhn/phpunit_demo

Page 26: PHPUnit with Magento

PHPUnit Plugins

◎ phpunit-clever-and-smart reorders your tests so that tests that failed in a previous run are executed first.

◎ phpunit-speedtrap reports slow-running tests right in your console.

◎ paratest can execute tests in parallel.

◎ phpunit-randomizer allows you to execute your tests in a random order.

Page 27: PHPUnit with Magento

PHPUnit Packages

◎ PHP_Invoker: a utility class for invoking callables with a timeout.

◎ DbUnit: support database interaction testing.

◎ PHPUnit_Selenium: lets you use the WebDriver API

Page 28: PHPUnit with Magento

PHPUnit and enterprise PHP

PHPUnit helps to transform PHP from a language for web projects into a serious and reliable enterprise platform

Page 29: PHPUnit with Magento

3.PHPUnit with MagentoWe will need an extension

Page 30: PHPUnit with Magento

Magento PHPUnit Integration

◎ Magento does not have built in unit test suite.

◎ Ecomdev_PHPUnit is an Magento extension to let us create unit test suite based on PHPUnit.

◎ It is basically a wrapper, or layer between Magento and PHPUnit.

◎ It also added its own specific methods for Magento testing.

Page 31: PHPUnit with Magento

Ecomdev_PHPUnit Installation

◎ GitHub: https://github.com/EcomDev/EcomDev_PHPUnit ◎ Obtain the extension: composer

◎ Download from GitHub:○ phpunit.xml○ app/etc/local.xml.phpunit

Page 32: PHPUnit with Magento

Ecomdev_PHPUnit Installation

# Shell scripts needs to be run from this directory

cd $YOUR_MAGENTO_DIRECTORY/shell

# Specify your test database name and base url

php ecomdev-phpunit.php -a magento-config --db-name $DB_NAME --base-url http://your.magento.url/

# Execute

bin/phpunit

Page 33: PHPUnit with Magento

DEMO TIMEand some exercises

Page 34: PHPUnit with Magento

Thanks!◎ This presentation is a very quick introduction

to Magento unit testing.

◎ Most content and practices are included in Demo section. Sorry slide readers.

◎ More in-depth presentations are coming.