-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
12 changed files
with
284 additions
and
14 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,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/|version|/phpunit.xsd" | ||
bootstrap="tests/bootstrap.php" | ||
colors="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory suffix="Test.php">tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
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
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 @@ | ||
/test-results.xml |
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,5 @@ | ||
<?php | ||
|
||
arch() | ||
->expect('Putyourlightson\Datastar') | ||
->not->toUse(['die', 'dd', 'dump', 'var_dump']); |
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,103 @@ | ||
<?php | ||
|
||
use Putyourlightson\Datastar\Models\Signals; | ||
use Putyourlightson\Datastar\Services\Sse; | ||
|
||
beforeEach(function() { | ||
$sse = Mockery::mock(Sse::class); | ||
app()->instance(Sse::class, $sse); | ||
}); | ||
|
||
test('Test getting a signal value', function() { | ||
$signals = new Signals(['a' => 1]); | ||
expect($signals->get('a')) | ||
->toBe(1); | ||
}); | ||
|
||
test('Test getting a signal value using a magic call', function() { | ||
$signals = new Signals(['a' => 1]); | ||
expect($signals->a) | ||
->toBe(1); | ||
}); | ||
|
||
test('Test getting a nested signal value', function() { | ||
$signals = new Signals(['a' => ['b' => ['c' => 1]]]); | ||
expect($signals->get('a.b.c')) | ||
->toBe(1); | ||
}); | ||
|
||
test('Test getting a missing signal value', function() { | ||
$signals = new Signals(['a' => 1]); | ||
expect($signals->get('x')) | ||
->toBeNull(); | ||
}); | ||
|
||
test('Test getting a missing signal value using a magic call', function() { | ||
$signals = new Signals(['a' => 1]); | ||
expect($signals->x) | ||
->toBeNull(); | ||
}); | ||
|
||
test('Test adding a signal', function() { | ||
app(Sse::class)->shouldReceive('mergeSignals'); | ||
$signals = new Signals([]); | ||
$signals->set('a', 1); | ||
expect($signals->get('a')) | ||
->toBe(1); | ||
}); | ||
|
||
test('Test adding a signal using a magic call', function() { | ||
app(Sse::class)->shouldReceive('mergeSignals'); | ||
$signals = new Signals([]); | ||
$signals->a(1); | ||
expect($signals->get('a')) | ||
->toBe(1); | ||
}); | ||
|
||
test('Test modifying an existing signal', function() { | ||
app(Sse::class)->shouldReceive('mergeSignals'); | ||
$signals = new Signals(['a' => 1]); | ||
$signals->set('a', 2); | ||
expect($signals->get('a')) | ||
->toBe(2); | ||
}); | ||
|
||
test('Test modifying an existing signal using a magic call', function() { | ||
app(Sse::class)->shouldReceive('mergeSignals'); | ||
$signals = new Signals(['a' => 1]); | ||
$signals->a(2); | ||
expect($signals->get('a')) | ||
->toBe(2); | ||
}); | ||
|
||
test('Test adding a nested signal', function() { | ||
app(Sse::class)->shouldReceive('mergeSignals'); | ||
$signals = new Signals([]); | ||
$signals->set('a.b.c', 1); | ||
expect($signals->get('a.b.c')) | ||
->toBe(1); | ||
}); | ||
|
||
test('Test modifying an existing nested signal', function() { | ||
app(Sse::class)->shouldReceive('mergeSignals'); | ||
$signals = new Signals(['a' => ['b' => ['c' => 1]]]); | ||
$signals->set('a.b.c', 2); | ||
expect($signals->get('a.b.c')) | ||
->toBe(2); | ||
}); | ||
|
||
test('Test removing a signal value', function() { | ||
app(Sse::class)->shouldReceive('removeSignals'); | ||
$signals = new Signals(['a' => 1]); | ||
$signals->remove('a'); | ||
expect($signals->getValues()) | ||
->toBe([]); | ||
}); | ||
|
||
test('Test removing a nested signal value', function() { | ||
app(Sse::class)->shouldReceive('removeSignals'); | ||
$signals = new Signals(['a' => ['b' => ['c' => 1]]]); | ||
$signals->remove('a.b.c'); | ||
expect($signals->getValues()) | ||
->toBe(['a' => ['b' => []]]); | ||
}); |
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,13 @@ | ||
<?php | ||
|
||
/** | ||
* Tests the SSE service. | ||
*/ | ||
|
||
use Putyourlightson\Datastar\Services\Sse; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
|
||
test('Test that calling an SSE method when another one is in process throws an exception', function() { | ||
app(Sse::class)->setSseInProcess('mergeFragments'); | ||
app(Sse::class)->mergeSignals([]); | ||
})->throws(BadRequestHttpException::class); |
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,44 @@ | ||
<?php | ||
|
||
use Putyourlightson\Datastar\Helpers\Datastar; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
|
||
test('Test creating an action', function(string $method) { | ||
$helper = new Datastar(); | ||
$value = $helper->$method('view'); | ||
expect($value) | ||
->toStartWith("@$method(") | ||
->toContain('view'); | ||
|
||
if ($method === 'get') { | ||
expect($value) | ||
->not->toContain('X-CSRF-TOKEN'); | ||
} else { | ||
expect($value) | ||
->toContain('X-CSRF-TOKEN'); | ||
} | ||
})->with([ | ||
'get', | ||
'post', | ||
'put', | ||
'patch', | ||
'delete', | ||
]); | ||
|
||
test('Test creating an action containing an array of primitive variables', function() { | ||
$helper = new Datastar(); | ||
$value = $helper->get('template', ['x' => 1, 'y' => 'string', 'z' => true]); | ||
expect($value) | ||
->toContain('1', 'string', 'true'); | ||
}); | ||
|
||
test('Test that creating an action containing a reserved variable name throws an exception', function() { | ||
$helper = new Datastar(); | ||
$signalsVariableName = config('datastar.signalsVariableName'); | ||
$helper->get('template', [$signalsVariableName => 1]); | ||
})->throws(BadRequestHttpException::class); | ||
|
||
test('Test that creating an action containing an object variable throws an exception', function() { | ||
$helper = new Datastar(); | ||
$helper->get('template', ['object' => new stdClass()]); | ||
})->throws(BadRequestHttpException::class); |
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,39 @@ | ||
<?php | ||
|
||
use Illuminate\Foundation\Testing\TestCase; | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Test Case | ||
|-------------------------------------------------------------------------- | ||
| | ||
| The closure you provide to your test functions is always bound to a specific PHPUnit test | ||
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may | ||
| need to change it using the "pest()" function to bind a different classes or traits. | ||
| | ||
*/ | ||
|
||
uses(TestCase::class) | ||
->in('./'); | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Expectations | ||
|-------------------------------------------------------------------------- | ||
| | ||
| When you're writing tests, you often need to check that values meet certain conditions. The | ||
| "expect()" function gives you access to a set of "expectations" methods that you can use | ||
| to assert different things. Of course, you may extend the Expectation API at any time. | ||
| | ||
*/ | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Functions | ||
|-------------------------------------------------------------------------- | ||
| | ||
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your | ||
| project that you don't want to repeat in every file. Here you can also expose helpers as | ||
| global functions to help you to reduce the number of lines of code in your test files. | ||
| | ||
*/ |
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,37 @@ | ||
# Testing | ||
|
||
## Static Analysis | ||
|
||
To run static analysis on the plugin, install [PHPStan for Craft CMS](https://github.com/craftcms/phpstan) and run the following command from the root of your project. | ||
|
||
```shell | ||
./vendor/bin/phpstan analyse -c vendor/putyourlightson/craft-datastar-module/phpstan.neon --memory-limit 1G | ||
``` | ||
|
||
## Easy Coding Standard | ||
|
||
To run the Easy Coding Standard on the plugin, install [ECS for Craft CMS](https://github.com/craftcms/ecs) and run the following command from the root of your project. | ||
|
||
```shell | ||
./vendor/bin/ecs check -c vendor/putyourlightson/craft-datastar-module/ecs.php | ||
``` | ||
|
||
## Pest Tests | ||
|
||
To run Pest tests, first install [Craft Pest](https://craft-pest.com/) core as a dev dependency. | ||
|
||
```shell | ||
composer require markhuot/craft-pest-core:^2.0.0 --dev | ||
``` | ||
|
||
Then run the following command from the root of your project. | ||
|
||
```shell | ||
php php vendor/bin/pest --configuration=vendor/putyourlightson/craft-datastar-module/phpunit.xml --test-directory=vendor/putyourlightson/craft-datastar-module/tests | ||
``` | ||
|
||
Or to run a specific test. | ||
|
||
```shell | ||
php php vendor/bin/pest --configuration=vendor/putyourlightson/craft-datastar-module/phpunit.xml --test-directory=vendor/putyourlightson/craft-datastar-module/tests --filter=StoreTest | ||
``` |
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,5 @@ | ||
<?php | ||
|
||
define('PROJECT_VENDOR_DIR', getenv('PROJECT_VENDOR_DIR') ?: dirname(__DIR__) . '/vendor'); | ||
|
||
require realpath(PROJECT_VENDOR_DIR . '/autoload.php'); |