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/Espo/Core/Utils/UtilTest.php
T
Taras Machyshyn fcead3379b Util impovements
2014-10-30 16:24:43 +02:00

906 lines
25 KiB
PHP

<?php
namespace tests\Espo\Core\Utils;
use Espo\Core\Utils\Util;
class UtilTest extends \PHPUnit_Framework_TestCase
{
public function testGetSeparator()
{
$this->assertEquals(DIRECTORY_SEPARATOR, Util::getSeparator());
}
public function testToCamelCase()
{
$this->assertEquals('detail', Util::toCamelCase('detail'));
$this->assertEquals('detailView', Util::toCamelCase('detail-view', '-'));
$this->assertEquals('myDetailView', Util::toCamelCase('my_detail_view'));
}
public function testFromCamelCase()
{
$this->assertEquals('detail', Util::fromCamelCase('detail'));
$this->assertEquals('detail-view', Util::fromCamelCase('detailView', '-'));
$this->assertEquals('my_detail_view', Util::fromCamelCase('myDetailView'));
$this->assertEquals('my_f_f', Util::fromCamelCase('myFF'));
}
public function testToUnderScoree()
{
$this->assertEquals('detail', Util::toUnderScore('detail'));
$this->assertEquals('detail_view', Util::toUnderScore('detailView'));
$this->assertEquals('my_detail_view', Util::toUnderScore('myDetailView'));
$this->assertEquals('my_f_f', Util::toUnderScore('myFF'));
}
public function testMerge()
{
$array1= array(
'defaultPermissions',
'logger',
'devMode'
);
$array2Main= array(
45 => '125',
'sub' => array (
'subV' => '125',
),
);
$result= array(
'defaultPermissions',
'logger',
'devMode',
45 => '125',
'sub' => array (
'subV' => '125',
),
);
$this->assertEquals($result, Util::merge($array1, $array2Main));
$array1= array(
'datetime' =>
array (
'dateFormat' => 'Y-m-d',
'timeFormat' => 'H:i:s',
),
);
$array2Main= array(
'datetime' =>
array (
'dateFormat' => 'MyDateFormat',
),
);
$result= array(
'datetime' =>
array (
'dateFormat' => 'MyDateFormat',
'timeFormat' => 'H:i:s',
),
);
$this->assertEquals($result, Util::merge($array1, $array2Main));
$array1= array(
'database' =>
array (
'driver' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => 'espocrm',
'user' => 'root',
'password' => '',
),
);
$array2Main= array(
'database' =>
array (
'password' => 'MyPass',
),
);
$result= array(
'database' =>
array (
'driver' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => 'espocrm',
'user' => 'root',
'password' => 'MyPass',
),
);
$this->assertEquals($result, Util::merge($array1, $array2Main));
}
public function testMergeWithAppend()
{
$currentArray = array(
'entityDefs' =>
array (
'Attachment' =>
array (
'fields' =>
array (
'name' =>
array (
'type' => 'varchar',
'required' => true,
),
'type' =>
array (
'type' => 'varchar',
'maxLength' => 36,
),
'size' =>
array (
'type' => 'enum',
'value' => ["v1", "v2", "v3"],
),
'sizeInt' =>
array (
'type' => 'enum',
'value' => [0, 1, 2],
),
'merged' =>
array (
'type' => 'enum',
'value' => ["v1", "v2", "v3"],
),
'mergedInt' =>
array (
'type' => 'enum',
'value' => [0, 1, 2],
),
),
),
'Contact' =>
array (
'fields' =>
array (
'name' =>
array (
'type' => 'varchar',
'required' => true,
),
'type' =>
array (
'type' => 'varchar',
'maxLength' => 36,
),
'size' =>
array (
'type' => 'enum',
'value' => ["v1", "v2", "v3"],
),
'merged' =>
array (
'type' => 'enum',
'value' => ["v1", "v2", "v3"],
),
),
),
),
'MyCustom' =>
array (
'fields' =>
array (
'name' =>
array (
'type' => 'varchar',
'required' => true,
),
),
),
);
$newArray = array(
'entityDefs' =>
array (
'Attachment' =>
array (
'fields' =>
array (
'name' =>
array (
'type' => 'varchar',
'required' => false,
'NEW' => 'NEWVAL',
),
'type' =>
array (
'type' => 'NETYPE',
),
'size' =>
array (
'type' => 'enum',
'value' => ["B1", "B2", "B3"],
),
'sizeInt' =>
array (
'type' => 'enum',
'value' => [5, 8, 9],
),
'merged' =>
array (
'type' => 'enum',
'value' => ["__APPEND__", "B1", "B2", "B3"],
),
'mergedInt' =>
array (
'type' => 'enum',
'value' => ['__APPEND__', 5, 8, 9],
),
),
'list' =>
array (
'test' => 'Here',
),
),
'Contact' =>
array (
'fields' =>
array (
'name' =>
array (
'type' => 'varchar',
'required' => false,
'NEW' => 'NEWVAL',
),
'type' =>
array (
'type' => 'NEW',
'maxLength' => 1000000,
),
'size' =>
array (
'type' => 'enum',
'value' => ["B1", "B2", "B3"],
),
'merged' =>
array (
'type' => 'enum',
'value' => ["__APPEND__", "B1", "B2", "B3"],
),
),
),
),
);
$result = array (
'entityDefs' =>
array (
'Attachment' =>
array (
'fields' =>
array (
'name' =>
array (
'type' => 'varchar',
'required' => false,
'NEW' => 'NEWVAL',
),
'type' =>
array (
'type' => 'NETYPE',
'maxLength' => 36,
),
'size' =>
array (
'type' => 'enum',
'value' =>
array (
0 => 'B1',
1 => 'B2',
2 => 'B3',
),
),
'sizeInt' =>
array (
'type' => 'enum',
'value' =>
array (
0 => 5,
1 => 8,
2 => 9,
),
),
'merged' =>
array (
'type' => 'enum',
'value' =>
array (
0 => 'v1',
1 => 'v2',
2 => 'v3',
3 => 'B1',
4 => 'B2',
5 => 'B3',
),
),
'mergedInt' =>
array (
'type' => 'enum',
'value' =>
array (
0 => 0,
1 => 1,
2 => 2,
3 => 5,
4 => 8,
5 => 9,
),
),
),
'list' =>
array (
'test' => 'Here',
),
),
'Contact' =>
array (
'fields' =>
array (
'name' =>
array (
'type' => 'varchar',
'required' => false,
'NEW' => 'NEWVAL',
),
'type' =>
array (
'type' => 'NEW',
'maxLength' => 1000000,
),
'size' =>
array (
'type' => 'enum',
'value' =>
array (
0 => 'B1',
1 => 'B2',
2 => 'B3',
),
),
'merged' =>
array (
'type' => 'enum',
'value' =>
array (
0 => 'v1',
1 => 'v2',
2 => 'v3',
3 => 'B1',
4 => 'B2',
5 => 'B3',
),
),
),
),
),
'MyCustom' =>
array (
'fields' =>
array (
'name' =>
array (
'type' => 'varchar',
'required' => true,
),
),
),
);
$this->assertEquals($result, Util::merge($currentArray, $newArray));
}
public function testMergeWithBool()
{
$currentArray = array (
'fields' =>
array (
'accountId' =>
array (
'type' => 'varchar',
'where' =>
array (
'=' => 'contact.id IN ({value})',
),
'len' => 255,
),
'deleted' =>
array (
'type' => 'bool',
'default' => false,
'trueValue' => true,
),
),
'relations' =>
array (
),
);
$newArray = array (
'fields' =>
array (
'accountName' =>
array (
'type' => 'foreign',
'relation' => 'account',
'foreign' => 'name',
),
'accountId' =>
array (
'type' => 'foreignId',
'index' => true,
),
),
'relations' =>
array (
'createdBy' =>
array (
'type' => 'belongsTo',
'entity' => 'User',
'key' => 'createdById',
'foreignKey' => 'id',
),
),
);
$result = array (
'fields' =>
array (
'accountName' =>
array (
'type' => 'foreign',
'relation' => 'account',
'foreign' => 'name',
),
'accountId' =>
array (
'type' => 'foreignId',
'index' => true,
'where' =>
array (
'=' => 'contact.id IN ({value})',
),
'len' => 255,
),
'deleted' =>
array (
'type' => 'bool',
'default' => false,
'trueValue' => true,
),
),
'relations' =>
array (
'createdBy' =>
array (
'type' => 'belongsTo',
'entity' => 'User',
'key' => 'createdById',
'foreignKey' => 'id',
),
),
);
$this->assertEquals($result, Util::merge($currentArray, $newArray));
}
public function testMergeWithFieldsDefs()
{
$currentArray = array (
'fields' =>
array (
'aaa1' =>
array (
'type' => 'enum',
'required' => false,
'options' =>
array (
0 => 'a1',
1 => 'a3',
2 => 'a3',
),
'isCustom' => true,
),
'hfghgfh' =>
array (
'type' => 'varchar',
'required' => false,
'isCustom' => true,
'default' => 'hfghfgh',
),
'jghjghj' =>
array (
'type' => 'varchar',
'required' => false,
'isCustom' => true,
'default' => 'jghjghjhg',
),
'gdfgdfg' =>
array (
'type' => 'varchar',
'required' => false,
'isCustom' => true,
'default' => 'gdfgdfg',
'maxLength' => 70,
),
),
);
$newArray = array (
'fields' =>
array (
'aaa1' =>
array (
'type' => 'enum',
'required' => false,
'options' =>
array (
0 => 'a1',
),
'isCustom' => true,
),
),
);
$result = array (
'fields' =>
array (
'aaa1' =>
array (
'type' => 'enum',
'required' => false,
'options' =>
array (
0 => 'a1',
),
'isCustom' => true,
),
'hfghgfh' =>
array (
'type' => 'varchar',
'required' => false,
'isCustom' => true,
'default' => 'hfghfgh',
),
'jghjghj' =>
array (
'type' => 'varchar',
'required' => false,
'isCustom' => true,
'default' => 'jghjghjhg',
),
'gdfgdfg' =>
array (
'type' => 'varchar',
'required' => false,
'isCustom' => true,
'default' => 'gdfgdfg',
'maxLength' => 70,
),
),
);
$this->assertEquals($result, Util::merge($currentArray, $newArray));
}
public function testToFormat()
{
$this->assertEquals('/Espo/Core/Utils', Util::toFormat('/Espo/Core/Utils', '/'));
$this->assertEquals('\Espo\Core\Utils', Util::toFormat('/Espo/Core/Utils', '\\'));
$this->assertEquals('/Espo/Core/Utils', Util::toFormat('\Espo\Core\Utils', '/'));
$this->assertEquals('\Espo\Core\Utils', Util::toFormat('\Espo\Core\Utils', '\\'));
}
public function testConcatPath()
{
$result= 'dir1/dir2/file1.json';
$this->assertEquals($result, Util::concatPath('dir1/dir2', 'file1.json'));
$result= 'dir1/dir2/file1.json';
$this->assertEquals($result, Util::concatPath('dir1/dir2/', 'file1.json'));
$result= 'dir1/dir2/file1.json';
$this->assertEquals($result, Util::concatPath('dir1/dir2/file1.json'));
$input = array('dir1/dir2', 'file1.json');
$result= 'dir1/dir2/file1.json';
$this->assertEquals($result, Util::concatPath($input));
$input = array('dir1/', 'dir2', 'file1.json');
$result = 'dir1/dir2/file1.json';
$this->assertEquals($result, Util::concatPath($input));
}
public function testArrayToObject()
{
$testArr= array(
'useCache' => true,
'sub' => array (
'subV' => '125',
'subO' => array(
'subOV' => '125',
),
),
);
$testResult= (object) array(
'useCache' => true,
);
$testResult->sub = (object) array (
'subV' => '125',
);
$testResult->sub->subO = (object) array (
'subOV' => '125',
);
$this->assertEquals($testResult, Util::arrayToObject($testArr));
}
public function testObjectToArray()
{
$testObj= (object) array(
'useCache' => true,
);
$testObj->sub = (object) array (
'subV' => '125',
);
$testObj->sub->subO = (object) array (
'subOV' => '125',
);
$testResult= array(
'useCache' => true,
'sub' => array (
'subV' => '125',
'subO' => array(
'subOV' => '125',
),
),
);
$this->assertEquals($testResult, Util::objectToArray($testObj));
}
public function testGetNaming()
{
$this->assertEquals('myPrefixMyName', Util::getNaming('myName', 'myPrefix', 'prefix'));
$this->assertEquals('myNameMyPostfix', Util::getNaming('myName', 'myPostfix', 'postfix'));
$this->assertEquals('myNameMyPostfix', Util::getNaming('my_name', 'myPostfix', 'postfix', '_'));
$this->assertEquals('myNameMyPostfix', Util::getNaming('my_name', 'my_postfix', 'postfix', '_'));
}
public function testReplaceInArray()
{
$testArray = array(
'option' => array(
'default' => '{0}',
'testKey' => array(
'{0}' => 'testVal',
),
),
);
$testResult = array(
'option' => array(
'default' => 'DONE',
'testKey' => array(
'DONE' => 'testVal',
),
),
);
$this->assertEquals($testResult, Util::replaceInArray('{0}', 'DONE', $testArray, true));
}
public function testGetClassName()
{
$this->assertEquals('\Espo\EntryPoints\Donwload', Util::getClassName('application/Espo/EntryPoints/Donwload.php'));
$this->assertEquals('\Espo\EntryPoints\Donwload', Util::getClassName('custom/Espo/EntryPoints/Donwload.php'));
$this->assertEquals('\Espo\EntryPoints\Donwload', Util::getClassName('Espo/EntryPoints/Donwload.php'));
$this->assertEquals('\Espo\EntryPoints\Donwload', Util::getClassName('application/Espo/EntryPoints/Donwload'));
}
public function testUnsetInArrayNotSingle()
{
$input = array(
'Account' => array(
'useCache' => true,
'sub' => array (
'subV' => '125',
'subO' => array(
'subOV' => '125',
'subOV2' => '125',
),
),
),
);
$unsets = array(
'Account' => array(
'sub.subO.subOV', 'sub.subV',
),
);
$result = array(
'Account' => array(
'useCache' => true,
'sub' => array (
'subO' => array(
'subOV2' => '125',
),
),
),
);
$this->assertEquals($result, Util::unsetInArray($input, $unsets));
}
public function testUnsetInArraySingle()
{
$input = array(
'Account' => array(
'useCache' => true,
'sub' => array (
'subV' => '125',
'subO' => array(
'subOV' => '125',
'subOV2' => '125',
),
),
),
);
$unsets = array(
'Account.sub.subO.subOV', 'Account.sub.subV',
);
$result = array(
'Account' => array(
'useCache' => true,
'sub' => array (
'subO' => array(
'subOV2' => '125',
),
),
),
);
$this->assertEquals($result, Util::unsetInArray($input, $unsets));
}
public function testUnsetInArrayTogether()
{
$input = array(
'Account' => array(
'useCache' => true,
'sub' => array (
'subV' => '125',
'subO' => array(
'subOV' => '125',
'subOV2' => '125',
),
),
),
);
$unsets = array(
'Account' => array(
'sub.subO.subOV',
),
'Account.sub.subV',
);
$result = array(
'Account' => array(
'useCache' => true,
'sub' => array (
'subO' => array(
'subOV2' => '125',
),
),
),
);
$this->assertEquals($result, Util::unsetInArray($input, $unsets));
}
public function testUnsetInArray()
{
$input = array(
'Account' => array(
'useCache' => true,
'sub' => array (
'subV' => '125',
'subO' => array(
'subOV' => '125',
'subOV2' => '125',
),
),
),
'Contact' => array(
'useCache' => true,
),
);
$unsets = array(
'Account',
);
$result = array(
'Contact' => array(
'useCache' => true,
),
);
$this->assertEquals($result, Util::unsetInArray($input, $unsets));
}
public function testUnsetInArrayByString()
{
$input = array(
'Account' => array(
'useCache' => true,
),
'Contact' => array(
'useCache' => true,
),
);
$unsets = 'Account.useCache';
$result = array(
'Account' => array(
),
'Contact' => array(
'useCache' => true,
),
);
$this->assertEquals($result, Util::unsetInArray($input, $unsets));
}
public function testGetValueByKey()
{
$inputArray = array(
'Account' => array(
'useCache' => true,
'sub' => array (
'subV' => '125',
'subO' => array(
'subOV' => '125',
'subOV2' => '125',
),
),
),
'Contact' => array(
'useCache' => true,
),
);
$this->assertEquals($inputArray, Util::getValueByKey($inputArray));
$this->assertEquals($inputArray, Util::getValueByKey($inputArray, ''));
$this->assertEquals('125', Util::getValueByKey($inputArray, 'Account.sub.subV'));
$result = array('useCache' => true, );
$this->assertEquals($result, Util::getValueByKey($inputArray, 'Contact'));
$this->assertNull(Util::getValueByKey($inputArray, 'Contact.notExists'));
$this->assertEquals('customReturns', Util::getValueByKey($inputArray, 'Contact.notExists', 'customReturns'));
$this->assertNotEquals('customReturns', Util::getValueByKey($inputArray, 'Contact.useCache', 'customReturns'));
}
}
?>