Skip to content

Commit

Permalink
feat: Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoya-de committed May 2, 2022
1 parent 4f54617 commit 02ae70f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 3 deletions.
105 changes: 105 additions & 0 deletions tests/Unit/RevisionHandler/Extractor/ArticleSelectListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Makaira\OxidConnectEssential\Test\Unit\RevisionHandler\Extractor;

use DateTimeImmutable;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Statement;
use Makaira\OxidConnectEssential\Domain\Revision;
use Makaira\OxidConnectEssential\RevisionHandler\Extractor\ArticleSelectList;
use OxidEsales\Eshop\Application\Model\Article as OxidArticle;
use OxidEsales\Eshop\Core\Model\BaseModel;
use OxidEsales\Eshop\Core\TableViewNameGenerator;
use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\TestCase;

class ArticleSelectListTest extends UnitTestCase
{
public function testItSupportsBaseModel()
{
$dataExtractor = new ArticleSelectList(
$this->createMock(Connection::class),
$this->createMock(TableViewNameGenerator::class)
);

$model = new BaseModel();
$model->init('oxobject2selectlist');

$actual = $dataExtractor->supports($model);
$this->assertTrue($actual);
}

public function testItDoesNotSupportProductModel()
{
$dataExtractor = new ArticleSelectList(
$this->createMock(Connection::class),
$this->createMock(TableViewNameGenerator::class)
);

$actual = $dataExtractor->supports(new OxidArticle());
$this->assertFalse($actual);
}

/**
* @param string $parentId
* @param string $expectedType
*
* @return void
* @throws \Doctrine\DBAL\Driver\Exception
* @throws \Doctrine\DBAL\Exception
* @dataProvider provideTestData
*/
public function testReturnsRevisionObject(string $parentId, string $expectedType)
{
$statementMock = $this->createMock(Statement::class);
$statementMock
->expects($this->once())
->method('execute')
->with(['phpunit42']);

$statementMock
->expects($this->once())
->method('fetchOne')
->willReturn($parentId);

$sql = "SELECT `OXPARENTID` FROM `phpunit_oxarticles_de` WHERE `OXID` = ?";

$db = $this->createMock(Connection::class);
$db->expects($this->once())
->method('prepare')
->with($sql)
->willReturn($statementMock);

$viewNameGenerator = $this->createMock(TableViewNameGenerator::class);
$viewNameGenerator
->expects($this->once())
->method('getViewName')
->with('oxarticles')
->willReturn('phpunit_oxarticles_de');

$model = $this->createMock(BaseModel::class);
$model->method('getRawFieldData')->with('oxobjectid')->willReturn('phpunit42');

$articleExtractor = new ArticleSelectList($db, $viewNameGenerator);
$actual = $articleExtractor->extract($model);

$changed = new DateTimeImmutable();

foreach ($actual as $revision) {
$revision->changed = $changed;
}

$expected = [
$expectedType . '-phpunit42' => new Revision($expectedType, 'phpunit42', $changed)
];
$this->assertEqualsCanonicalizing($expected, $actual);
}

public function provideTestData()
{
return [
'Testing product' => ['', Revision::TYPE_PRODUCT],
'Testing variant' => ['phpunit21', Revision::TYPE_VARIANT]
];
}
}
2 changes: 0 additions & 2 deletions tests/Unit/RevisionHandler/Extractor/CategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
use OxidEsales\Eshop\Core\TableViewNameGenerator;
use OxidEsales\TestingLibrary\UnitTestCase;

use function var_export;

class CategoryTest extends UnitTestCase
{
public function testItSupportsCategoryModel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,4 @@ public function provideTestData()
'Testing variant' => ['phpunit21', Revision::TYPE_VARIANT]
];
}

}

0 comments on commit 02ae70f

Please # to comment.