From 13c4a1876b85a5a8e189f9315d4c0c4cdec79717 Mon Sep 17 00:00:00 2001 From: Francisco Albert Date: Tue, 7 Nov 2017 12:14:44 +0000 Subject: [PATCH] Adding iSite Contact Details support to Programme Options (#210) --- src/Domain/Enumeration/ContactMediumEnum.php | 21 ++++++ src/Domain/ValueObject/ContactDetails.php | 43 +++++++++++ .../ProgrammesDbToDomain/OptionsMapper.php | 35 ++++++++- .../ProgrammesDbToDomain/CachingTest.php | 2 + .../OptionsMapperTest.php | 72 +++++++++++++++++-- 5 files changed, 166 insertions(+), 7 deletions(-) create mode 100644 src/Domain/Enumeration/ContactMediumEnum.php create mode 100644 src/Domain/ValueObject/ContactDetails.php diff --git a/src/Domain/Enumeration/ContactMediumEnum.php b/src/Domain/Enumeration/ContactMediumEnum.php new file mode 100644 index 00000000..406e2d79 --- /dev/null +++ b/src/Domain/Enumeration/ContactMediumEnum.php @@ -0,0 +1,21 @@ +type = $mediaType; + $this->value = $value; + $this->freetext = $freetext; + } + + public function getType(): string + { + return $this->type; + } + + public function getValue(): string + { + return $this->value; + } + + public function getFreetext(): string + { + return $this->freetext; + } +} diff --git a/src/Mapper/ProgrammesDbToDomain/OptionsMapper.php b/src/Mapper/ProgrammesDbToDomain/OptionsMapper.php index ad611e76..3fb342ea 100644 --- a/src/Mapper/ProgrammesDbToDomain/OptionsMapper.php +++ b/src/Mapper/ProgrammesDbToDomain/OptionsMapper.php @@ -3,6 +3,7 @@ namespace BBC\ProgrammesPagesService\Mapper\ProgrammesDbToDomain; use BBC\ProgrammesPagesService\Domain\Entity\Options; +use BBC\ProgrammesPagesService\Domain\ValueObject\ContactDetails; class OptionsMapper extends AbstractMapper { @@ -41,6 +42,9 @@ class OptionsMapper extends AbstractMapper 'bbc_site' => [ 'default' => null, 'cascades' => false ], 'livepromo_block' => [ 'default' => null, 'cascades' => false ], 'prioritytext_block' => [ 'default' => null, 'cascades' => false ], + + // contact options + 'contact_details' => [ 'default' => [], 'cascades' => false ], ]; private static $defaults = []; @@ -62,15 +66,42 @@ public function getDomainModel(array $options, array ...$parentEntities) // set default values $defaults = $this->getDefaultValues(); - foreach ($defaults as $key => $value) { + foreach ($defaults as $key => $defaultValue) { if (!isset($options[$key])) { - $options[$key] = $value; + $options[$key] = $defaultValue; + } + } + + if (isset($options['contact_details'])) { + $contacts = []; + + foreach ($options['contact_details'] as $contactDetails) { + if ($this->isValidContactDetails($contactDetails)) { + $contacts[] = new ContactDetails( + $contactDetails['type'], + $contactDetails['value'], + $contactDetails['freetext'] + ); + } + } + + if ($contacts) { + $options['contact_details'] = $contacts; } } return new Options($options); } + private function isValidContactDetails(array $contact): bool + { + return ( + isset($contact['type']) && + isset($contact['value']) && + isset($contact['freetext']) + ); + } + private function getDefaultValues() { if (!self::$defaults) { diff --git a/tests/Mapper/ProgrammesDbToDomain/CachingTest.php b/tests/Mapper/ProgrammesDbToDomain/CachingTest.php index ba518767..130539b9 100644 --- a/tests/Mapper/ProgrammesDbToDomain/CachingTest.php +++ b/tests/Mapper/ProgrammesDbToDomain/CachingTest.php @@ -3,6 +3,7 @@ namespace Tests\BBC\ProgrammesPagesService\Mapper\ProgrammesDbToDomain; use BBC\ProgrammesPagesService\Domain\Entity\Options; +use BBC\ProgrammesPagesService\Domain\Entity\ContactMediaMap; use BBC\ProgrammesPagesService\Mapper\ProgrammesDbToDomain\MapperFactory; class CachingTest extends BaseCoreEntityMapperTestCase @@ -63,6 +64,7 @@ public function testCacheAccountsForUnfetchedEntities() 'livepromo_block' => null, 'prioritytext_block' => null, 'navigation_links' => [], + 'contact_details' => [], ] ) ); diff --git a/tests/Mapper/ProgrammesDbToDomain/OptionsMapperTest.php b/tests/Mapper/ProgrammesDbToDomain/OptionsMapperTest.php index e19fc549..74dbfb86 100644 --- a/tests/Mapper/ProgrammesDbToDomain/OptionsMapperTest.php +++ b/tests/Mapper/ProgrammesDbToDomain/OptionsMapperTest.php @@ -3,7 +3,8 @@ namespace Tests\BBC\ProgrammesPagesService\Mapper\ProgrammesDbToDomain; use BBC\ProgrammesPagesService\Domain\Entity\Options; -use BBC\ProgrammesPagesService\Mapper\ProgrammesDbToDomain\OptionsMapper; +use BBC\ProgrammesPagesService\Domain\ValueObject\ContactDetails; +use BBC\ProgrammesPagesService\Mapper\ProgrammesDbToDomain\MapperFactory; class OptionsMapperTest extends BaseMapperTestCase { @@ -48,11 +49,12 @@ public function testBasicOptionsNoHierarchy() 'livepromo_block' => null, 'prioritytext_block' => null, 'navigation_links' => [], + 'contact_details' => [], ]; $this->assertEquals( new Options($expectedOptions), - $this->getMapper()->getDomainModel($options) + (new MapperFactory())->getOptionsMapper()->getDomainModel($options) ); } @@ -109,16 +111,76 @@ public function testInheritanceOfOptions() 'livepromo_block' => null, 'prioritytext_block' => null, 'navigation_links' => [], + 'contact_details' => [], ]; $this->assertEquals( new Options($expectedOptions), - $this->getMapper()->getDomainModel($childOptions, $parentOptions, $grandparentOptions) + (new MapperFactory())->getOptionsMapper()->getDomainModel($childOptions, $parentOptions, $grandparentOptions) ); } - private function getMapper(): OptionsMapper + public function testCanAddMappedContact() { - return new OptionsMapper($this->getMapperFactory()); + $options = [ + 'language' => 'cy', + 'second_option' => null, + 'contact_details' => [ + [ + 'type' => 'email', + 'value' => 'emailAAAA@myemail.com', + 'freetext' => 'Free text', + ], + [ + 'type' => 'email', + 'value' => 'emailBBBB@myemail.com', + 'freetext' => 'Free text', + ], + ], + ]; + + $expectedOptions = [ + 'projectId' => null, + 'language' => 'cy', + 'branding_id' => 'br-00002', + 'second_option' => null, + 'pulse_survey' => null, + 'brand_2016_layout' => false, + 'brand_2016_layout_use_minimap' => false, + 'show_clip_cards' => true, + 'show_gallery_cards' => true, + 'double_width_first_promo' => false, + 'show_tracklist_inadvance' => false, + 'show_tracklist_timings' => false, + 'show_enhanced_navigation' => false, + 'comments_clips_id' => null, + 'comments_clips_enabled' => false, + 'comments_episodes_id' => null, + 'comments_episodes_enabled' => false, + 'playlister_popularity_enabled' => false, + 'recipes_enabled' => false, + 'brand_layout' => 'availability', + 'promoted_programmes' => [], + 'live_stream_id' => null, + 'live_stream_heading' => null, + 'pid_override_url' => null, + 'pid_override_code' => null, + 'ivote_block' => null, + 'comingsoon_block' => null, + 'comingsoon_textonly' => null, + 'bbc_site' => null, + 'livepromo_block' => null, + 'prioritytext_block' => null, + 'navigation_links' => [], + 'contact_details' => [ + new ContactDetails('email', 'emailAAAA@myemail.com', 'Free text'), + new ContactDetails('email', 'emailBBBB@myemail.com', 'Free text'), + ], + ]; + + $this->assertEquals( + new Options($expectedOptions), + (new MapperFactory())->getOptionsMapper()->getDomainModel($options) + ); } }