Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/digitalpianism/testframework

A simple test framework repo for Magento 1
https://github.com/digitalpianism/testframework

Last synced: 3 months ago
JSON representation

A simple test framework repo for Magento 1

Awesome Lists containing this project

README

        

# Digital Pianism Test Framework

A simple test framework module that can be used to create unit and integration tests on Magento 1.

## Prepare your module for your tests

- Create a `Test` folder under your module folder
- Under this `Test` folder create the following files:

`phpunit.xml` with the following content:





.

Note that you can change most of the file here, the important part being the `bootstrap.php` file declaration.

`bootstrap.php` with the following content:

setResponse($stubResponse);

// Possible parameter
// Mage::app()->getRequest()->setParam('myparameter', 'myvalue');

// Use the controller helper
$controllerTestHelper = new \DigitalPianism_TestFramework_Helper_ControllerTestHelper($this);

// Dispatch a GET request
$controllerTestHelper->dispatchGetRequest('route', 'controller', 'action');
// Dispatch a POST request
//$controllerTestHelper->dispatchPostRequest('route', 'controller', 'action');
}

public function testSomething()
{
// Get the body
$body = Mage::app()->getResponse()->getBody(true);

// Get the headers
$headers = Mage::app()->getResponse()->getHeaders();

// Get a block
$block = Mage::app()->getLayout()->getBlock('block_name');

// Do your tests here
}
}

## Using test doubles

As you may have noticed, the `bootstrap.php` injects a custom instance of the config class `DigitalPianism_TestFramework_Model_Config`

This class declares several new methods:

- `setModelTestDouble` for model test doubles
- `setResourceModelTestDouble` for resource model test doubles
- `setHelperTestDouble` for helper test doubles

To use test doubles you can do the following in your `setUp` method.

Let's say you want to check what template ID is assigned to `Mage_Core_Model_Email_Template_Mailer` when the new customer account email is sent:

$mailer = Mage::getModel('core/email_template_mailer');
Mage::getConfig()->setModelTestDouble('core/email_template_mailer', $mailer);

$customer->sendNewAccountEmail();

$this->assertSame($expectedEmailTemplateId, $mailer->getTemplateId());

## Fixtures

You can create fixtures programmatically in your code.

A good recommendation to avoid having to manually delete your fixtures is to call `Mage::getSingleton('core/resource')->getConnection('core_write')->beginTransaction();` in the `setUp` method and then call `Mage::getSingleton('core/resource')->getConnection('core_write')->rollBack();` in the `tearDown` method