Skip to content

Instantly share code, notes, and snippets.

@mmarum-sugarcrm
Last active January 12, 2016 06:39
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 mmarum-sugarcrm/a139cec1de7ba4c31226 to your computer and use it in GitHub Desktop.
Save mmarum-sugarcrm/a139cec1de7ba4c31226 to your computer and use it in GitHub Desktop.
PHPUnit tests for example AccountsOnSave logic hooks
<?php
/*
* Copyright 2015 SugarCRM Inc.
*/
require_once 'custom/modules/Accounts/AccountsOnSaveHooks.php';
/**
* Example tests for our custom Logic Hook.
*/
class AccountsOnSaveHooksTest extends Sugar_PHPUnit_Framework_TestCase
{
private $bean; //Accounts bean
private $hooks; //Hooks class
/**
* Set up before each test
*/
public function setUp(){
parent::setUp();
$this->bean = BeanFactory::newBean('Accounts');
$this->hooks = new AccountsOnSaveHooks();
/**
* Use SugarTestHelper to set up only those Sugar global values that are needed.
* Framework will tear these down automatically after each test.
*/
SugarTestHelper::setUp("beanList");
}
/**
* Example that relies on SugarTestHelper
*/
public function testBeanListLoaded(){
global $beanList;
$this->assertNotEmpty($beanList["Accounts"], "This test relies on Accounts bean.");
}
/**
* Verify that logic hook changes Industry value when necessary
*/
public function testSetIndustryForAnalystAccount_ChangingIndustry()
{
$bean = $this->bean;
$bean->account_type = "Analyst";
$this->assertNotEquals($bean->industry, "Banking");
//Mock event
$this->hooks->setIndustryForAnalystAccount($bean, "before_save", array());
//Verify that Banking industry is now set
$this->assertEquals($bean->industry, "Banking");
$this->assertEquals($bean->account_type, "Analyst");
$bean->industry = "Apparel";
$this->hooks->setIndustryForAnalystAccount($bean, "before_save", array());
//Verify that Banking industry is reset
$this->assertEquals($bean->industry, "Banking");
$this->assertEquals($bean->account_type, "Analyst");
}
/**
* Verify that Industry value is ignored by logic hook for other changes
*/
public function testSetIndustryForAnalystAccount_IgnoreIndustry()
{
$bean = $this->bean;
$bean->account_type = "Reseller";
$this->assertNotEquals($bean->industry, "Banking");
//Mock event
$this->hooks->setIndustryForAnalystAccount($bean, "before_save", array());
//Verify that Banking industry is ignored
$this->assertNotEquals($bean->industry, "Banking");
$this->assertEquals($bean->account_type, "Reseller");
$bean->industry = "Apparel";
//Mock event
$this->hooks->setIndustryForAnalystAccount($bean, "before_save", array());
//Verify that industry is stays as Apparel
$this->assertEquals($bean->industry, "Apparel");
$this->assertEquals($bean->account_type, "Reseller");
}
/**
* Test that our logic hook can handle unexpected values
*/
public function testSetIndustryForAnalystAccount_UnexpectedValues()
{
$bean = $this->bean;
//Expected values are not set on bean
unset($bean->industry);
unset($bean->account_type);
$this->hooks->setIndustryForAnalystAccount($bean, "before_save", array());
$this->assertEmpty($bean->industry);
$this->assertEmpty($bean->account_type);
//Unexpected data type
$bean->account_type = -1;
$this->hooks->setIndustryForAnalystAccount($bean, "before_save", array());
$this->assertEmpty($bean->industry);
$this->assertEquals($bean->account_type, -1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment