-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52744b3
commit a1b97c1
Showing
5 changed files
with
253 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |