Skip to content

Commit

Permalink
WIP: Part VI (Unit tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nattfarinn committed Dec 12, 2023
1 parent ecc9812 commit 5d0ca46
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 39 deletions.
14 changes: 7 additions & 7 deletions src/lib/Persistence/Legacy/Content/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public function extractContentFromRows(array $rows, array $nameRows, $prefix = '

foreach ($rows as $row) {
$contentId = (int)$row["{$prefix}id"];
$versionId = (int)$row["{$prefix}version_id"];
$contentTypeId = (int)$row["{$prefix}contentclass_id"];
if (!isset($contentInfos[$contentId])) {
$contentInfos[$contentId] = $this->extractContentInfoFromRow($row, $prefix);
Expand All @@ -214,26 +215,25 @@ public function extractContentFromRows(array $rows, array $nameRows, $prefix = '
$versionInfos[$contentId] = [];
}

if (!isset($fieldDefinitions[$contentId])) {
if (!isset($fieldDefinitions[$contentId][$versionId])) {
$contentType = $this->contentTypeHandler->load($contentTypeId);
foreach ($contentType->fieldDefinitions as $fieldDefinition) {
foreach ($languageCodes as $languageCode) {
$fieldDefinitions[$contentId][$languageCode][$fieldDefinition->id] = $fieldDefinition;
$fieldDefinitions[$contentId][$versionId][$languageCode][$fieldDefinition->id] = $fieldDefinition;
}
}
}

$versionId = (int)$row["{$prefix}version_id"];
if (!isset($versionInfos[$contentId][$versionId])) {
$versionInfos[$contentId][$versionId] = $this->extractVersionInfoFromRow($row);
}

$fieldId = (int)$row["{$prefix}attribute_id"];
$fieldDefinitionId = (int)$row["{$prefix}attribute_contentclassattribute_id"];
$languageCode = $row["{$prefix}attribute_language_code"];
if (!isset($fields[$contentId][$versionId][$fieldId]) && isset($fieldDefinitions[$contentId][$languageCode][$fieldDefinitionId])) {
if (!isset($fields[$contentId][$versionId][$fieldId]) && isset($fieldDefinitions[$contentId][$versionId][$languageCode][$fieldDefinitionId])) {
$fields[$contentId][$versionId][$fieldId] = $this->extractFieldFromRow($row);
unset($fieldDefinitions[$contentId][$languageCode][$fieldDefinitionId]);
unset($fieldDefinitions[$contentId][$versionId][$languageCode][$fieldDefinitionId]);
}
}

Expand All @@ -253,8 +253,8 @@ public function extractContentFromRows(array $rows, array $nameRows, $prefix = '
$content->versionInfo->contentInfo = $contentInfo;
$content->fields = array_values($fields[$contentId][$versionId]);

foreach ($fieldDefinitions[$contentId] as $languageCode => $fieldDefinitions) {
foreach ($fieldDefinitions as $fieldDefinition) {
foreach ($fieldDefinitions[$contentId][$versionId] as $languageCode => $versionFieldDefinitions) {
foreach ($versionFieldDefinitions as $fieldDefinition) {
$content->fields[] = $this->createEmptyField(
$fieldDefinition,
$languageCode

Check failure on line 260 in src/lib/Persistence/Legacy/Content/Mapper.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Parameter #2 $languageCode of method Ibexa\Core\Persistence\Legacy\Content\Mapper::createEmptyField() expects string, int|string given.

Check failure on line 260 in src/lib/Persistence/Legacy/Content/Mapper.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Parameter #2 $languageCode of method Ibexa\Core\Persistence\Legacy\Content\Mapper::createEmptyField() expects string, int|string given.

Check failure on line 260 in src/lib/Persistence/Legacy/Content/Mapper.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Parameter #2 $languageCode of method Ibexa\Core\Persistence\Legacy\Content\Mapper::createEmptyField() expects string, int|string given.
Expand Down
49 changes: 40 additions & 9 deletions tests/lib/Persistence/Legacy/Content/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
namespace Ibexa\Tests\Core\Persistence\Legacy\Content;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\MockType;
use function count;
use Ibexa\Contracts\Core\Persistence\Content;
use Ibexa\Contracts\Core\Persistence\Content\ContentInfo;
Expand Down Expand Up @@ -154,6 +156,12 @@ public function testExtractContentFromRows()
$rowsFixture = $this->getContentExtractFixture();
$nameRowsFixture = $this->getNamesExtractFixture();

$contentType = $this->getContentTypeFromRows($rowsFixture);

/** @var MockObject $contentTypeHandlerMock */
$contentTypeHandlerMock = $this->getContentTypeHandler();
$contentTypeHandlerMock->method('load')->willReturn($contentType);

$convMock = $this->createMock(Converter::class);
$convMock->expects($this->exactly(count($rowsFixture)))
->method('toFieldValue')
Expand All @@ -178,7 +186,7 @@ public function testExtractContentFromRows()
]
);

$mapper = new Mapper($reg, $this->getLanguageHandler(), $this->getContentTypeHandler());
$mapper = new Mapper($reg, $this->getLanguageHandler(), $contentTypeHandlerMock);

Check failure on line 189 in tests/lib/Persistence/Legacy/Content/MapperTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Parameter #3 $contentTypeHandler of class Ibexa\Core\Persistence\Legacy\Content\Mapper constructor expects Ibexa\Contracts\Core\Persistence\Content\Type\Handler, PHPUnit\Framework\MockObject\MockObject given.

Check failure on line 189 in tests/lib/Persistence/Legacy/Content/MapperTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Parameter #3 $contentTypeHandler of class Ibexa\Core\Persistence\Legacy\Content\Mapper constructor expects Ibexa\Contracts\Core\Persistence\Content\Type\Handler, PHPUnit\Framework\MockObject\MockObject given.

Check failure on line 189 in tests/lib/Persistence/Legacy/Content/MapperTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Parameter #3 $contentTypeHandler of class Ibexa\Core\Persistence\Legacy\Content\Mapper constructor expects Ibexa\Contracts\Core\Persistence\Content\Type\Handler, PHPUnit\Framework\MockObject\MockObject given.
$result = $mapper->extractContentFromRows($rowsFixture, $nameRowsFixture);

$this->assertEquals(
Expand Down Expand Up @@ -206,7 +214,13 @@ public function testExtractContentFromRowsMultipleVersions()
$rowsFixture = $this->getMultipleVersionsExtractFixture();
$nameRowsFixture = $this->getMultipleVersionsNamesExtractFixture();

$mapper = new Mapper($reg, $this->getLanguageHandler(), $this->getContentTypeHandler());
$contentType = $this->getContentTypeFromRows($rowsFixture);

/** @var MockObject $contentTypeHandlerMock */
$contentTypeHandlerMock = $this->getContentTypeHandler();
$contentTypeHandlerMock->method('load')->willReturn($contentType);

$mapper = new Mapper($reg, $this->getLanguageHandler(), $contentTypeHandlerMock);

Check failure on line 223 in tests/lib/Persistence/Legacy/Content/MapperTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Parameter #3 $contentTypeHandler of class Ibexa\Core\Persistence\Legacy\Content\Mapper constructor expects Ibexa\Contracts\Core\Persistence\Content\Type\Handler, PHPUnit\Framework\MockObject\MockObject given.

Check failure on line 223 in tests/lib/Persistence/Legacy/Content/MapperTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Parameter #3 $contentTypeHandler of class Ibexa\Core\Persistence\Legacy\Content\Mapper constructor expects Ibexa\Contracts\Core\Persistence\Content\Type\Handler, PHPUnit\Framework\MockObject\MockObject given.

Check failure on line 223 in tests/lib/Persistence/Legacy/Content/MapperTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Parameter #3 $contentTypeHandler of class Ibexa\Core\Persistence\Legacy\Content\Mapper constructor expects Ibexa\Contracts\Core\Persistence\Content\Type\Handler, PHPUnit\Framework\MockObject\MockObject given.
$result = $mapper->extractContentFromRows($rowsFixture, $nameRowsFixture);

$this->assertCount(
Expand Down Expand Up @@ -608,14 +622,31 @@ static function ($languageCode) use ($languages) {

protected function getContentTypeHandler(): Content\Type\Handler
{
$mock = $this->createMock(Content\Type\Handler::class);
$mock
->method('load')
->willReturn(
$this->createMock(Content\Type::class)
);
return $this->createMock(Content\Type\Handler::class);
}

protected function getContentTypeFromRows(array $rows): Content\Type

Check failure on line 628 in tests/lib/Persistence/Legacy/Content/MapperTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Method Ibexa\Tests\Core\Persistence\Legacy\Content\MapperTest::getContentTypeFromRows() has parameter $rows with no value type specified in iterable type array.

Check failure on line 628 in tests/lib/Persistence/Legacy/Content/MapperTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Method Ibexa\Tests\Core\Persistence\Legacy\Content\MapperTest::getContentTypeFromRows() has parameter $rows with no value type specified in iterable type array.

Check failure on line 628 in tests/lib/Persistence/Legacy/Content/MapperTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Method Ibexa\Tests\Core\Persistence\Legacy\Content\MapperTest::getContentTypeFromRows() has parameter $rows with no value type specified in iterable type array.
{
$contentType = new Content\Type();
$fieldDefinitions = [];

foreach ($rows as $row) {
$fieldDefinitionId = $row['ezcontentobject_attribute_contentclassattribute_id'];
$fieldType = $row['ezcontentobject_attribute_data_type_string'];

if (isset($fieldDefinitions[$fieldDefinitionId])) {
continue;
}

$fieldDefinitions[$fieldDefinitionId] = new Content\Type\FieldDefinition([
'id' => $fieldDefinitionId,
'fieldType' => $fieldType,
]);
}

$contentType->fieldDefinitions = array_values($fieldDefinitions);

return $mock;
return $contentType;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1005,12 +1005,9 @@ public function testPublish()
$this->returnValue(new Type())
);

$updateHandlerMock->expects($this->once())
->method('updateContentObjects')
->with(
$this->isInstanceOf(Type::class),
$this->isInstanceOf(Type::class)
);
$updateHandlerMock->expects($this->never())
->method('updateContentObjects');

$updateHandlerMock->expects($this->once())
->method('deleteOldType')
->with(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,11 @@ public function testUpdateContentObjects()

$updaterMock = $this->getContentUpdaterMock();

$updaterMock->expects($this->once())
->method('determineActions')
->with(
$this->isInstanceOf(
Type::class
),
$this->isInstanceOf(
Type::class
)
)->will($this->returnValue([]));

$updaterMock->expects($this->once())
->method('applyUpdates')
->with(
$this->equalTo(23),
$this->equalTo([])
);
$updaterMock->expects($this->never())
->method('determineActions');

$updaterMock->expects($this->never())
->method('applyUpdates');

$types = $this->getTypeFixtures();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,60 @@

$content->fields[] = $field;

$field = new Field();
$field->fieldDefinitionId = 183;
$field->type = 'ezstring';
$field->value = new FieldValue();
$field->languageCode = 'eng-GB';

$content->fields[] = $field;

$field = new Field();
$field->fieldDefinitionId = 184;
$field->type = 'ezstring';
$field->value = new FieldValue();
$field->languageCode = 'eng-GB';

$content->fields[] = $field;

$field = new Field();
$field->fieldDefinitionId = 185;
$field->type = 'ezauthor';
$field->value = new FieldValue();
$field->languageCode = 'eng-GB';

$content->fields[] = $field;

$field = new Field();
$field->fieldDefinitionId = 188;
$field->type = 'ezboolean';
$field->value = new FieldValue();
$field->languageCode = 'eng-GB';

$content->fields[] = $field;

$field = new Field();
$field->fieldDefinitionId = 189;
$field->type = 'ezimage';
$field->value = new FieldValue();
$field->languageCode = 'eng-GB';

$content->fields[] = $field;

$field = new Field();
$field->fieldDefinitionId = 191;
$field->type = 'ezdatetime';
$field->value = new FieldValue();
$field->languageCode = 'eng-GB';

$content->fields[] = $field;

$field = new Field();
$field->fieldDefinitionId = 192;
$field->type = 'ezdatetime';
$field->value = new FieldValue();
$field->languageCode = 'eng-GB';

$content->fields[] = $field;

return $content;

0 comments on commit 5d0ca46

Please sign in to comment.