Files
iTop/tests/php-unit-tests/unitary-tests/core/DBObject/DBObjectSetTest.php

139 lines
3.8 KiB
PHP

<?php
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
class DBObjectSetTest extends ItopDataTestCase
{
public const USE_TRANSACTION = true;
protected function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->CreateURs();
}
protected function tearDown(): void
{
CMDBSource::TriggerExceptionWhenSqlQuery(null);
parent::tearDown(); // TODO: Change the autogenerated stub
}
private function CreateURs()
{
$this->CreateTestOrganization();
for ($i = 0; $i < 10; $i++) {
$this->CreateTicket($i);
}
}
public function testCount()
{
$oSearch = DBObjectSearch::FromOQL_AllData("SELECT UserRequest");
$oSet = new DBObjectSet($oSearch);
$iCount = $oSet->Count();
$oSet->Fetch();
$this->assertEquals($iCount, $oSet->Count());
$this->assertEquals($iCount, $oSet->CountWithLimit(0));
$this->assertTrue($oSet->CountExceeds(0));
//no DB SQL query: exception will be raised after here
CMDBSource::TriggerExceptionWhenSqlQuery(__METHOD__.' :'.__LINE__);
$this->assertEquals($iCount, $oSet->Count(), 'should use cache and not call DB again');
}
public function testRewind()
{
$oSearch = DBObjectSearch::FromOQL_AllData("SELECT UserRequest");
$oSet = new DBObjectSet($oSearch);
while ($oObj = $oSet->Fetch()) {
$this->assertNotEquals(0, $oObj->GetKey());
}
//no DB SQL query: exception will be raised after here
CMDBSource::TriggerExceptionWhenSqlQuery(__METHOD__.' :'.__LINE__);
$oSet->Rewind();
while ($oObj = $oSet->Fetch()) {
$this->assertNotEquals(0, $oObj->GetKey());
}
}
public function testDBObjectSetComparator()
{
$oSearch = DBObjectSearch::FromOQL_AllData("SELECT UserRequest");
$DBObjectSet1 = new DBObjectSet($oSearch);
$DBObjectSet3 = new DBObjectSet($oSearch);
$oDBObjectSetComparator = new DBObjectSetComparator($DBObjectSet1, $DBObjectSet3);
$this->assertTrue($oDBObjectSetComparator->SetsAreEquivalent());
}
public function testDBObjectSetComparator_CheckCache()
{
$oSearch = DBObjectSearch::FromOQL_AllData("SELECT UserRequest");
$DBObjectSet1 = new DBObjectSet($oSearch);
$DBObjectSet3 = new DBObjectSet($oSearch);
$oDBObjectSetComparator = new DBObjectSetComparator($DBObjectSet1, $DBObjectSet3);
$this->assertTrue($oDBObjectSetComparator->SetsAreEquivalent());
$sMsg = __METHOD__.' :'.__LINE__;
//no DB SQL query: exception will be raised after here
CMDBSource::TriggerExceptionWhenSqlQuery($sMsg);
$oDBObjectSetComparator = new DBObjectSetComparator($DBObjectSet1, $DBObjectSet3);
$this->assertTrue($oDBObjectSetComparator->SetsAreEquivalent());
$oDBObjectSetComparator = new DBObjectSetComparator($DBObjectSet1, new DBObjectSet($oSearch));
$this->expectExceptionMessage($sMsg, "should call DB again this time");
$this->assertTrue($oDBObjectSetComparator->SetsAreEquivalent());
}
public static function JeffreyProvider()
{
return [
'basic' => [false],
'opt trick' => [true],
];
}
/**
* @dataProvider JeffreyProvider
*/
public function testJeffrey(bool $bTrick)
{
echo '<p>-------</p>';
$this->doesNotPerformAssertions();
$iMax = 100;
$oFilter = DBObjectSearch::FromOQL_AllData('SELECT UserRequest');
$oSet = new DBObjectSet($oFilter);
$oSet->OptimizeColumnLoad([
'UserRequest' => ['ref', 'status', 'title'],
]);
if ($bTrick) {
$oNewSet = DBObjectSet::FromScratch($oSet->GetClass());
while ($oObj = $oSet->Fetch()) {
$oNewSet->AddObject($oObj);
}
$oSet = $oNewSet;
}
echo '<p>Start: '.date('Y-m-d H:i:s').'</p>';
$i = 0;
while ($i < $iMax) {
$oSet->Rewind();
while ($oObj = $oSet->Fetch()) {
// Do nothing
$s = $oObj->Get('title');
}
$i += 1;
}
$peak = memory_get_peak_usage(true) / 1000000;
echo '<p>End: '.date('Y-m-d H:i:s').'</p><p>Peak memory: '.$peak.'</p> \n';
}
}