Skip to content

Commit

Permalink
OXDEV-8215: Use random instead of fix values for testing cases
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelOxid committed Jul 30, 2024
1 parent 6a8e5b4 commit 78fbcd2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
21 changes: 9 additions & 12 deletions tests/Unit/Module/DataType/ModuleDataTypeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ class ModuleDataTypeFactoryTest extends UnitTestCase
{
public function testCreateFromCoreModule()
{
$expectedTitle = 'Title in de language';
$expectedDescription = 'Description in de language';

$titlesData = [
'de' => 'Title in de language',
'en' => 'Title in en language',
'de' => uniqid(),
'en' => uniqid(),
];
$descriptionData = [
'de' => 'Description in de language',
'en' => 'Description in en language',
'de' => uniqid(),
'en' => uniqid(),
];
$expectedId = uniqid();
$expectedVersion = uniqid();
Expand All @@ -49,23 +46,23 @@ public function testCreateFromCoreModule()
$moduleConfigMock->method('getAuthor')->willReturn($expectedAuthor);
$moduleConfigMock->method('getUrl')->willReturn($expectedUrl);
$moduleConfigMock->method('getEmail')->willReturn($expectedEmail);
$moduleConfigMock->method('isActivated')->willReturn(true);
$moduleConfigMock->method('isActivated')->willReturn((bool)random_int(0, 1));

$languageServiceMock = $this->createMock(LanguageService::class);
$languageServiceMock
->method('filterByLanguageAbbreviation')
->willReturnMap([
[$titlesData, $expectedTitle],
[$descriptionData, $expectedDescription]
[$titlesData, $titlesData['de']],
[$descriptionData, $descriptionData['de']]
]);

$moduleDataTypeFactory = new ModuleDataTypeFactory($languageServiceMock);
$moduleDataType = $moduleDataTypeFactory->createFromCoreModule(moduleConfig: $moduleConfigMock);

$this->assertSame($expectedId, $moduleDataType->getId());
$this->assertSame($expectedVersion, $moduleDataType->getVersion());
$this->assertSame($expectedTitle, $moduleDataType->getTitle());
$this->assertSame($expectedDescription, $moduleDataType->getDescription());
$this->assertSame($titlesData['de'], $moduleDataType->getTitle());
$this->assertSame($descriptionData['de'], $moduleDataType->getDescription());
$this->assertSame($expectedThumbnail, $moduleDataType->getThumbnail());
$this->assertSame($expectedAuthor, $moduleDataType->getAuthor());
$this->assertSame($expectedUrl, $moduleDataType->getUrl());
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Module/DataType/ModuleDataTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testModuleDataType(): void
$author = uniqid();
$url = uniqid();
$email = uniqid();
$active = true;
$active = (bool)random_int(0, 1);

$moduleDataType = new ModuleDataType(
id: $id,
Expand Down
12 changes: 5 additions & 7 deletions tests/Unit/Module/DataType/ModuleFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,28 @@ class ModuleFiltersTest extends TestCase
{
/** @dataProvider moduleByTitleDataProvider */
public function testFilterModuleByTitle(
string $expectedTitle,
bool $expectedResult,
bool $enableFilters,
): void {
$title = uniqid();
$stringFilterMock = $this->createMock(StringFilter::class);
$stringFilterMock->method('matches')->willReturn($expectedResult);
$stringFilterMock->expects($this->once())->method('matches')->with($title)->willReturn($expectedResult);

$moduleMock = $this->createMock(ModuleDataType::class);
$moduleMock->method('getTitle')->willReturn($expectedTitle);
$moduleMock->expects($this->once())->method('getTitle')->willReturn($title);

$moduleFilters = ($enableFilters) ? new ModuleFilters(titleFilter: $stringFilterMock) : new ModuleFilters();
$this->assertEquals($expectedResult, $moduleFilters->filterModuleByTitle($moduleMock));
}

public static function moduleByTitleDataProvider(): \Generator
{
yield "filter module by titles matches" => [
'expectedTitle' => 'test module 1',
yield "filter module by title matches" => [
'expectedResult' => true,
'enableFilters' => true
];

yield "filter module by titles do not matches" => [
'expectedTitle' => 'random module title',
yield "filter module by title do not matches" => [
'expectedResult' => false,
'enableFilters' => true
];
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Theme/Service/ThemeFilterServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function testFilterThemes(
*/
public static function themeFilterResultProvider(): \Generator
{
$theme1 = new ThemeDataType('theme1', uniqid(), uniqid(), uniqid(), true);
$theme2 = new ThemeDataType('theme2', uniqid(), uniqid(), uniqid(), false);
$theme1 = new ThemeDataType(uniqid(), uniqid(), uniqid(), uniqid(), true);
$theme2 = new ThemeDataType(uniqid(), uniqid(), uniqid(), uniqid(), false);

yield "filter with only second title check is true but status check is false" => [
'themeList' => [$theme1, $theme2],
Expand Down

0 comments on commit 78fbcd2

Please sign in to comment.