Skip to content

Commit

Permalink
Introduced ModalVisit class
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Nov 3, 2024
1 parent 52744b3 commit a1b97c1
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 25 deletions.
13 changes: 0 additions & 13 deletions demo-app/tests/Unit/ModalConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public function it_can_create_a_new_instance()
'type' => null,
'modal' => null,
'slideover' => null,
'navigate' => null,
'closeButton' => null,
'closeExplicitly' => null,
'maxWidth' => null,
Expand Down Expand Up @@ -54,16 +53,6 @@ public function it_can_set_the_type_to_slideover()
$this->assertTrue($config->toArray()['slideover']);
}

#[Test]
public function it_can_configure_the_navigation_setting()
{
$config = ModalConfig::new()->navigate();
$this->assertTrue($config->toArray()['navigate']);

$config = ModalConfig::new()->navigate(false);
$this->assertFalse($config->toArray()['navigate']);
}

#[Test]
public function it_can_configure_the_close_button()
{
Expand Down Expand Up @@ -164,7 +153,6 @@ public function it_can_chain_multiple_configurations()
{
$config = ModalConfig::new()
->modal()
->navigate()
->closeButton()
->maxWidth('2xl')
->paddingClasses('p-4')
Expand All @@ -175,7 +163,6 @@ public function it_can_chain_multiple_configurations()
'type' => ModalType::Modal->value,
'modal' => true,
'slideover' => false,
'navigate' => true,
'closeButton' => true,
'closeExplicitly' => null,
'maxWidth' => '2xl',
Expand Down
138 changes: 138 additions & 0 deletions demo-app/tests/Unit/ModalVisitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

declare(strict_types=1);

namespace Tests\Unit;

use InertiaUI\Modal\ModalConfig;
use InertiaUI\Modal\ModalVisit;
use InertiaUI\Modal\QueryStringArrayFormat;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

class ModalVisitTest extends TestCase
{
#[Test]
public function it_can_create_a_new_instance()
{
$visit = ModalVisit::new();

$this->assertInstanceOf(ModalVisit::class, $visit);
$this->assertEquals([
'method' => null,
'navigate' => null,
'data' => null,
'headers' => null,
'config' => null,
'queryStringArrayFormat' => null,
], $visit->toArray());
}

#[Test]
#[DataProvider('httpMethodsProvider')]
public function it_can_set_the_http_method(string $method)
{
$visit = ModalVisit::new()->method($method);

$this->assertEquals($method, $visit->toArray()['method']);
}

public static function httpMethodsProvider(): array
{
return [
'GET method' => ['GET'],
'POST method' => ['POST'],
'PUT method' => ['PUT'],
'PATCH method' => ['PATCH'],
'DELETE method' => ['DELETE'],
];
}

#[Test]
public function it_can_configure_navigation()
{
$visit = ModalVisit::new()->navigate();
$this->assertTrue($visit->toArray()['navigate']);

$visit = ModalVisit::new()->navigate(false);
$this->assertFalse($visit->toArray()['navigate']);
}

#[Test]
public function it_can_set_the_data()
{
$data = ['key' => 'value'];
$visit = ModalVisit::new()->data($data);

$this->assertEquals($data, $visit->toArray()['data']);
}

#[Test]
public function it_sets_the_data_to_null_when_empty()
{
$visit = ModalVisit::new()->data([]);

$this->assertNull($visit->toArray()['data']);
}

#[Test]
public function it_can_set_headers()
{
$headers = ['X-Custom' => 'value'];
$visit = ModalVisit::new()->headers($headers);

$this->assertEquals($headers, $visit->toArray()['headers']);
}

#[Test]
public function it_sets_the_headers_to_null_when_empty()
{
$visit = ModalVisit::new()->headers([]);

$this->assertNull($visit->toArray()['headers']);
}

#[Test]
public function it_can_set_a_modal_config()
{
$config = ModalConfig::new()->modal()->maxWidth('2xl');
$visit = ModalVisit::new()->config($config);

$this->assertEquals($config->toArray(), $visit->toArray()['config']);
}

#[Test]
public function it_can_set_a_query_string_array_format()
{
$format = QueryStringArrayFormat::Brackets;
$visit = ModalVisit::new()->queryStringArrayFormat($format);

$this->assertEquals($format->value, $visit->toArray()['queryStringArrayFormat']);
}

#[Test]
public function it_can_chain_multiple_configurations()
{
$config = ModalConfig::new()->modal();
$data = ['key' => 'value'];
$headers = ['X-Custom' => 'header'];

$visit = ModalVisit::new()
->method('POST')
->navigate()
->data($data)
->headers($headers)
->config($config)
->queryStringArrayFormat(QueryStringArrayFormat::Brackets);

$this->assertEquals([
'method' => 'POST',
'navigate' => true,
'data' => $data,
'headers' => $headers,
'config' => $config->toArray(),
'queryStringArrayFormat' => QueryStringArrayFormat::Brackets->value,
], $visit->toArray());
}
}
12 changes: 0 additions & 12 deletions src/ModalConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class ModalConfig implements Arrayable
{
public function __construct(
protected ?ModalType $type = null,
protected ?bool $navigate = null,
protected ?bool $closeButton = null,
protected ?bool $closeExplicitly = null,
protected ?string $maxWidth = null,
Expand Down Expand Up @@ -50,16 +49,6 @@ public function slideover(): self
return $this;
}

/**
* Configures whether the Base Route / URL feature should be used.
*/
public function navigate(?bool $navigate = true): self
{
$this->navigate = $navigate;

return $this;
}

/**
* Controls the visibility of the close button in the modal.
*/
Expand Down Expand Up @@ -174,7 +163,6 @@ public function toArray(): array
'type' => $this->type?->value,
'modal' => $this->type instanceof ModalType && $this->type === ModalType::Modal,
'slideover' => $this->type instanceof ModalType && $this->type === ModalType::Slideover,
'navigate' => $this->navigate,
'closeButton' => $this->closeButton,
'closeExplicitly' => $this->closeExplicitly,
'maxWidth' => $this->maxWidth,
Expand Down
104 changes: 104 additions & 0 deletions src/ModalVisit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace InertiaUI\Modal;

use Illuminate\Contracts\Support\Arrayable;

class ModalVisit implements Arrayable
{
public function __construct(
protected ?string $method = null,
protected ?bool $navigate = null,
protected ?array $data = null,
protected ?array $headers = null,
protected ?ModalConfig $config = null,
protected ?QueryStringArrayFormat $queryStringArrayFormat = null,
) {
//
}

/**
* Creates a new instance of the modal configuration.
*/
public static function new(): self
{
return new self;
}

/**
* Sets the HTTP method for the modal visit.
*/
public function method(?string $method): self
{
$this->method = $method;

return $this;
}

/**
* Configures whether the Base Route / URL feature should be used.
*/
public function navigate(?bool $navigate = true): self
{
$this->navigate = $navigate;

return $this;
}

/**
* Sets the data to be sent with the modal visit.
*/
public function data(?array $data): self
{
$this->data = blank($data) ? null : $data;

return $this;
}

/**
* Sets the headers to be sent with the modal visit.
*/
public function headers(?array $headers): self
{
$this->headers = blank($headers) ? null : $headers;

return $this;
}

/**
* Sets the configuration for the modal visit.
*/
public function config(?ModalConfig $config): self
{
$this->config = $config;

return $this;
}

/**
* Sets the query string array format for the modal visit.
*/
public function queryStringArrayFormat(?QueryStringArrayFormat $queryStringArrayFormat): self
{
$this->queryStringArrayFormat = $queryStringArrayFormat;

return $this;
}

/**
* Converts the modal visit to an array.
*/
public function toArray(): array
{
return [
'method' => $this->method,
'navigate' => $this->navigate,
'data' => $this->data,
'headers' => $this->headers,
'config' => $this->config?->toArray(),
'queryStringArrayFormat' => $this->queryStringArrayFormat?->value,
];
}
}
11 changes: 11 additions & 0 deletions src/QueryStringArrayFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace InertiaUI\Modal;

enum QueryStringArrayFormat: string
{
case Brackets = 'brackets';
case Indices = 'indices';
}

0 comments on commit a1b97c1

Please sign in to comment.