Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/homepage repository - Add catalog component to homepage #715

Closed
wants to merge 7 commits into from
Closed
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
22 changes: 18 additions & 4 deletions app/Http/Controllers/HomepageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\View\View;
use Illuminate\Http\Request;
use Contracts\Repositories\HomepageRepositoryContract;
use Contracts\Repositories\ModularPageRepositoryContract;
use Contracts\Repositories\EventRepositoryContract;
use Contracts\Repositories\ArticleRepositoryContract;

Expand All @@ -18,9 +19,14 @@ class HomepageController extends Controller
/**
* Construct the controller.
*/
public function __construct(HomepageRepositoryContract $promo, ArticleRepositoryContract $article, EventRepositoryContract $event)
{
public function __construct(
HomepageRepositoryContract $promo,
ModularPageRepositoryContract $modularComponent,
ArticleRepositoryContract $article,
EventRepositoryContract $event
) {
$this->promo = $promo;
$this->modularComponent = $modularComponent;
$this->article = $article;
$this->event = $event;
}
Expand All @@ -31,12 +37,20 @@ public function __construct(HomepageRepositoryContract $promo, ArticleRepository
public function index(Request $request): View
{
// $request->data['base']['show_site_menu'] = false;
// $promos = $this->promo->getHomepagePromos();

$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));
}
}
4 changes: 2 additions & 2 deletions app/Repositories/HomepageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/Repositories/HomepageRepositoryContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ interface HomepageRepositoryContract
/**
* {@inheritdoc}
*/
public function getHomepagePromos(int $page_id = 0);
public function getHomepagePromos(array $data): array;
}
53 changes: 53 additions & 0 deletions factories/Catalog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Factories;

use Contracts\Factories\FactoryContract;
use Faker\Factory;

class Catalog implements FactoryContract
{
/**
* Construct the factory.
*/
public function __construct(Factory $faker)
{
$this->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;
}
}
Empty file modified resources/views/components/footer.blade.php
100644 → 100755
Empty file.
5 changes: 5 additions & 0 deletions resources/views/homepage.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
{!! $base['page']['content']['main'] !!}
</div>

@if(!empty($components['catalog-1']['data']))
<div>
@include('components.catalog', ['data' => $components['catalog-1']['data'], 'component' => $components['catalog-1']['component'], 'class' => 'my-class'])
</div>
@endif
@if(!empty($articles['data']) || !empty($events))
<div class="row md:flex gap-4 xl:gap-8">
@if(!empty($articles['data']))
Expand Down
18 changes: 14 additions & 4 deletions styleguide/Repositories/HomepageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@ public function __construct(Factory $faker)
/**
* {@inheritdoc}
*/
public function getHomepagePromos(int $page_id = null)
public function getHomepagePromos(array $data): array
{
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\GenericPromo::class)->create(4, false),
'component' => [
'filename' => 'catalog-1',
'gradientOverlay' => true,
],
],
],
];

return $promos;
}
}
71 changes: 71 additions & 0 deletions tests/Unit/Http/Controllers/HomepageControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Tests\Unit\Http\Controllers;

use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
use Illuminate\Http\Request;
use App\Http\Controllers\HomepageController;
use App\Repositories\ModularPageRepository;
use Styleguide\Pages\Homepage;
use Factories\Page;
use Factories\GenericPromo;
use Factories\Catalog;
use Mockery as Mockery;
use Waynestate\Api\Connector;

final class HomepageControllerTest extends TestCase
{
#[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
{
// Fake request
$request = new Request();
$base['base'] = app(Homepage::class)->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));
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Http/Controllers/ProfileControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions tests/Unit/Repositories/HomepageRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,17 +14,19 @@ 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
$wsuApi = Mockery::mock(Connector::class);
$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));
}
Expand Down
Loading