Skip to content

Commit

Permalink
Merge branch '5.x' into feat/colorized-favison
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidmattei authored Oct 15, 2023
2 parents f39296d + a93a066 commit e0f24d1
Show file tree
Hide file tree
Showing 74 changed files with 3,758 additions and 180 deletions.
6 changes: 5 additions & 1 deletion EMS/common-bundle/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
</phpunit>

<extensions>
<extension class="EMS\CommonBundle\Tests\Hook\BypassFinalHook"/>
</extensions>
</phpunit>
16 changes: 16 additions & 0 deletions EMS/common-bundle/tests/Hook/BypassFinalHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace EMS\CommonBundle\Tests\Hook;

use DG\BypassFinals;
use PHPUnit\Runner\BeforeTestHook;

final class BypassFinalHook implements BeforeTestHook
{
public function executeBeforeTest(string $test): void
{
BypassFinals::enable();
}
}
98 changes: 98 additions & 0 deletions EMS/common-bundle/tests/Unit/Common/Admin/ConfigHelperAiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace EMS\Tests\CommonBundle\Unit\Common\Admin;

use EMS\CommonBundle\Common\Admin\ConfigHelper;
use EMS\CommonBundle\Contracts\CoreApi\Endpoint\Admin\ConfigInterface;
use PHPUnit\Framework\TestCase;

class ConfigHelperAiTest extends TestCase
{
private ConfigInterface $config;
private ConfigHelper $configHelper;
private string $tempDir;

protected function setUp(): void
{
$this->config = $this->createMock(ConfigInterface::class);
$this->tempDir = \sys_get_temp_dir().DIRECTORY_SEPARATOR.\uniqid('config_test_', true);
$this->configHelper = new ConfigHelper($this->config, $this->tempDir);
}

protected function tearDown(): void
{
// Cleanup temporary directory
\array_map('unlink', \glob("$this->tempDir/*.*"));
\rmdir($this->tempDir);
}

public function testUpdate(): void
{
$this->config->expects($this->once())->method('index')->willReturn(['config1', 'config2']);
$this->config->expects($this->exactly(2))->method('get')->willReturnOnConsecutiveCalls(['key1' => 'value1'], ['key2' => 'value2']);

$this->configHelper->update();

$this->assertFileExists($this->tempDir.DIRECTORY_SEPARATOR.'config1.json');
$this->assertFileExists($this->tempDir.DIRECTORY_SEPARATOR.'config2.json');
}

public function testSave(): void
{
$configData = ['key' => 'value'];
$this->configHelper->save('testConfig', $configData);

$this->assertFileExists($this->tempDir.DIRECTORY_SEPARATOR.'testConfig.json');
$this->assertEquals(\json_encode($configData, JSON_PRETTY_PRINT), \file_get_contents($this->tempDir.DIRECTORY_SEPARATOR.'testConfig.json'));
}

public function testLocal(): void
{
\touch($this->tempDir.DIRECTORY_SEPARATOR.'config1.json');
\touch($this->tempDir.DIRECTORY_SEPARATOR.'config2.json');

$localConfigs = $this->configHelper->local();

$this->assertEquals(['config1', 'config2'], $localConfigs);
}

public function testRemote(): void
{
$this->config->expects($this->once())->method('index')->willReturn(['config1', 'config2']);

$remoteConfigs = $this->configHelper->remote();

$this->assertEquals(['config1', 'config2'], $remoteConfigs);
}

public function testNeedUpdate(): void
{
$this->config->expects($this->exactly(2))->method('get')->willReturnOnConsecutiveCalls(['key1' => 'value1'], ['key2' => 'value2']);

\file_put_contents($this->tempDir.DIRECTORY_SEPARATOR.'config1.json', \json_encode(['key1' => 'value1']));
\file_put_contents($this->tempDir.DIRECTORY_SEPARATOR.'config2.json', \json_encode(['key2' => 'value2_changed']));

$configsToUpdate = $this->configHelper->needUpdate(['config1', 'config2']);

$this->assertEquals(['config2'], $configsToUpdate);
}

public function testDeleteConfigs(): void
{
$this->config->expects($this->exactly(2))->method('delete');

$this->configHelper->deleteConfigs(['config1', 'config2']);
}

public function testUpdateConfigs(): void
{
$this->config->expects($this->exactly(2))->method('update');

\file_put_contents($this->tempDir.DIRECTORY_SEPARATOR.'config1.json', \json_encode(['key1' => 'value1']));
\file_put_contents($this->tempDir.DIRECTORY_SEPARATOR.'config2.json', \json_encode(['key2' => 'value2']));

$this->configHelper->updateConfigs(['config1', 'config2']);
}
}
107 changes: 107 additions & 0 deletions EMS/common-bundle/tests/Unit/Common/Cache/CacheAiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace EMS\Tests\CommonBundle\Unit\Common\Cache;

use EMS\CommonBundle\Common\Cache\Cache;
use PHPUnit\Framework\TestCase;

class CacheAiTest extends TestCase
{
private string $cacheDir;
private Cache $cache;

protected function setUp(): void
{
$this->cacheDir = \sys_get_temp_dir().DIRECTORY_SEPARATOR.\uniqid('cache_test_', true);
\mkdir($this->cacheDir);
}

protected function removeDirectory($path): void
{
$files = \glob($path.'/*');
foreach ($files as $file) {
\is_dir($file) ? $this->removeDirectory($file) : \unlink($file);
}
\rmdir($path);
}

protected function tearDown(): void
{
\array_map('unlink', \glob("$this->cacheDir/*.*"));
$this->removeDirectory($this->cacheDir);
}

public function testIsApc(): void
{
$this->cache = new Cache(['type' => Cache::TYPE_APC, 'prefix' => 'test_prefix_'], $this->cacheDir);
$this->assertTrue($this->cache->isApc());
}

public function testIsRedis(): void
{
$this->cache = new Cache(['type' => Cache::TYPE_REDIS, 'prefix' => 'test_prefix_', 'redis' => ['host' => '127.0.0.1', 'port' => 6379]], $this->cacheDir);
$this->assertTrue($this->cache->isRedis());
}

public function testIsFilesystem(): void
{
$this->cache = new Cache(['type' => Cache::TYPE_FILE_SYSTEM, 'prefix' => 'test_prefix_'], $this->cacheDir);
$this->assertTrue($this->cache->isFilesystem());
}

public function testGetType(): void
{
$this->cache = new Cache(['type' => Cache::TYPE_APC, 'prefix' => 'test_prefix_'], $this->cacheDir);
$this->assertEquals(Cache::TYPE_APC, $this->cache->getType());
}

public function testGetItem(): void
{
$this->cache = new Cache(['type' => Cache::TYPE_FILE_SYSTEM, 'prefix' => 'test_prefix_'], $this->cacheDir);
$item = $this->cache->getItem('test_key');
$this->assertFalse($item->isHit());
}

public function testSave(): void
{
$this->cache = new Cache(['type' => Cache::TYPE_FILE_SYSTEM, 'prefix' => 'test_prefix_'], $this->cacheDir);
$item = $this->cache->getItem('test_key');
$item->set('test_value');
$this->cache->save($item);

$savedItem = $this->cache->getItem('test_key');
$this->assertTrue($savedItem->isHit());
$this->assertEquals('test_value', $savedItem->get());
}

public function testDelete(): void
{
$this->cache = new Cache(['type' => Cache::TYPE_FILE_SYSTEM, 'prefix' => 'test_prefix_'], $this->cacheDir);
$item = $this->cache->getItem('test_key');
$item->set('test_value');
$this->cache->save($item);

$this->cache->delete('test_key');
$deletedItem = $this->cache->getItem('test_key');
$this->assertFalse($deletedItem->isHit());
}

public function testClear(): void
{
$this->cache = new Cache(['type' => Cache::TYPE_FILE_SYSTEM, 'prefix' => 'test_prefix_'], $this->cacheDir);
$item1 = $this->cache->getItem('test_key1');
$item1->set('test_value1');
$this->cache->save($item1);

$item2 = $this->cache->getItem('test_key2');
$item2->set('test_value2');
$this->cache->save($item2);

$this->cache->clear();

$this->assertFalse($this->cache->getItem('test_key1')->isHit());
$this->assertFalse($this->cache->getItem('test_key2')->isHit());
}
}
70 changes: 70 additions & 0 deletions EMS/common-bundle/tests/Unit/Common/Cache/ConfigAiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace EMS\Tests\CommonBundle\Unit\Common\Cache;

use EMS\CommonBundle\Common\Cache\Cache;
use EMS\CommonBundle\Common\Cache\Config;
use PHPUnit\Framework\TestCase;

class ConfigAiTest extends TestCase
{
public function testValidConfig(): void
{
$config = new Config([
'type' => Cache::TYPE_REDIS,
'prefix' => 'test_prefix_',
'redis' => [
'host' => 'localhost',
'port' => 6379,
],
]);

$this->assertEquals(Cache::TYPE_REDIS, $config->type);
$this->assertEquals('test_prefix_', $config->prefix);
$this->assertEquals('localhost', $config->redis['host']);
$this->assertEquals(6379, $config->redis['port']);
}

public function testDefaultRedisConfig(): void
{
$config = new Config([
'type' => Cache::TYPE_REDIS,
'prefix' => 'test_prefix_',
]);

$this->assertEquals('localhost', $config->redis['host']);
$this->assertEquals(6379, $config->redis['port']);
}

public function testInvalidType(): void
{
$this->expectException(\InvalidArgumentException::class);
new Config([
'type' => 'invalid_type',
'prefix' => 'test_prefix_',
]);
}

public function testMissingRequiredFields(): void
{
$this->expectException(\InvalidArgumentException::class);
new Config([
'type' => Cache::TYPE_REDIS,
]);
}

public function testInvalidRedisPort(): void
{
$this->expectException(\InvalidArgumentException::class);
new Config([
'type' => Cache::TYPE_REDIS,
'prefix' => 'test_prefix_',
'redis' => [
'host' => 'localhost',
'port' => 'invalid_port',
],
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace EMS\CommonBundle\Tests\Unit\Common\Composer;

use EMS\CommonBundle\Common\Composer\ComposerInfo;
use PHPUnit\Framework\TestCase;

final class ComposerInfoAiTest extends TestCase
{
private ComposerInfo $composerInfo;

protected function setUp(): void
{
$projectDir = __DIR__.'/fixtures';
$this->composerInfo = new ComposerInfo($projectDir);
}

public function testBuild(): void
{
$this->composerInfo->build();

$expectedPackages = [
'core' => '1.0.0',
'client' => '1.1.0',
'common' => '1.2.0',
'form' => '1.3.0',
'submission' => '1.4.0',
'symfony' => '5.3.0',
];

$this->assertEquals($expectedPackages, $this->composerInfo->getVersionPackages());
}

public function testGetVersionPackagesWithoutBuild(): void
{
$this->assertEmpty($this->composerInfo->getVersionPackages());
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e0f24d1

Please sign in to comment.