From 9b783e5e8980e063387e7b2e133e84eaaf9ccb47 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Wed, 3 Aug 2022 06:03:06 +0000 Subject: [PATCH] Ensure no "Undefined array key" within factories --- .../Domain/Model/Dto/DateDemandFactory.php | 34 +++++++-- .../Domain/Model/Dto/EventDemandFactory.php | 33 +++++++-- .../Model/Dto/DateDemandFactoryTest.php | 74 +++++++++++++++++++ .../Model/Dto/EventDemandFactoryTest.php | 62 ++++++++++++++++ 4 files changed, 187 insertions(+), 16 deletions(-) create mode 100644 Tests/Unit/Domain/Model/Dto/DateDemandFactoryTest.php create mode 100644 Tests/Unit/Domain/Model/Dto/EventDemandFactoryTest.php diff --git a/Classes/Domain/Model/Dto/DateDemandFactory.php b/Classes/Domain/Model/Dto/DateDemandFactory.php index b006dc8..711866b 100644 --- a/Classes/Domain/Model/Dto/DateDemandFactory.php +++ b/Classes/Domain/Model/Dto/DateDemandFactory.php @@ -60,15 +60,33 @@ class DateDemandFactory } } - $demand->setRegions(GeneralUtility::intExplode(',', (string)$settings['region'], true)); - $demand->setLocations(GeneralUtility::intExplode(',', (string)$settings['locations'], true)); - $demand->setCategories((string)$settings['categories']); - $categoryCombination = (int)$settings['categoryCombination'] === 1 ? 'or' : 'and'; + if (!empty($settings['region'])) { + $demand->setRegions(GeneralUtility::intExplode(',', (string)$settings['region'], true)); + } + if (!empty($settings['locations'])) { + $demand->setLocations(GeneralUtility::intExplode(',', (string)$settings['locations'], true)); + } + if (!empty($settings['categories'])) { + $demand->setCategories((string)$settings['categories']); + } + $categoryCombination = 'and'; + if (isset($settings['categoryCombination']) && (int)$settings['categoryCombination'] === 1) { + $categoryCombination = 'or'; + } $demand->setCategoryCombination($categoryCombination); - $demand->setIncludeSubCategories((bool)$settings['includeSubcategories']); - $demand->setSortBy((string)$settings['sortByDate']); - $demand->setSortOrder((string)$settings['sortOrder']); - $demand->setHighlight((bool)$settings['highlight']); + + if (isset($settings['includeSubcategories'])) { + $demand->setIncludeSubCategories((bool)$settings['includeSubcategories']); + } + if (isset($settings['sortByDate'])) { + $demand->setSortBy((string)$settings['sortByDate']); + } + if (!empty($settings['sortOrder'])) { + $demand->setSortOrder((string)$settings['sortOrder']); + } + if (isset($settings['highlight'])) { + $demand->setHighlight((bool)$settings['highlight']); + } if (!empty($settings['start'])) { $demand->setStart((int)$settings['start']); } diff --git a/Classes/Domain/Model/Dto/EventDemandFactory.php b/Classes/Domain/Model/Dto/EventDemandFactory.php index 7011fc7..4952270 100644 --- a/Classes/Domain/Model/Dto/EventDemandFactory.php +++ b/Classes/Domain/Model/Dto/EventDemandFactory.php @@ -30,24 +30,41 @@ class EventDemandFactory /** @var EventDemand $demand */ $demand = GeneralUtility::makeInstance(EventDemand::class); - $demand->setRegion((string)$settings['region']); + if (!empty($settings['region'])) { + $demand->setRegion((string)$settings['region']); + } - $demand->setCategories((string)$settings['categories']); + if (!empty($settings['categories'])) { + $demand->setCategories((string)$settings['categories']); + } $categoryCombination = 'and'; - if ((int)$settings['categoryCombination'] === 1) { + if ( + isset($settings['categoryCombination']) + && (int)$settings['categoryCombination'] === 1 + ) { $categoryCombination = 'or'; } $demand->setCategoryCombination($categoryCombination); - $demand->setIncludeSubCategories((bool)$settings['includeSubcategories']); + if (isset($settings['includeSubcategories'])) { + $demand->setIncludeSubCategories((bool)$settings['includeSubcategories']); + } - $demand->setSortBy((string)$settings['sortByEvent']); - $demand->setSortOrder((string)$settings['sortOrder']); + if (!empty($settings['sortByEvent'])) { + $demand->setSortBy((string)$settings['sortByEvent']); + } + if (!empty($settings['sortOrder'])) { + $demand->setSortOrder((string)$settings['sortOrder']); + } - $demand->setHighlight((bool)$settings['highlight']); + if (isset($settings['highlight'])) { + $demand->setHighlight((bool)$settings['highlight']); + } - $demand->setRecordUids(GeneralUtility::intExplode(',', $settings['selectedRecords'], true)); + if (!empty($settings['selectedRecords'])) { + $demand->setRecordUids(GeneralUtility::intExplode(',', $settings['selectedRecords'], true)); + } if (!empty($settings['limit'])) { $demand->setLimit($settings['limit']); diff --git a/Tests/Unit/Domain/Model/Dto/DateDemandFactoryTest.php b/Tests/Unit/Domain/Model/Dto/DateDemandFactoryTest.php new file mode 100644 index 0000000..76f0420 --- /dev/null +++ b/Tests/Unit/Domain/Model/Dto/DateDemandFactoryTest.php @@ -0,0 +1,74 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +namespace Wrm\Events\Tests\Unit\Domain\Model\Dto; + +use PHPUnit\Framework\TestCase; +use TYPO3\CMS\Core\TypoScript\TypoScriptService; +use Wrm\Events\Domain\Model\Dto\DateDemand; +use Wrm\Events\Domain\Model\Dto\DateDemandFactory; +use Wrm\Events\Tests\ProphecyTrait; + +/** + * @covers \Wrm\Events\Domain\Model\Dto\DateDemandFactory + */ +class DateDemandFactoryTest extends TestCase +{ + use ProphecyTrait; + + /** + * @test + */ + public function canBeCreated(): void + { + $typoScriptService = $this->prophesize(TypoScriptService::class); + + $subject = new DateDemandFactory( + $typoScriptService->reveal() + ); + + self::assertInstanceOf( + DateDemandFactory::class, + $subject + ); + } + + /** + * @test + */ + public function fromSettingsDoesNotThrowUndefinedArrayKeyWarnings(): void + { + $typoScriptService = $this->prophesize(TypoScriptService::class); + + $subject = new DateDemandFactory( + $typoScriptService->reveal() + ); + + $result = $subject->fromSettings([]); + + self::assertInstanceOf( + DateDemand::class, + $result + ); + } +} diff --git a/Tests/Unit/Domain/Model/Dto/EventDemandFactoryTest.php b/Tests/Unit/Domain/Model/Dto/EventDemandFactoryTest.php new file mode 100644 index 0000000..3324ee2 --- /dev/null +++ b/Tests/Unit/Domain/Model/Dto/EventDemandFactoryTest.php @@ -0,0 +1,62 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +namespace Wrm\Events\Tests\Unit\Domain\Model\Dto; + +use Wrm\Events\Domain\Model\Dto\EventDemand; +use Wrm\Events\Domain\Model\Dto\EventDemandFactory; +use PHPUnit\Framework\TestCase; + +/** + * @covers \Wrm\Events\Domain\Model\Dto\EventDemandFactory + */ +class EventDemandFactoryTest extends TestCase +{ + /** + * @test + */ + public function canBeCreated(): void + { + $subject = new EventDemandFactory(); + + self::assertInstanceOf( + EventDemandFactory::class, + $subject + ); + } + + /** + * @test + */ + public function fromSettingsDoesNotThrowUndefinedArrayKeyWarnings(): void + { + $subject = new EventDemandFactory(); + + $result = $subject->fromSettings([]); + + self::assertInstanceOf( + EventDemand::class, + $result + ); + } +}