Magento PHPUnit Integration

Overview

EcomDev

Magento PHPUnit Integration

Magento is a quite complex platform without built in unit test suite, so the code is not oriented on running tests over it.

This extension was created especially for resolving this problem and promoting test driven development practices in Magento developers community. It doesn't change core files or brake your Magento instalment database, because all the system objects are replaced during the run-time with the test ones and a separate database connection is used for tests.

System Requirements

  • PHP 5.3 or higher
  • PHPUnit 3.7.x
  • Magento CE1.4.x-1.7.x/PE1.9.x-PE1.10.x/EE1.9.x-1.12.x

Build Status

  • Latest Release: Master Branch
  • Development Branch: Development Branch

Documentation

Also you may follow our related blogposts.

Installation

  1. There are two ways of obtaining the extension:

    modman clone git://github.com/EcomDev/EcomDev_PHPUnit.git 
    • Add extension as dependency in your composer.json to install it from Magento Composer Repository
      {
            "require": {
               "ecomdev/ecomdev_phpunit": "*"
            }
      }
  2. Open your terminal and navigate to your magento directory for performing the following command, they are required to configure system for running the test suite

    # Shell scripts needs to be run from this directory
    cd $YOUR_MAGENTO_DIRECTORY/shell 
    # Specify your test database name and base url for controller tests
    php ecomdev-phpunit.php -a magento-config --db-name $DB_NAME --base-url http://your.magento.url/

    If you receive a warning on PHPUnit checks for optional packages, run the following command

    php ecomdev-phpunit.php -a fix-autoloader
  3. Run the unit tests first time for installing test database. It will take about 3 minutes.

     $ phpunit 
    
  4. If it shows that there was no tests found, it means that extension was successfully installed. If it shows some errors, then it means, that your customizations has install scripts that relay on your current database data and you should fix them. Or use your dev database as a base for the tests, but prior first time running the suite.

Issue Tracker

We use github issue tracker only for contributions management. If you want to post an issue please use our Issue Tracker

Contributions

If you want to take a part in improving our extension please create branches based on dev one.

###Create your contribution branch:

$ git checkout -b [your-name]/[feature] dev

Then submit them for pull request.

Comments
  • websites, store groups, stores break after first use

    websites, store groups, stores break after first use

    My test database, which was copied over from development, was setup with multiple stores and websites - this worked fine on all my tests, none of which had a multiple store configuration in their fixture file, until I tried to write a new test that included a new configuration because I was specifically testing it. Now when I run my tests I'm getting a SQL error on this specific test.

    Zend_Db_Statement_Exception: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'base' for key 'UNQ_MAG_CORE_WEBSITE_CODE'

    This is what my initial DB looked like for websites

    mysql> select * from mag_core_website;
    +------------+-------+--------------+------------+------------------+------------+
    | website_id | code  | name         | sort_order | default_group_id | is_default |
    +------------+-------+--------------+------------+------------------+------------+
    |          0 | admin | Admin        |          0 |                0 |          0 |
    |          1 | base  | Main Website |          0 |                1 |          1 |
    +------------+-------+--------------+------------+------------------+------------+
    

    and this was the part of the fixture file that is throwing the error:

    scope:
        website: # Initializes websites
            - website_id: 1
              code: "base"
              name: "Main Website"
              default_group_id: 1
    

    It appears I can make this work if I remove website_id: 1 from my test database and have the tests create/remove the entry, but then all my other tests start to break. The only way I see to fix that then will be to add the same multi store markup to all my fixture config files....which I have a lot and ideally don't want to do.

    Is there a better way to handle this? I could always use different ID's but if we ever need to adjust these in the DB for some reason and it creates a conflict again - then we'll have the same issue.

    I'm concerned I'm going to have the same results when I add groups and/or stores to the fixture as well...although I haven't gotten that far yet.

    opened by veilig2000 10
  • Is this project not maintained anymore?

    Is this project not maintained anymore?

    Hello,

    i guess there is actually a lot of work to do, because there are some open pull request and issues as well. Is anyone else going to maintain this project or is there a more active fork?

    Regards

    opened by azngeek 7
  • Issues in create test case for magento controller function

    Issues in create test case for magento controller function

    1. PHP Fatal error: Call to undefined method Mage_Core_Controller_Request_Http: :reset() in C:\xampp\htdocs\Cacommons\app\code\community\EcomDev\PHPUnit\Test\Ca se\Controller.php on line 1905

    Fatal error: Call to undefined method Mage_Core_Controller_Request_Http::reset() in C:\xampp\htdocs\Cacommons\app\code\community\EcomDev\PHPUnit\Test\Case\Contr oller.php on line 1905

    1. PHP Fatal error: Call to undefined method CA_Comparesort_Test_Controller_Index ::getMockBuilder() in C:\xampp\htdocs\Cacommons\app\code\community\EcomDev\PHPUn it\Test\Case.php on line 406

    Fatal error: Call to undefined method CA_Comparesort_Test_Controller_Index::getM ockBuilder() in C:\xampp\htdocs\Cacommons\app\code\community\EcomDev\PHPUnit\Tes t\Case.php on line 406

    opened by gadhavims 7
  • Wrong reference to composer package in README.md

    Wrong reference to composer package in README.md

    In README.md there is a wrong reference to composer package:

    • wrong one: "ecomdev/ecomdev_phpunit"
    • correct one: "ivanchepurnyi/ecomdev_phpunit"
    opened by aleron75 6
  • getModelMock

    getModelMock

    I'm trying to get a mock of my observer, but nothing happens:

    
    $mock = $this->getModelMock('mymodule/observer');
    
    

    phpunit just silently exits. Please help.

    opened by ExtensionsStore 5
  • Cannot find local.xml.phpunit file in app/etc directory

    Cannot find local.xml.phpunit file in app/etc directory

    composer.json:

    {
        "require-dev": {
            "ecomdev/ecomdev_phpunit": "*",
            "magento-hackathon/magento-composer-installer": "*"
        },
        "repositories": [
            {
                "type": "composer",
                "url": "http://packages.firegento.com"
            }
        ],
        "extra":{
            "magento-root-dir": ".",
            "magento-deploystrategy": "copy"
        }
    }
    

    In $root/shell/: php -f ecomdev-phpunit.php -- -a magento-config --db-user ... and I get: Cannot find local.xml.phpunit file in app/etc directory because it's missing.

    opened by jameswithers 5
  • Installation failed

    Installation failed

    I tried both modman and composer installation. Both failed.

    Composer: I added to composer.json:

        "require": {
            "ivanchepurnyi/ecomdev_phpunit": "*"
        }
    

    When I run "composer update" I get:

    Loading composer repositories with package information
    Updating dependencies (including require-dev)
    Your requirements could not be resolved to an installable set of packages.
    
      Problem 1
        - The requested package ecomdev/ecomdev_phpunit could not be found in any ve
    rsion, there may be a typo in the package name.
    
    Potential causes:
     - A typo in the package name
     - The package is not available in a stable-enough version according to your min
    imum-stability setting
       see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> f
    or more details.
    
    Read <http://getcomposer.org/doc/articles/troubleshooting.md> for further common
     problems.
    

    Modman I try to run

    modman clone git://github.com/EcomDev/EcomDev_PHPUnit.git
    

    I get

    /bin/modman: line 759: conditional binary operator expected
    /bin/modman: line 759: syntax error near `=~'
    /bin/modman: line 759: `if ! [[ "$action" =~ $REGEX_ACTION ]]; th
    en'
    
    opened by razbakov 5
  • Test for vfsStream is bad

    Test for vfsStream is bad

    If vfsStream is installed via composer dependency, the check fails, because composer installs it into vendor directory. Better try to init the class if this fails, throw the exception.

    opened by Schrank 5
  • cleanEntity does not remove

    cleanEntity does not remove

    When cleanEntity is called it only removes the main table content. Entries in the related tables for the different types is not necessarily deleted (ie through delete cascade). The patch manually introduces cleanTable calls for category and product.

    The patch additionally calls cleanEntity before the fixture is loaded to ensure that no existing data interferes with the data from the fixture.

    opened by fooman 5
  • Update composer.json for v0.3.0

    Update composer.json for v0.3.0

    I see from the README you've bumped phpunit up to 3.7.x. I have updated composer.json to reflect this.

    Also changed "magento-hackathon/magento-composer-installer": dependancy to "*"

    I think this is the preferred version to pin for magento-composer-installer //cc @vinai

    opened by rjocoleman 5
  • PDOException - Invalid catalog name: 1046 No database selected

    PDOException - Invalid catalog name: 1046 No database selected

    I have created a test suite that I ran successfully for months. I changed my laptop, and in my new environment I'm not able to execute the tests, as it fails with the error message below:

    'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' in www/shop/lib/Zend/Db/Statement/Pdo.php:228

    The database settings appear to be correct because when I first ran the test suite, it has successfully created the copy of the Magento database into the test database.

    Error message and stack trace:

    PHPUnit 3.6.12 by Sebastian Bergmann.
    
    F
    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' in www/shop/lib/Zend/Db/Statement/Pdo.php:228
    Stack trace:
    #0 www/shop/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
    #1 www/shop/lib/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
    #2 www/shop/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
    #3 www/shop/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECT `main_ta...', Array)
    #4 www/shop/lib/Varien/Db/Adapter/Pdo/Mysql.php(337): Zend_Db_Adapter_Pdo_Abstract->query('SELECT `main_ta...', Array)
    #5 www/shop/li in www/shop/lib/Zend/Db/Statement/Pdo.php on line 234
    

    (note: I shrunk down full paths in the error messages)

    It's definitely some local problem, however in it's current form it seems to be nearly impossible to fix, so I'd like to ask for some help on how to discover the root cause.

    opened by fulopattila122 5
  • Error while creating test db

    Error while creating test db

    So, till the 2nd step everything was proper. And when running phpunit, it throws an error :

    myProj/app/code/local/Dbaux/Customer/sql/customer_setup/upgrade-1.6.2.0.4-1.6.2.0.5.php" - SQLSTATE[42S02]: Base table or view not found: 1146 Table 'magento_test.customer_entity

    And when I looked into it, its because the script present in: app/code/core/Mage/Customer/sql/customer_setup which has files with lower version number are not run before running this script.

    So, did anyone else facing this issue, or knows how to resolve this issue.

    opened by torrtuga 0
  • Readme.md : not all the installations steps are clear enough

    Readme.md : not all the installations steps are clear enough

    For the step 2: you should add: "The database $DB_NAME should be already created"

    For the step 3 you should mention where phpunit must run from:

    1. Go to your magento root directory and run unit tests for the first time to install test database by running phpunit. It should take about 3 minutes. $ phpunit

    Spent some time to figure it out.

    opened by lycenok 1
  • Duplication of class Spyc

    Duplication of class Spyc

    We are currently using Composer to deploy Magento with its modules and also some other non-Magento related tools used for our CI cycle.

    We are currently using the dev branch of EcomDev_PHPUnit

    We recently run into an issue where PHPUnit crashed silently and it took us several hours before we found out that the source of the problem was that Spyc was being autoloaded via composer at the beginning of the PHPUnit process and later again when loading tests that use either a fixture or dataProvider (we are not using expectations, but I assume its the same case).

    The problem was that the first line of EcomDev_PHPUnit_Model_Yaml_Loader does a require_once 'Spyc/spyc.php' without considering the possibility of it already beind loaded gloabally.

    For our case, simply wrapping the require_once in if (!class_exists('Spyc', false)) fixed the issue.

    Looking closely at the 2 versions of Spyc, we noticed the one we have via Composer is version 0.6.2 while the one bundled with EcomDev_PHPUnit is version 0.4.5 so I am not 100% this fix will work in every scenario.

    I can provide a PR with this fix unless the Spyc version needs to be fixed to 0.4.5 for some reason.

    Comparing both versions, I only noticed differences in comments, internal functions naming, a bunch of dump stuff (not used by this module) and some additional controls when loading files. Nothing struck me as a possible compatibility breaker but I am not that familiar with Spyc.

    opened by barbazul 0
  • Fix broken headings in Markdown files

    Fix broken headings in Markdown files

    GitHub changed the way Markdown headings are parsed, so this change fixes it.

    See bryant1410/readmesfix for more information.

    Tackles bryant1410/readmesfix#1

    opened by bryant1410 0
  • Controller Unit Test Improvements

    Controller Unit Test Improvements

    While creating for another project a unit test for controller there appeared a problem: They were not starting, found problem at lib/EcomDev/PHPUnit/Mock/Proxy.php wich implements PHPUnit_Framework_MockObject_MockObject and is required to implement also function __phpunit_hasMatchers. I have raised a PR which fixes this issue.

    Also my IDE shows following error screenshot from 2016-12-06 15 54 38. Fortunately for me this has not stopped testing controller

    opened by Ig0r-M-magic42 4
Owner
EcomDev B.V.
Providing High Performance Web-shops since 2010
EcomDev B.V.
Mockery - Designed as a drop in alternative to PHPUnit's phpunit-mock-objects library

Mockery Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its c

Mockery 10.3k Jan 1, 2023
Magento PHPUnit Integration

Magento PHPUnit Integration Magento is a quite complex platform without built in unit test suite, so the code is not oriented on running tests over it

EcomDev B.V. 303 Dec 18, 2022
The most powerful and flexible mocking framework for PHPUnit / Codeception.

AspectMock AspectMock is not an ordinary PHP mocking framework. With the power of Aspect Oriented programming and the awesome Go-AOP library, AspectMo

Codeception Testing Framework 777 Dec 12, 2022
:computer: Parallel testing for PHPUnit

ParaTest The objective of ParaTest is to support parallel testing in PHPUnit. Provided you have well-written PHPUnit tests, you can drop paratest in y

null 2k Dec 31, 2022
vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with any unit test framework, like PHPUnit or SimpleTest.

vfsStream vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with

null 1.4k Dec 23, 2022
Rector upgrades rules for PHPUnit

Rector Rules for PHPUnit See available PHPUnit rules Install composer require rector/rector-phpunit Use Sets To add a set to your config, use Rector\P

RectorPHP 34 Dec 27, 2022
Add mocking capabilities to Pest or PHPUnit

This repository contains the Pest Plugin Mock. The Mocking API can be used in regular PHPUnit projects. For that, you just have to run the following c

PEST 16 Dec 3, 2022
A sample RESTful API in Laravel with PHPunit test.

Laravel PHP Framework URL | URI | Action |

Fasil 9 Jul 11, 2020
PHPUnit Application Architecture Test

PHPUnit Application Architecture Test Idea: write architecture tests as well as feature and unit tests Installation Install via composer composer requ

null 19 Dec 11, 2022
PHPUnit to Pest Converter

PestConverter PestConverter is a PHP library for converting PHPUnit tests to Pest tests. Before use Before using this converter, make sure your files

null 10 Nov 21, 2022
Allows the running of PHPUnit within ExpressionEngine

EE Unit Tests EE Unit Tests is an Add-on for ExpressionEngine that allows developers to execute unit tests from the Command Line. EE Unit Tests uses P

Eric Lamb 6 Jan 14, 2022
PHP libraries that makes Selenium WebDriver + PHPUnit functional testing easy and robust

Steward: easy and robust testing with Selenium WebDriver + PHPUnit Steward is a set of libraries made to simplify writing and running robust functiona

LMC s.r.o. 219 Dec 17, 2022
PHPUnit extension for database interaction testing.

This extension is no longer maintained DbUnit PHPUnit extension for database interaction testing. Installation Composer If you use Composer to manage

Sebastian Bergmann 224 Aug 20, 2022
Mock implementation of the Translation package, for testing with PHPUnit

PoP Translation - Mock Mock implementation of the Translation package, for testing with PHPUnit Install Via Composer composer require getpop/translati

PoP 1 Jan 13, 2022
The objective of ParaTest is to support parallel testing in PHPUnit

The objective of ParaTest is to support parallel testing in PHPUnit. Provided you have well-written PHPUnit tests, you can drop paratest in your project and start using it with no additional bootstrap or configurations!

null 2k Dec 31, 2022
Report high memory usage PHPUnit tests: Managed by opg-org-infra & Terraform

phpunit-memory-usage Report high memory usage PHPUnit tests: Managed by opg-org-infra & Terraform Configuration Add into the phpunit.xml extensions se

Ministry of Justice 2 Aug 4, 2022
PHPStan PHPUnit extensions and rules

PHPStan PHPUnit extensions and rules PHPStan PHPUnit This extension provides following features: createMock(), getMockForAbstractClass() and getMockFr

PHPStan 359 Dec 28, 2022
Satisfy the Type APIs for the WordPress schema when running PHPUnit tests

Satisfy the Type APIs for the WordPress schema when running PHPUnit tests

GraphQL API 1 Apr 12, 2022