Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ jobs:
- coding-guideline
- code-quality
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v5

- uses: cachix/install-nix-action@v17
- uses: cachix/install-nix-action@v31
with:
nix_path: nixpkgs=channel:nixos-unstable

Expand Down Expand Up @@ -138,9 +138,9 @@ jobs:
needs:
- test-php
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v5

- uses: cachix/install-nix-action@v17
- uses: cachix/install-nix-action@v31
with:
nix_path: nixpkgs=channel:nixos-unstable

Expand Down
27 changes: 27 additions & 0 deletions Classes/Domain/DoctrineRepository/Product/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ public function findProductByUid(int $uid): array|bool
->fetchAssociative();
}

public function findFirstProductImageUid(int $uid): int|bool
{
$queryBuilder = $this
->connectionPool
->getConnectionForTable('sys_file_reference')
->createQueryBuilder()
;

$queryBuilder
->select('uid')
->from('sys_file_reference')
->where(
$queryBuilder->expr()->and(
$queryBuilder->expr()->eq('tablenames', $queryBuilder->createNamedParameter(self::TABLENAME)),
$queryBuilder->expr()->eq('fieldname', $queryBuilder->createNamedParameter('images')),
$queryBuilder->expr()->eq('uid_foreign', $queryBuilder->createNamedParameter($uid)),
)
)
->orderBy('sorting_foreign')
->setMaxResults(1);

return $queryBuilder
->executeQuery()
->fetchOne()
;
}

public function getStock(int $uid): int
{
$queryBuilder = $this->getQueryBuilder();
Expand Down
12 changes: 2 additions & 10 deletions Classes/Domain/Model/WatchlistItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,10 @@ public function createFromIdentifier(

private function getFirstImageReference(array $product): ?int
{
if (is_string($product['images'] ?? null) === false || $product['images'] === '') {
if ((int)$product['images'] === 0) {
return null;
}

$images = explode(',', $product['images']);

$image = array_pop($images);

if (is_numeric($image) === false) {
return null;
}

return (int)$image;
return $this->productRepository->findFirstProductImageUid($product['uid']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

return [
'sys_file_reference' => [
0 => [
'uid' => 1,
'pid' => 7,
'uid_local' => 42,
'uid_foreign' => 1,
'tablenames' => 'tx_cartproducts_domain_model_product_product',
'fieldname' => 'images',
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

return [
'sys_file_reference' => [
0 => [
'uid' => 5,
'pid' => 7,
'uid_local' => 42,
'uid_foreign' => 1,
'tablenames' => 'tx_cartproducts_domain_model_product_product',
'fieldname' => 'images',
],
1 => [
'uid' => 16,
'pid' => 7,
'uid_local' => 37,
'uid_foreign' => 1,
'tablenames' => 'tx_cartproducts_domain_model_product_product',
'fieldname' => 'images',
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

namespace Extcode\CartProducts\Tests\Functional\Domain\DoctrineRepository\Product;

use Codappix\Typo3PhpDatasets\TestingFramework;
use Extcode\CartProducts\Domain\DoctrineRepository\Product\ProductRepository;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

#[CoversClass(ProductRepository::class)]
class ProductRepositoryTest extends FunctionalTestCase
{
use TestingFramework;

protected ProductRepository $productRepository;

public function setUp(): void
{
$this->testExtensionsToLoad[] = 'extcode/cart';
$this->testExtensionsToLoad[] = 'extcode/cart-products';

$this->coreExtensionsToLoad[] = 'typo3/cms-reactions';

parent::setUp();

$GLOBALS['TYPO3_REQUEST'] = (new ServerRequest())
->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_BE);

$this->productRepository = GeneralUtility::makeInstance(
ProductRepository::class,
GeneralUtility::makeInstance(
ConnectionPool::class
)
);

$this->importPHPDataSet(__DIR__ . '/../../../Fixtures/Pages.php');
$this->importPHPDataSet(__DIR__ . '/../../../Fixtures/Products.php');
}

#[Test]
public function returnsFalseIfProductHasNoImage(): void
{
self::assertFalse(
$this->productRepository->findFirstProductImageUid(1)
);
}

#[Test]
public function returnsUidOfImageIfProductHasOnlyOneImage(): void
{
$this->importPhpDataSet(__DIR__ . '/Fixtures/OneImageFileReference.php');

self::assertSame(
1,
$this->productRepository->findFirstProductImageUid(1)
);
}

#[Test]
public function returnsFirstUidOfImageIfProductHasMoreThanOneImage(): void
{
$this->importPhpDataSet(__DIR__ . '/Fixtures/TwoImageFileReference.php');

self::assertSame(
5,
$this->productRepository->findFirstProductImageUid(1)
);
}

#[Test]
public function returnsSecondUidOfImageIfProductHasMoreThanOneImageAndRespectsSorting(): void
{
$this->importPhpDataSet(__DIR__ . '/Fixtures/TwoImageFileReference.php');

$this
->getConnectionPool()
->getConnectionForTable('sys_file_reference')
->update(
'sys_file_reference',
[
'sorting_foreign' => 128,
],
[
'uid' => 5,
]
)
;
$this
->getConnectionPool()
->getConnectionForTable('sys_file_reference')
->update(
'sys_file_reference',
[
'sorting_foreign' => 16,
],
[
'uid' => 16,
]
)
;

self::assertSame(
16,
$this->productRepository->findFirstProductImageUid(1)
);
}

#[Test]
public function returnsSecondUidOfImageIfProductHasMoreThanOneImageAndFirstIsDeleted(): void
{
$this->importPhpDataSet(__DIR__ . '/Fixtures/TwoImageFileReference.php');

$this
->getConnectionPool()
->getConnectionForTable('sys_file_reference')
->update(
'sys_file_reference',
[
'deleted' => 1,
],
[
'uid' => 5,
]
)
;

self::assertSame(
16,
$this->productRepository->findFirstProductImageUid(1)
);
}

#[Test]
public function returnsSecondUidOfImageIfProductHasMoreThanOneImageAndFirstIsHidden(): void
{
$this->importPhpDataSet(__DIR__ . '/Fixtures/TwoImageFileReference.php');

$this
->getConnectionPool()
->getConnectionForTable('sys_file_reference')
->update(
'sys_file_reference',
[
'hidden' => 1,
],
[
'uid' => 5,
]
)
;

self::assertSame(
16,
$this->productRepository->findFirstProductImageUid(1)
);
}
}
Loading