|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Extcode\CartProducts\Tests\Functional\Domain\DoctrineRepository\Product; |
| 4 | + |
| 5 | +use Codappix\Typo3PhpDatasets\TestingFramework; |
| 6 | +use Extcode\CartProducts\Domain\DoctrineRepository\Product\ProductRepository; |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\Attributes\Test; |
| 9 | +use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder; |
| 10 | +use TYPO3\CMS\Core\Database\ConnectionPool; |
| 11 | +use TYPO3\CMS\Core\Http\ServerRequest; |
| 12 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 13 | +use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; |
| 14 | + |
| 15 | +#[CoversClass(ProductRepository::class)] |
| 16 | +class ProductRepositoryTest extends FunctionalTestCase |
| 17 | +{ |
| 18 | + use TestingFramework; |
| 19 | + |
| 20 | + protected ProductRepository $productRepository; |
| 21 | + |
| 22 | + public function setUp(): void |
| 23 | + { |
| 24 | + $this->testExtensionsToLoad[] = 'extcode/cart'; |
| 25 | + $this->testExtensionsToLoad[] = 'extcode/cart-products'; |
| 26 | + |
| 27 | + $this->coreExtensionsToLoad[] = 'typo3/cms-reactions'; |
| 28 | + |
| 29 | + parent::setUp(); |
| 30 | + |
| 31 | + $GLOBALS['TYPO3_REQUEST'] = (new ServerRequest()) |
| 32 | + ->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_BE); |
| 33 | + |
| 34 | + $this->productRepository = GeneralUtility::makeInstance( |
| 35 | + ProductRepository::class, |
| 36 | + GeneralUtility::makeInstance( |
| 37 | + ConnectionPool::class |
| 38 | + ) |
| 39 | + ); |
| 40 | + |
| 41 | + $this->importPHPDataSet(__DIR__ . '/../../../Fixtures/Pages.php'); |
| 42 | + $this->importPHPDataSet(__DIR__ . '/../../../Fixtures/Products.php'); |
| 43 | + } |
| 44 | + |
| 45 | + #[Test] |
| 46 | + public function returnsFalseIfProductHasNoImage(): void |
| 47 | + { |
| 48 | + self::assertFalse( |
| 49 | + $this->productRepository->findFirstProductImageUid(1) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + #[Test] |
| 54 | + public function returnsUidOfImageIfProductHasOnlyOneImage(): void |
| 55 | + { |
| 56 | + $this->importPhpDataSet(__DIR__ . '/Fixtures/OneImageFileReference.php'); |
| 57 | + |
| 58 | + self::assertSame( |
| 59 | + 42, |
| 60 | + $this->productRepository->findFirstProductImageUid(1) |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + #[Test] |
| 65 | + public function returnsFirstUidOfImageIfProductHasMoreThanOneImage(): void |
| 66 | + { |
| 67 | + $this->importPhpDataSet(__DIR__ . '/Fixtures/TwoImageFileReference.php'); |
| 68 | + |
| 69 | + self::assertSame( |
| 70 | + 42, |
| 71 | + $this->productRepository->findFirstProductImageUid(1) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + #[Test] |
| 76 | + public function returnsSecondUidOfImageIfProductHasMoreThanOneImageAndRespectsSorting(): void |
| 77 | + { |
| 78 | + $this->importPhpDataSet(__DIR__ . '/Fixtures/TwoImageFileReference.php'); |
| 79 | + |
| 80 | + $this |
| 81 | + ->getConnectionPool() |
| 82 | + ->getConnectionForTable('sys_file_reference') |
| 83 | + ->update( |
| 84 | + 'sys_file_reference', |
| 85 | + [ |
| 86 | + 'sorting_foreign' => 128, |
| 87 | + ], |
| 88 | + [ |
| 89 | + 'uid' => 1, |
| 90 | + ] |
| 91 | + ) |
| 92 | + ; |
| 93 | + $this |
| 94 | + ->getConnectionPool() |
| 95 | + ->getConnectionForTable('sys_file_reference') |
| 96 | + ->update( |
| 97 | + 'sys_file_reference', |
| 98 | + [ |
| 99 | + 'sorting_foreign' => 16, |
| 100 | + ], |
| 101 | + [ |
| 102 | + 'uid' => 2, |
| 103 | + ] |
| 104 | + ) |
| 105 | + ; |
| 106 | + |
| 107 | + self::assertSame( |
| 108 | + 37, |
| 109 | + $this->productRepository->findFirstProductImageUid(1) |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + #[Test] |
| 114 | + public function returnsSecondUidOfImageIfProductHasMoreThanOneImageAndFirstIsDeleted(): void |
| 115 | + { |
| 116 | + $this->importPhpDataSet(__DIR__ . '/Fixtures/TwoImageFileReference.php'); |
| 117 | + |
| 118 | + $this |
| 119 | + ->getConnectionPool() |
| 120 | + ->getConnectionForTable('sys_file_reference') |
| 121 | + ->update( |
| 122 | + 'sys_file_reference', |
| 123 | + [ |
| 124 | + 'deleted' => 1, |
| 125 | + ], |
| 126 | + [ |
| 127 | + 'uid' => 1, |
| 128 | + ] |
| 129 | + ) |
| 130 | + ; |
| 131 | + |
| 132 | + self::assertSame( |
| 133 | + 37, |
| 134 | + $this->productRepository->findFirstProductImageUid(1) |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + #[Test] |
| 139 | + public function returnsSecondUidOfImageIfProductHasMoreThanOneImageAndFirstIsHidden(): void |
| 140 | + { |
| 141 | + $this->importPhpDataSet(__DIR__ . '/Fixtures/TwoImageFileReference.php'); |
| 142 | + |
| 143 | + $this |
| 144 | + ->getConnectionPool() |
| 145 | + ->getConnectionForTable('sys_file_reference') |
| 146 | + ->update( |
| 147 | + 'sys_file_reference', |
| 148 | + [ |
| 149 | + 'hidden' => 1, |
| 150 | + ], |
| 151 | + [ |
| 152 | + 'uid' => 1, |
| 153 | + ] |
| 154 | + ) |
| 155 | + ; |
| 156 | + |
| 157 | + self::assertSame( |
| 158 | + 37, |
| 159 | + $this->productRepository->findFirstProductImageUid(1) |
| 160 | + ); |
| 161 | + } |
| 162 | +} |
0 commit comments