Skip to content

Commit

Permalink
Fixed salesagility#7740 - Discussion on replacing the StateChecker wi…
Browse files Browse the repository at this point in the history
…th database truncation in tests
  • Loading branch information
Dillon-Brown committed Sep 4, 2019
1 parent f0312b0 commit 42e0d34
Show file tree
Hide file tree
Showing 158 changed files with 392 additions and 345 deletions.
3 changes: 2 additions & 1 deletion include/DatabaseTransactions.php
Expand Up @@ -58,7 +58,8 @@ public function startDBTransaction()
$db->query('START TRANSACTION');
}

public function rollbackDBTransaction() {
public function rollbackDBTransaction()
{
$db = DBManagerFactory::getInstance();
$db->query('ROLLBACK');
}
Expand Down
16 changes: 11 additions & 5 deletions include/RefreshDatabase.php
Expand Up @@ -54,16 +54,22 @@
trait RefreshDatabase
{
/**
* Truncates the database before each unit test
* Truncates the database/table before each unit test
* @param string $table database table to truncate or 'ALL' to truncate all tables.
* @throws Exception
*/
public function refreshDatabase()
public function refreshDatabase($table = 'ALL')
{
$db = DBManagerFactory::getInstance();
foreach ($db->getTablesArray() as $table) {
if (!$db->query('TRUNCATE TABLE ' . $table)) {
throw new Exception('Failed to truncate database');

if ($table === 'ALL') {
foreach ($db->getTablesArray() as $table) {
if (!$db->query('TRUNCATE TABLE ' . $table)) {
throw new Exception('Failed to truncate database');
}
}
} elseif (!$db->query('TRUNCATE TABLE ' . $table)) {
throw new Exception('Failed to truncate table: ' . $table);
}
}
}
21 changes: 13 additions & 8 deletions include/TestCaseAbstract.php
Expand Up @@ -56,25 +56,30 @@ abstract class TestCaseAbstract extends PHPUnit_Framework_TestCase
use DatabaseTransactions;
use RefreshDatabase;

protected static $verbose = true;
protected static $cleanupStrategy = 'transaction';
protected $verbose;
protected $cleanupStrategy;

/**
* @param bool $verbose
* @param string $cleanupStrategy
* @throws Exception
*/
protected function setUp()
protected function setUp($verbose = true, $cleanupStrategy = 'transaction')
{
if (self::$verbose) {
$this->verbose = $verbose;
$this->cleanupStrategy = $cleanupStrategy;

if ($this->verbose) {
$currentTestName = get_class($this) . '::' . $this->getName(false);
fwrite(STDOUT, "\t" . $currentTestName . ' ..');
for ($i = 60, $iMax = strlen($currentTestName); $i > $iMax; $i--) {
fwrite(STDOUT, '.');
}
}

if (self::$cleanupStrategy === 'transaction') {
if ($this->cleanupStrategy === 'transaction') {
$this->startDBTransaction();
} elseif (self::$cleanupStrategy === 'refresh') {
} elseif ($this->cleanupStrategy === 'refresh') {
$this->refreshDatabase();
} else {
throw new Exception('Failed to truncate database');
Expand All @@ -86,11 +91,11 @@ protected function tearDown()
{
parent::tearDown();

if (self::$cleanupStrategy === 'transaction') {
if ($this->cleanupStrategy === 'transaction') {
$this->rollbackDBTransaction();
}

if (self::$verbose) {
if ($this->verbose) {
fwrite(STDOUT, " [done]\n");
}
}
Expand Down
@@ -1,11 +1,19 @@
<?php

namespace SuiteCRM\Test;

use BeanFactory;
use DBManager;
use DBManagerFactory;
use LoggerManager;
use SuiteCRM\Exception\Exception;
use SuiteCRM\TestCaseAbstract;

/** @noinspection PhpUndefinedClassInspection */
abstract class SuitePHPUnit_Framework_TestCase extends TestCaseAbstract
/**
* Class SuitePHPUnitFrameworkTestCase
* @package SuiteCRM\Test
*/
abstract class SuitePHPUnitFrameworkTestCase extends TestCaseAbstract
{

/**
Expand All @@ -14,12 +22,12 @@ abstract class SuitePHPUnit_Framework_TestCase extends TestCaseAbstract
protected $env = [];

/**
* @var \LoggerManager
* @var LoggerManager
*/
protected $log;

/**
* @var \DBManager
* @var DBManager
*/
protected $db;

Expand All @@ -45,16 +53,16 @@ public static function setUpBeforeClass()
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
* @param bool $verbose
* @param string $cleanupStrategy
* @throws Exception
*/
public function setUp()
public function setUp($verbose = true, $cleanupStrategy = 'transaction')
{
parent::setUp();
parent::setUp($verbose, $cleanupStrategy);

global $current_user, $sugar_config;
try {
$current_user = @\BeanFactory::getBean('Users'); //new User();
} catch (Exception $e) {
}
$current_user = @BeanFactory::getBean('Users');
get_sugar_config_defaults();

$this->log = $GLOBALS['log'];
Expand Down Expand Up @@ -94,7 +102,7 @@ public function tearDown()
$GLOBALS['log'] = $this->log;

DBManagerFactory::$instances = $this->dbManagerFactoryInstances;

parent::tearDown();
}
}
4 changes: 2 additions & 2 deletions tests/unit/phpunit/Api/Core/Loader/CustomLoaderTest.php
Expand Up @@ -40,14 +40,14 @@

use Api\Core\Loader\CustomLoader;
use Slim\App;
use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

/**
* CustomLoaderTest
*
* @author gyula
*/
class CustomLoaderTest extends SuitePHPUnit_Framework_TestCase
class CustomLoaderTest extends SuitePHPUnitFrameworkTestCase
{
public function testArrayMerge()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/phpunit/configTest.php
@@ -1,8 +1,8 @@
<?php

use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

class configTest extends SuitePHPUnit_Framework_TestCase
class configTest extends SuitePHPUnitFrameworkTestCase
{
public function test_config()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/phpunit/data/SugarBeanTest.php
Expand Up @@ -2,10 +2,10 @@
include_once __DIR__ . '/SugarBeanMock.php';
include_once __DIR__ . '/../../../../include/SubPanel/SubPanelDefinitions.php';

use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

/** @noinspection PhpUndefinedClassInspection */
class SugarBeanTest extends SuitePHPUnit_Framework_TestCase
class SugarBeanTest extends SuitePHPUnitFrameworkTestCase
{


Expand Down
4 changes: 2 additions & 2 deletions tests/unit/phpunit/include/APIErrorObjectTest.php
Expand Up @@ -42,7 +42,7 @@
use SuiteCRM\JsonApiErrorObject;
use SuiteCRM\LangException;
use SuiteCRM\LangText;
use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

if (!defined('sugarEntry') || !sugarEntry) {
die('Not A Valid Entry Point');
Expand All @@ -53,7 +53,7 @@
*
* @author gyula
*/
class JsonApiErrorObjectTest extends SuitePHPUnit_Framework_TestCase
class JsonApiErrorObjectTest extends SuitePHPUnitFrameworkTestCase
{
public function setUp()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/phpunit/include/ErrorMessageTest.php
Expand Up @@ -40,7 +40,7 @@

use SuiteCRM\ErrorMessage;
use SuiteCRM\ErrorMessageException;
use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

if (!defined('sugarEntry') || !sugarEntry) {
die('Not A Valid Entry Point');
Expand All @@ -53,7 +53,7 @@
*
* @author gyula
*/
class ErrorMessageTest extends SuitePHPUnit_Framework_TestCase
class ErrorMessageTest extends SuitePHPUnitFrameworkTestCase
{
public function setUp()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/phpunit/include/Imap/ImapHandlerFactoryTest.php
Expand Up @@ -38,7 +38,7 @@
* display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
*/

use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

if (!defined('sugarEntry') || !sugarEntry) {
die('Not A Valid Entry Point');
Expand All @@ -51,7 +51,7 @@
*
* @author gyula
*/
class ImapHandlerFactoryTest extends SuitePHPUnit_Framework_TestCase
class ImapHandlerFactoryTest extends SuitePHPUnitFrameworkTestCase
{
/**
* FAIL: invalid key argument for save test settings key
Expand Down
Expand Up @@ -38,7 +38,7 @@
* display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
*/

use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

if (!defined('sugarEntry') || !sugarEntry) {
die('Not A Valid Entry Point');
Expand All @@ -51,7 +51,7 @@
*
* @author gyula
*/
class ImapTestSettingsEntryHandlerTest extends SuitePHPUnit_Framework_TestCase
class ImapTestSettingsEntryHandlerTest extends SuitePHPUnitFrameworkTestCase
{

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/phpunit/include/LangExceptionTest.php
Expand Up @@ -40,7 +40,7 @@

use SuiteCRM\LangException;
use SuiteCRM\LangText;
use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

if (!defined('sugarEntry') || !sugarEntry) {
die('Not A Valid Entry Point');
Expand All @@ -52,7 +52,7 @@
*
* @author gyula
*/
class LangExceptionTest extends SuitePHPUnit_Framework_TestCase
class LangExceptionTest extends SuitePHPUnitFrameworkTestCase
{
public function setUp()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/phpunit/include/LangTextTest.php
Expand Up @@ -41,7 +41,7 @@

use SuiteCRM\ErrorMessageException;
use SuiteCRM\LangText;
use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

if (!defined('sugarEntry') || !sugarEntry) {
die('Not A Valid Entry Point');
Expand All @@ -52,7 +52,7 @@
*
* @author gyula
*/
class LangTextTest extends SuitePHPUnit_Framework_TestCase
class LangTextTest extends SuitePHPUnitFrameworkTestCase
{
public function setUp()
{
Expand Down
@@ -1,9 +1,9 @@
<?php


use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

class ControllerFactoryTest extends SuitePHPUnit_Framework_TestCase
class ControllerFactoryTest extends SuitePHPUnitFrameworkTestCase
{
public function testgetController()
{
Expand Down
@@ -1,9 +1,9 @@
<?php

use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;
use SuiteCRM\Test\TestLogger;

class SugarControllerTest extends SuitePHPUnit_Framework_TestCase
class SugarControllerTest extends SuitePHPUnitFrameworkTestCase
{
public function setUp()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/phpunit/include/MVC/SugarApplicationTest.php
@@ -1,9 +1,9 @@
<?php


use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

class SugarApplicationTest extends SuitePHPUnit_Framework_TestCase
class SugarApplicationTest extends SuitePHPUnitFrameworkTestCase
{
public function setUp()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/phpunit/include/MVC/SugarModuleTest.php
@@ -1,8 +1,8 @@
<?php

use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

class SugarModuleTest extends SuitePHPUnit_Framework_TestCase
class SugarModuleTest extends SuitePHPUnitFrameworkTestCase
{
public function testconstructor()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/phpunit/include/MVC/View/SugarViewTest.php
@@ -1,8 +1,8 @@
<?php

use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

class SugarViewTest extends SuitePHPUnit_Framework_TestCase
class SugarViewTest extends SuitePHPUnitFrameworkTestCase
{
public function setUp()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/phpunit/include/MVC/View/ViewFactoryTest.php
@@ -1,9 +1,9 @@
<?php


use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

class ViewFactoryTest extends SuitePHPUnit_Framework_TestCase
class ViewFactoryTest extends SuitePHPUnitFrameworkTestCase
{
public function testloadView()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/phpunit/include/MVC/View/views/view.ajaxTest.php
@@ -1,8 +1,8 @@
<?php

use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

class ViewAjaxTest extends SuitePHPUnit_Framework_TestCase
class ViewAjaxTest extends SuitePHPUnitFrameworkTestCase
{
public function setUp()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/phpunit/include/MVC/View/views/view.ajaxuiTest.php
@@ -1,8 +1,8 @@
<?php

use SuiteCRM\Test\SuitePHPUnit_Framework_TestCase;
use SuiteCRM\Test\SuitePHPUnitFrameworkTestCase;

class ViewAjaxUITest extends SuitePHPUnit_Framework_TestCase
class ViewAjaxUITest extends SuitePHPUnitFrameworkTestCase
{
public function test__construct()
{
Expand Down

0 comments on commit 42e0d34

Please sign in to comment.