This repository has been archived on 2026-07-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
espocrm-base/tests/unit/Espo/ORM/EntityTest.php
T
Yuri Kuznetsov 78b9e608e9 locker
2020-08-27 12:58:22 +03:00

225 lines
7.8 KiB
PHP

<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\unit\Espo\ORM;
use Espo\ORM\{
DB\MysqlMapper,
DB\Query\Mysql as Query,
EntityFactory,
};
use Espo\Entities\Job;
use Espo\Core\ORM\EntityManager;
require_once 'tests/unit/testData/DB/Entities.php';
class EntityTest extends \PHPUnit\Framework\TestCase
{
protected function setUp() : void
{
}
protected function tearDown() : void
{
}
protected function createEntity(string $entityType, string $className)
{
$em = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
$entity = new $className($entityType, [], $em);
return $entity;
}
public function testIsAttributeChanged()
{
$job = $this->createEntity('Job', Job::class);
$job->setFetched('string', 'test');
$this->assertFalse($job->isAttributeChanged('string'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('string', 'test');
$job->set('string', 'hello');
$this->assertTrue($job->isAttributeChanged('string'));
$job = $this->createEntity('Job', Job::class);
$job->set('string', 'hello');
$this->assertTrue($job->isAttributeChanged('string'));
$job = $this->createEntity('Job', Job::class);
$job->set('string', null);
$this->assertTrue($job->isAttributeChanged('string'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('array', ['1', '2']);
$job->set('array', ['2', '1']);
$this->assertTrue($job->isAttributeChanged('array'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('array', ['1', '2']);
$job->set('array', ['1', '2']);
$this->assertFalse($job->isAttributeChanged('array'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('array', ['1', '2']);
$job->set('array', ['1', 2]);
$this->assertTrue($job->isAttributeChanged('array'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('array', [
(object) ['1' => 'v1']
]);
$job->set('array', [
(object) ['1' => 'v1']
]);
$this->assertFalse($job->isAttributeChanged('array'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('array', [
(object) ['k1' => 'v1']
]);
$job->set('array', [
(object) ['k1' => 'v2']
]);
$this->assertTrue($job->isAttributeChanged('array'));
$job = $this->createEntity('Job', Job::class);
$job->set('array', [
(object) ['k1' => 'v1']
]);
$job->setAsFetched();
$job->set('array', [
(object) ['k1' => 'v1', 'k2' => 'v2'],
]);
$this->assertTrue($job->isAttributeChanged('array'));
$job = $this->createEntity('Job', Job::class);
$v = [
(object) ['k1' => 'v1']
];
$job->setFetched('array', $v);
$v[0]->k2 = 'v2';
$job->set('array', $v);
$this->assertTrue($job->isAttributeChanged('array'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('array', ['1', '2']);
$job->set('array', ['1', '2', '3']);
$this->assertTrue($job->isAttributeChanged('array'));
$job = $this->createEntity('Job', Job::class);
$job->set('array', ['1', '2', '3']);
$this->assertTrue($job->isAttributeChanged('array'));
$job = $this->createEntity('Job', Job::class);
$job->set('array', null);
$this->assertTrue($job->isAttributeChanged('array'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('array', null);
$this->assertFalse($job->isAttributeChanged('array'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('arrayUnordered', ['1', '2']);
$job->set('arrayUnordered', ['2', '1']);
$this->assertFalse($job->isAttributeChanged('arrayUnordered'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('arrayUnordered', ['1', '2']);
$job->set('arrayUnordered', ['1', '2']);
$this->assertFalse($job->isAttributeChanged('arrayUnordered'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('arrayUnordered', ['1', '2']);
$job->set('arrayUnordered', ['1', '2', '3']);
$this->assertTrue($job->isAttributeChanged('arrayUnordered'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('arrayUnordered', ['1', '2']);
$job->set('arrayUnordered', null);
$this->assertTrue($job->isAttributeChanged('arrayUnordered'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('object', (object) ['a1' => 'value-1']);
$job->set('object', (object) ['a1' => 'value-1']);
$this->assertFalse($job->isAttributeChanged('object'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('object', (object) ['a1' => 'value-1']);
$job->set('object', ['a1' => 'value-1']);
$this->assertTrue($job->isAttributeChanged('object'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('object', (object) ['1' => '1']);
$job->set('object', (object) ['1' => 1]);
$this->assertTrue($job->isAttributeChanged('object'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('object', (object) [
'k1' => (object) [
'k11' => 'v1'
]
]);
$job->set('object', (object) [
'k1' => (object) [
'k11' => 'v2'
]
]);
$this->assertTrue($job->isAttributeChanged('object'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('object', (object) [
'k1' => [
'k11' => 'v1'
]
]);
$job->set('object', (object) [
'k1' => (object) [
'k11' => 'v1'
]
]);
$this->assertTrue($job->isAttributeChanged('object'));
$job = $this->createEntity('Job', Job::class);
$job->setFetched('object', [
'k1' => [
'k11' => 'v1'
]
]);
$job->set('object', (object) [
'k1' => (object) [
'k11' => 'v1'
]
]);
$this->assertTrue($job->isAttributeChanged('object'));
}
}