mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 15:34:12 +01:00
108 lines
3.2 KiB
PHP
108 lines
3.2 KiB
PHP
<?php
|
|
/*
|
|
* @copyright Copyright (C) 2010-2023 Combodo SARL
|
|
* @license http://opensource.org/licenses/AGPL-3.0
|
|
*/
|
|
|
|
/**
|
|
* @runTestsInSeparateProcesses
|
|
* @preserveGlobalState disabled
|
|
* @backupGlobals disabled
|
|
*/
|
|
class ApplicationObjectExtensionTest extends \Combodo\iTop\Test\UnitTest\ItopDataTestCase
|
|
{
|
|
const CREATE_TEST_ORG = true;
|
|
|
|
// Count the calls by name
|
|
private static array $aCalls = [];
|
|
private static int $iCalls = 0;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->RequireOnceUnitTestFile('iApplicationObjectExtension/MockApplicationObjectExtensionForTest.php');
|
|
$this->ResetApplicationObjectExtensions();
|
|
// Count all the calls to this object
|
|
MockApplicationObjectExtensionForTest::SetCallBack([ApplicationObjectExtensionTest::class, 'IncrementCallCount']);
|
|
}
|
|
|
|
public function tearDown(): void
|
|
{
|
|
MockApplicationObjectExtensionForTest::SetModifications('Person', 'name', 0);
|
|
MockApplicationObjectExtensionForTest::SetCallBack(null);
|
|
parent::tearDown();
|
|
}
|
|
|
|
public static function IncrementCallCount(string $sOrigin)
|
|
{
|
|
self::$aCalls[$sOrigin] = (self::$aCalls[$sOrigin] ?? 0) + 1;
|
|
self::$iCalls++;
|
|
}
|
|
|
|
public static function ResetCallCount()
|
|
{
|
|
self::$aCalls = [];
|
|
self::$iCalls = 0;
|
|
}
|
|
|
|
public function testExtensionCalled()
|
|
{
|
|
// Check that extension is called
|
|
$oPerson = $this->CreatePerson(1);
|
|
$oPerson->Set('first_name', 'testUpdateReentranceProtection');
|
|
MockApplicationObjectExtensionForTest::SetModifications('Person', 'name', 1);
|
|
self::ResetCallCount();
|
|
$oPerson->DBUpdate();
|
|
// Called twice, the first call will provoke the DBUpdate and call again the object extension
|
|
$this->assertEquals(2, self::$iCalls);
|
|
}
|
|
|
|
public function testUpdateReentranceProtection()
|
|
{
|
|
$oPerson = $this->CreatePerson(1);
|
|
|
|
// Check that loop limit is 10
|
|
$i = 15;
|
|
self::ResetCallCount();
|
|
MockApplicationObjectExtensionForTest::SetModifications('Person', 'name', $i);
|
|
$oPerson->Set('first_name', 'testUpdateReentranceProtection');
|
|
$oPerson->DBUpdate();
|
|
$this->assertEquals(10, self::$iCalls);
|
|
}
|
|
|
|
public function testModificationsOnUpdate()
|
|
{
|
|
$oPerson = $this->CreatePerson(1);
|
|
$oPerson->Set('first_name', 'testUpdateReentranceProtection');
|
|
|
|
self::ResetCallCount();
|
|
MockApplicationObjectExtensionForTest::SetModifications('Person', 'name', 1);
|
|
$oPerson->DBUpdate();
|
|
$this->assertEquals(2, self::$iCalls);
|
|
}
|
|
|
|
public function testModificationsOnInsert()
|
|
{
|
|
self::ResetCallCount();
|
|
MockApplicationObjectExtensionForTest::SetModifications('Person', 'name', 1);
|
|
$oPerson = $this->CreatePerson(1);
|
|
$this->assertEquals(2, self::$iCalls);
|
|
}
|
|
|
|
|
|
public function testModificationsOnInsertWith2Extensions()
|
|
{
|
|
self::ResetCallCount();
|
|
$this->RequireOnceUnitTestFile('iApplicationObjectExtension/MockApplicationObjectExtensionForTest2.php');
|
|
$this->ResetApplicationObjectExtensions();
|
|
// Count all the calls to this object
|
|
MockApplicationObjectExtensionForTest2::SetCallBack([ApplicationObjectExtensionTest::class, 'IncrementCallCount']);
|
|
|
|
MockApplicationObjectExtensionForTest::SetModifications('Person', 'name', 2);
|
|
MockApplicationObjectExtensionForTest2::SetModifications('Person', 'first_name', 2);
|
|
$oPerson = $this->CreatePerson(1);
|
|
$this->assertEquals(6, self::$iCalls);
|
|
}
|
|
|
|
} |