Skip to content

Instantly share code, notes, and snippets.

@gpaddis
Last active June 16, 2018 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gpaddis/04643eea62dff1d6cb04cc500eea859c to your computer and use it in GitHub Desktop.
Save gpaddis/04643eea62dff1d6cb04cc500eea859c to your computer and use it in GitHub Desktop.
Magento 1 - Module Unit Testing
<?php
class Some_Module_Helper_Data extends Mage_Core_Helper_Abstract
{
/**
* This method interacts with the database, so it will be mocked for the unit test.
*/
public function queryDatabase()
{
return $this->getSomeDbData();
}
/**
* This method changes the data fetched from the DB. We will test its logic.
*
* @return integer
*/
public function multiply()
{
$dbData = $this->queryDatabase();
return $dbData * 10;
}
}
#!/bin/bash
for i in $(seq 5 7);
do
if ! type "phpunit$i" > /dev/null; then
sudo wget -O /usr/local/bin/phpunit$i https://phar.phpunit.de/phpunit-$i.phar
sudo chmod +x /usr/local/bin/phpunit$i
echo -e "Installed phpunit$i.\n"
fi
done
<?php
use PHPUnit\Framework\TestCase;
/**
* To run the test suite: phpunit6 --colors=auto Test*
*/
class TestHelper extends TestCase
{
public function setUp()
{
/**
* We have to mock the parent class if we want to break the inheritance chain
* and avoid loading the whole framework.
*
* See this comment by Sebastian Bergmann (creator of phpunit):
* https://stackoverflow.com/a/28303890/7874784
*/
$this->getMockBuilder('Mage_Core_Helper_Abstract')->getMock();
/**
* Now that we have mocked the parent, we can load the helper directly.
*/
require_once dirname(__FILE__) . '/Helper.php';
/**
* We will replace the return value of queryDatabase during the test.
*/
$this->helper = $this->getMockBuilder(Some_Module_Helper_Data::class)
->setMethods(['queryDatabase'])
->getMock();
}
/** @test */
public function it_multiplies_the_db_data()
{
/**
* Here we set the method queryDatabase() to return dummy data for our test,
* then we test the logic inside the method multiply().
*/
$this->helper->method('queryDatabase')->willReturn(5);
$this->assertEquals(50, $this->helper->multiply());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment