From e7bf35e29cd829376d664a0a42cbb79246b45b5d Mon Sep 17 00:00:00 2001 From: Ambyr Date: Thu, 2 May 2024 12:30:11 -0400 Subject: [PATCH 1/7] feat: add catalog component to homepage styleguide --- app/Http/Controllers/HomepageController.php | 53 ++++++++++++++++++- resources/views/components/footer.blade.php | 0 resources/views/homepage.blade.php | 5 ++ .../Repositories/HomepageRepository.php | 16 ++++-- 4 files changed, 70 insertions(+), 4 deletions(-) mode change 100644 => 100755 resources/views/components/footer.blade.php diff --git a/app/Http/Controllers/HomepageController.php b/app/Http/Controllers/HomepageController.php index b0dadbe33..b26212e27 100644 --- a/app/Http/Controllers/HomepageController.php +++ b/app/Http/Controllers/HomepageController.php @@ -32,11 +32,62 @@ public function index(Request $request): View { // $request->data['base']['show_site_menu'] = false; // $promos = $this->promo->getHomepagePromos(); + + $components = + +promo = $promo; + $this->modularComponent = $modularComponent; + $this->article = $article; + $this->event = $event; + } + + /** + * Display the homepage view. + */ + public function index(Request $request): View + { + // $request->data['base']['show_site_menu'] = false; + + $promos = $this->promo->getHomepagePromos($request->data); + + $modularComponents['modularComponents'] = []; + + if(!empty($request->data['base']['data'])) { + $modularComponents['modularComponents'] = $this->modularComponent->getModularComponents($request->data['base']); + $promos['components'] = $modularComponents['modularComponents']; + } $articles = $this->article->listing($request->data['base']['site']['news']['application_id']); $events = $this->event->getEvents($request->data['base']['site']['id']); - return view('homepage', merge($request->data, $articles, $events)); + return view('homepage', merge($request->data, $promos, $articles, $events)); } } diff --git a/resources/views/components/footer.blade.php b/resources/views/components/footer.blade.php old mode 100644 new mode 100755 diff --git a/resources/views/homepage.blade.php b/resources/views/homepage.blade.php index 258f4b961..6d92d0f5b 100644 --- a/resources/views/homepage.blade.php +++ b/resources/views/homepage.blade.php @@ -7,6 +7,11 @@ {!! $base['page']['content']['main'] !!} + @if(!empty($components['catalog-1']['data'])) +
+ @include('components.catalog', ['data' => $components['catalog-1']['data'], 'component' => $components['catalog-1']['component'], 'class' => 'my-class']) +
+ @endif @if(!empty($articles['data']) || !empty($events))
@if(!empty($articles['data'])) diff --git a/styleguide/Repositories/HomepageRepository.php b/styleguide/Repositories/HomepageRepository.php index ff7c9b1b2..34f5301bc 100644 --- a/styleguide/Repositories/HomepageRepository.php +++ b/styleguide/Repositories/HomepageRepository.php @@ -20,9 +20,19 @@ public function __construct(Factory $faker) */ public function getHomepagePromos(int $page_id = null) { - return [ - //'key' => app(\Factories\YourFactory::class)->create(5), - 'promos' => app(\Factories\GenericPromo::class)->create(5), + $promos => [ + 'homepageItems' => app(\Factories\GenericPromo::class)->create(5, false), + 'components' => [ + 'catalog-1'=> [ + 'data' => app(\Factories\CatalogPromo::class)->create(4, false), + 'component' => [ + 'filename' => 'catalog-1', + 'gradientOverlay' => true, + ], + ], + ], ]; + + return $promos; } } From 8a7e28b1ea76e557ccd33274959b5f57bebbd26a Mon Sep 17 00:00:00 2001 From: Ambyr Date: Thu, 2 May 2024 12:30:28 -0400 Subject: [PATCH 2/7] chore: composer update --- composer.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.lock b/composer.lock index 71ea8a410..90f842e2a 100644 --- a/composer.lock +++ b/composer.lock @@ -9129,5 +9129,5 @@ "php": "^8.1" }, "platform-dev": [], - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.6.0" } From 924de479002616562902474da7fa8b3b7e5db152 Mon Sep 17 00:00:00 2001 From: Ambyr Date: Thu, 2 May 2024 14:28:49 -0400 Subject: [PATCH 3/7] fix: remove unnecessary code --- app/Http/Controllers/HomepageController.php | 45 ++------------------- 1 file changed, 4 insertions(+), 41 deletions(-) diff --git a/app/Http/Controllers/HomepageController.php b/app/Http/Controllers/HomepageController.php index b26212e27..153f26947 100644 --- a/app/Http/Controllers/HomepageController.php +++ b/app/Http/Controllers/HomepageController.php @@ -7,43 +7,6 @@ namespace App\Http\Controllers; -use Illuminate\View\View; -use Illuminate\Http\Request; -use Contracts\Repositories\HomepageRepositoryContract; -use Contracts\Repositories\EventRepositoryContract; -use Contracts\Repositories\ArticleRepositoryContract; - -class HomepageController extends Controller -{ - /** - * Construct the controller. - */ - public function __construct(HomepageRepositoryContract $promo, ArticleRepositoryContract $article, EventRepositoryContract $event) - { - $this->promo = $promo; - $this->article = $article; - $this->event = $event; - } - - /** - * Display the homepage view. - */ - public function index(Request $request): View - { - // $request->data['base']['show_site_menu'] = false; - // $promos = $this->promo->getHomepagePromos(); - - $components = - -promo = $promo; @@ -78,10 +41,10 @@ public function index(Request $request): View $promos = $this->promo->getHomepagePromos($request->data); $modularComponents['modularComponents'] = []; - + if(!empty($request->data['base']['data'])) { $modularComponents['modularComponents'] = $this->modularComponent->getModularComponents($request->data['base']); - $promos['components'] = $modularComponents['modularComponents']; + $promos['components'] = $modularComponents['modularComponents']; } $articles = $this->article->listing($request->data['base']['site']['news']['application_id']); From 529d961a066b7606730cc5d69028dcddd4364fab Mon Sep 17 00:00:00 2001 From: Ambyr Date: Thu, 2 May 2024 14:30:16 -0400 Subject: [PATCH 4/7] feat: update homepage to accept array & update test case --- app/Repositories/HomepageRepository.php | 4 ++-- contracts/Repositories/HomepageRepositoryContract.php | 2 +- styleguide/Repositories/HomepageRepository.php | 8 ++++---- tests/Unit/Repositories/HomepageRepositoryTest.php | 7 +++++-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/Repositories/HomepageRepository.php b/app/Repositories/HomepageRepository.php index 8d0010d7c..aae70df4e 100644 --- a/app/Repositories/HomepageRepository.php +++ b/app/Repositories/HomepageRepository.php @@ -31,14 +31,14 @@ public function __construct(Connector $wsuApi, ParsePromos $parsePromos, Reposit /** * {@inheritdoc} */ - public function getHomepagePromos(int $page_id = 0) + public function getHomepagePromos(array $data): array { $group_reference = [ 123 => 'example', ]; $group_config = [ - 'example' => 'page_id:'.$page_id.'|randomize|first', + 'example' => 'randomize|first', ]; $params = [ diff --git a/contracts/Repositories/HomepageRepositoryContract.php b/contracts/Repositories/HomepageRepositoryContract.php index e6072547c..949f013cc 100644 --- a/contracts/Repositories/HomepageRepositoryContract.php +++ b/contracts/Repositories/HomepageRepositoryContract.php @@ -7,5 +7,5 @@ interface HomepageRepositoryContract /** * {@inheritdoc} */ - public function getHomepagePromos(int $page_id = 0); + public function getHomepagePromos(array $data): array; } diff --git a/styleguide/Repositories/HomepageRepository.php b/styleguide/Repositories/HomepageRepository.php index 34f5301bc..13b892459 100644 --- a/styleguide/Repositories/HomepageRepository.php +++ b/styleguide/Repositories/HomepageRepository.php @@ -18,13 +18,13 @@ public function __construct(Factory $faker) /** * {@inheritdoc} */ - public function getHomepagePromos(int $page_id = null) + public function getHomepagePromos(array $data): array { - $promos => [ + $promos = [ 'homepageItems' => app(\Factories\GenericPromo::class)->create(5, false), 'components' => [ - 'catalog-1'=> [ - 'data' => app(\Factories\CatalogPromo::class)->create(4, false), + 'catalog-1' => [ + 'data' => app(\Factories\GenericPromo::class)->create(4, false), 'component' => [ 'filename' => 'catalog-1', 'gradientOverlay' => true, diff --git a/tests/Unit/Repositories/HomepageRepositoryTest.php b/tests/Unit/Repositories/HomepageRepositoryTest.php index de079fd2e..ff06a0a28 100644 --- a/tests/Unit/Repositories/HomepageRepositoryTest.php +++ b/tests/Unit/Repositories/HomepageRepositoryTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\Attributes\Test; use App\Repositories\HomepageRepository; +use Factories\GenericPromo; use Tests\TestCase; use Mockery as Mockery; use Waynestate\Api\Connector; @@ -13,9 +14,11 @@ final class HomepageRepositoryTest extends TestCase #[Test] public function getting_homepage_promos_should_return_array(): void { + $promo_return = app(GenericPromo::class)->create(4, false); + // Fake return $return = [ - 'promotions' => [], + 'promotion' => $promo_return, ]; // Mock the connector and set the return @@ -23,7 +26,7 @@ public function getting_homepage_promos_should_return_array(): void $wsuApi->shouldReceive('sendRequest')->with('cms.promotions.listing', Mockery::type('array'))->once()->andReturn($return); // Get the promos - $promos = app(HomepageRepository::class, ['wsuApi' => $wsuApi])->getHomepagePromos($this->faker->randomDigit()); + $promos = app(HomepageRepository::class, ['wsuApi' => $wsuApi])->getHomepagePromos($promo_return); $this->assertTrue(is_array($promos)); } From 4ed19a7add9d8323bae474f3ce004e94c3fc6b63 Mon Sep 17 00:00:00 2001 From: Ambyr Date: Fri, 17 May 2024 10:55:09 -0400 Subject: [PATCH 5/7] refactor: update comment to say profile --- tests/Unit/Http/Controllers/ProfileControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Http/Controllers/ProfileControllerTest.php b/tests/Unit/Http/Controllers/ProfileControllerTest.php index 873f09b3e..3ac22beb0 100644 --- a/tests/Unit/Http/Controllers/ProfileControllerTest.php +++ b/tests/Unit/Http/Controllers/ProfileControllerTest.php @@ -55,7 +55,7 @@ public function invalid_profile_should_404_using_profile_repository(): void // Construct the profile repository $profileRepository = app(ProfileRepository::class, ['wsuApi' => $wsuApi]); - // Construct the news controller + // Construct the profile controller $this->profileController = app(ProfileController::class, ['profile' => $profileRepository]); // Call the profile listing From 12b0afa18dd237b0639ce3450795274dec0b4b4a Mon Sep 17 00:00:00 2001 From: Ambyr Date: Fri, 17 May 2024 10:55:41 -0400 Subject: [PATCH 6/7] fix: test coverage for homepage controller --- factories/Catalog.php | 53 ++++++++++++++ .../Controllers/HomepageControllerTest.php | 73 +++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 factories/Catalog.php create mode 100644 tests/Unit/Http/Controllers/HomepageControllerTest.php diff --git a/factories/Catalog.php b/factories/Catalog.php new file mode 100644 index 000000000..1260ca0d9 --- /dev/null +++ b/factories/Catalog.php @@ -0,0 +1,53 @@ +faker = $faker->create(); + } + + /** + * {@inheritdoc} + */ + public function create($limit = 1, $flatten = false, $options = []) + { + // Doc https://github.com/fzaninotto/Faker + + $promo_group_id = $this->faker->randomNumber(5); + + $relative_url = '/styleguide/image/600x600?text=600x600'; // 1:1 + + + for ($i = 0; $i <= $limit - 1; $i++) { + $data[$i] = [ + 'title' => 'Call to action', + 'link' => '#', + 'promo_item_id' => $i, + 'promo_group_id' => strval($promo_group_id), + 'relative_url' => $relative_url.':'.$i, + 'filename_url' => $relative_url.':'.$i, + 'filename_alt_text' => 'Placeholder image '.$i, + 'group' => [ + 'title' => 'Modular Catalog Promotion Group', + ], + ]; + + $data[$i] = array_replace_recursive($data[$i], $options); + } + + if ($limit === 1 && $flatten === true) { + return current($data); + } + + return $data; + } +} diff --git a/tests/Unit/Http/Controllers/HomepageControllerTest.php b/tests/Unit/Http/Controllers/HomepageControllerTest.php new file mode 100644 index 000000000..b7c464cec --- /dev/null +++ b/tests/Unit/Http/Controllers/HomepageControllerTest.php @@ -0,0 +1,73 @@ +getPageData(); + $request->data = $base; + + $promo_group_id = $this->faker->numberbetween(1, 3); + + // Create a fake data request + $data = app(Page::class)->create(1, true, [ + 'page' => [ + 'controller' => 'HomepageController', + ], + 'data' => [ + 'modular-catalog-1' => json_encode([ + 'id' => $promo_group_id, + 'config' => 'randomize|limit:2', + ]), + ], + ]); + + // Fake return + $return['promotions'] = app(GenericPromo::class)->create(3, false, [ + 'promo_group_id' => $promo_group_id, + 'group' => [ + 'promo_group_id' => $promo_group_id, + ], + ]); + + // Mock the connector and set the return + $wsuApi = Mockery::mock(Connector::class); + $wsuApi->shouldReceive('sendRequest')->with('cms.promotions.listing', Mockery::type('array'))->once()->andReturn($return); + + // Pass in the modular repository component data + // $modularComponents['modularComponents'] = app(ModularPageRepository::class, ['wsuApi' => $wsuApi])->getModularComponents($data); + + // Create a modular catalog component + $request->data['base']['data']['modular-catalog-1'] = json_encode(['id' => $promo_group_id]); + + // Construct the modular repository + $modularRepository = app(ModularPageRepository::class, ['wsuApi' => $wsuApi]); + + // Construct the homepage controller + $this->modularComponent = app(HomepageController::class, ['modularComponent' => $modularRepository]); + + // Create the view + $view = $this->modularComponent->index($request); + // $this->assertTrue(is_array($HomepageController)); + } +} From 9a6f5ca08529f8e0f3683e7d1f562457a42b32b5 Mon Sep 17 00:00:00 2001 From: Ambyr Date: Fri, 17 May 2024 10:57:45 -0400 Subject: [PATCH 7/7] refactor: phplint --- tests/Unit/Http/Controllers/HomepageControllerTest.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/Unit/Http/Controllers/HomepageControllerTest.php b/tests/Unit/Http/Controllers/HomepageControllerTest.php index b7c464cec..04b360c7a 100644 --- a/tests/Unit/Http/Controllers/HomepageControllerTest.php +++ b/tests/Unit/Http/Controllers/HomepageControllerTest.php @@ -7,7 +7,6 @@ use Illuminate\Http\Request; use App\Http\Controllers\HomepageController; use App\Repositories\ModularPageRepository; -use App\Repositories\PromoRepository; use Styleguide\Pages\Homepage; use Factories\Page; use Factories\GenericPromo; @@ -17,8 +16,7 @@ final class HomepageControllerTest extends TestCase { - - #[Test] + #[Test] #[Description('Test if the homepage controller is returning the modular component array data.')] public function homepage_with_modular_components_should_return_to_the_view(): void { @@ -53,9 +51,9 @@ public function homepage_with_modular_components_should_return_to_the_view(): vo // Mock the connector and set the return $wsuApi = Mockery::mock(Connector::class); $wsuApi->shouldReceive('sendRequest')->with('cms.promotions.listing', Mockery::type('array'))->once()->andReturn($return); - + // Pass in the modular repository component data - // $modularComponents['modularComponents'] = app(ModularPageRepository::class, ['wsuApi' => $wsuApi])->getModularComponents($data); + // $modularComponents['modularComponents'] = app(ModularPageRepository::class, ['wsuApi' => $wsuApi])->getModularComponents($data); // Create a modular catalog component $request->data['base']['data']['modular-catalog-1'] = json_encode(['id' => $promo_group_id]);