From 02ae70f10ad5a1cffc0f1d9e6512ffac8f27eb0a Mon Sep 17 00:00:00 2001 From: Stefan Krenz Date: Mon, 2 May 2022 14:08:19 +0200 Subject: [PATCH] feat: Add test --- .../Extractor/ArticleSelectListTest.php | 105 ++++++++++++++++++ .../Extractor/CategoryTest.php | 2 - .../Extractor/GraduatedPricesTest.php | 1 - 3 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 tests/Unit/RevisionHandler/Extractor/ArticleSelectListTest.php diff --git a/tests/Unit/RevisionHandler/Extractor/ArticleSelectListTest.php b/tests/Unit/RevisionHandler/Extractor/ArticleSelectListTest.php new file mode 100644 index 0000000..15e974a --- /dev/null +++ b/tests/Unit/RevisionHandler/Extractor/ArticleSelectListTest.php @@ -0,0 +1,105 @@ +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] + ]; + } +} diff --git a/tests/Unit/RevisionHandler/Extractor/CategoryTest.php b/tests/Unit/RevisionHandler/Extractor/CategoryTest.php index 7565ff7..947a451 100644 --- a/tests/Unit/RevisionHandler/Extractor/CategoryTest.php +++ b/tests/Unit/RevisionHandler/Extractor/CategoryTest.php @@ -12,8 +12,6 @@ use OxidEsales\Eshop\Core\TableViewNameGenerator; use OxidEsales\TestingLibrary\UnitTestCase; -use function var_export; - class CategoryTest extends UnitTestCase { public function testItSupportsCategoryModel() diff --git a/tests/Unit/RevisionHandler/Extractor/GraduatedPricesTest.php b/tests/Unit/RevisionHandler/Extractor/GraduatedPricesTest.php index 3ed2162..875d0ef 100644 --- a/tests/Unit/RevisionHandler/Extractor/GraduatedPricesTest.php +++ b/tests/Unit/RevisionHandler/Extractor/GraduatedPricesTest.php @@ -88,5 +88,4 @@ public function provideTestData() 'Testing variant' => ['phpunit21', Revision::TYPE_VARIANT] ]; } - }