Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inmanturbo committed Feb 7, 2024
1 parent 2723c89 commit daaf728
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 11 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ coverage
docs
phpunit.xml
phpstan.neon
testbench.yaml
vendor
node_modules
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@

[![Latest Version on Packagist](https://img.shields.io/packagist/v/inmanturbo/turbohx.svg?style=flat-square)](https://packagist.org/packages/inmanturbo/turbohx)[![Total Downloads](https://img.shields.io/packagist/dt/inmanturbo/turbohx.svg?style=flat-square)](https://packagist.org/packages/inmanturbo/turbohx)

Allows you to send `HTTP` `POST`, `PATCH`,`PUT` and `DELETE` requests to your [laravel/folio](https://github.com/laravel/folio) routes.

## Installation

First install [laravel/folio](https://laravel.com/docs/10.x/folio#installation)

```bash
composer require laravel folio
```

```bash
composer php artisan folio:install
```

Then install this package

```bash
composer require inmanturbo/turbohx
```

That's it!
That's it! Now you can send all types of `HTMX` and `ajax` requests to your [laravel/folio](https://github.com/laravel/folio) routes and handle them directly with your blade files.

## Usage

Expand All @@ -19,7 +33,7 @@ Also allows for a wilcard file to work in place of an `index.blade.php` file as
> [!CAUTION]
> This allows all `HTTP` methods on all folio routes!
Currently just replaces `Route::get` with `Route::any`. May be improved in the future to allow more granular control, such as a function similiar to the `name()` function available in folio. `PR`'s welcome!
`Folio` uses the default `Rout::fallback` method which only supports get requests. This just replaces it with `Route::any` method. May be improved in the future to allow more granular control, such as a function similiar to the `name()` function available in folio. `PR`'s welcome!

## Testing

Expand Down
15 changes: 12 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,24 @@
},
"autoload-dev": {
"psr-4": {
"Inmanturbo\\TurboHX\\Tests\\": "tests"
"Inmanturbo\\TurboHX\\Tests\\": "tests",
"Workbench\\App\\": "workbench/app"
}
},
"scripts": {
"post-autoload-dump": "@php ./vendor/bin/testbench package:discover --ansi",
"post-autoload-dump": "@composer run prepare",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"analyse": "vendor/bin/phpstan analyse",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"format": "vendor/bin/pint"
"format": "vendor/bin/pint",
"build": [
"@composer run prepare"
],
"start": [
"@composer run prepare",
"@php ./vendor/bin/testbench serve"
]
},
"config": {
"sort-packages": true,
Expand Down
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@ parameters:
message: "#^Call to an undefined method Pest\\\\PendingCalls\\\\TestCall\\:\\:expect\\(\\)\\.$#"
count: 1
path: tests/ArchTest.php

-
message: "#^Undefined variable\\: \\$this$#"
count: 5
path: tests/MethodTest.php

-
message: "#^Undefined variable\\: \\$this$#"
count: 6
path: tests/WildcardIndexTest.php
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@
<directory suffix=".php">./src</directory>
</include>
</source>
<php>
<env name="APP_KEY" value="lp0azzXLPJl7BJZkm2agvfXojVsPOWMe"/>
</php>
</phpunit>
5 changes: 5 additions & 0 deletions src/TurboHXServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Inmanturbo\TurboHX;

use Illuminate\Support\Facades\Route;
use Laravel\Folio\FolioManager;
use Laravel\Folio\Router as FolioRouter;

Expand All @@ -11,6 +12,10 @@ public function register()
{
$this->app->singleton(FolioManager::class, TurboHX::class);
$this->app->bind(FolioRouter::class, Router::class);

Route::get('/turbo', function () {
return 'Hello World';
});
}

public function boot()
Expand Down
2 changes: 2 additions & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
providers:
- Workbench\App\Providers\FolioServiceProvider
53 changes: 53 additions & 0 deletions tests/MethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

use Laravel\Folio\Folio;

it('can get', function () {
Folio::route(__DIR__.'/resources/views/pages');

$response = $this->get('/methods');

$response->assertStatus(200);

$response->assertSee('get');
});

it('can post', function () {
Folio::route(__DIR__.'/resources/views/pages');

$response = $this->post('/methods');

$response->assertStatus(200);

$response->assertSee('post');
});

it('can put', function () {
Folio::route(__DIR__.'/resources/views/pages');

$response = $this->put('/methods');

$response->assertStatus(200);

$response->assertSee('put');
});

it('can patch', function () {
Folio::route(__DIR__.'/resources/views/pages');

$response = $this->patch('/methods');

$response->assertStatus(200);

$response->assertSee('patch');
});

it('can delete', function () {
Folio::route(__DIR__.'/resources/views/pages');

$response = $this->delete('/methods');

$response->assertStatus(200);

$response->assertSee('delete');
});
11 changes: 6 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@

namespace Inmanturbo\TurboHX\Tests;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\View;
use Inmanturbo\TurboHX\TurboHXServiceProvider;
use Laravel\Folio\FolioServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
{
protected function setUp(): void
{
$this->afterApplicationCreated(fn () => View::addLocation(__DIR__.'/Feature/resources/views'));

parent::setUp();
}

protected function getPackageProviders($app)
{
return [
FolioServiceProvider::class,
TurboHXServiceProvider::class,
];
}

public function getEnvironmentSetUp($app)
{
//
}
}
36 changes: 36 additions & 0 deletions tests/WildcardIndexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Laravel\Folio\Folio;

test('there will be none', function () {
$this->withoutExceptionHandling();
Folio::route(__DIR__.'/resources/views/pages');

$response = $this->get('/wildcards');

$response->assertStatus(200);

$response->assertSee('0');
});

test('there will be one', function () {
$this->withoutExceptionHandling();
Folio::route(__DIR__.'/resources/views/pages');

$response = $this->get('/wildcards/1');

$response->assertStatus(200);

$response->assertSee('1');
});

test('there will be two', function () {
$this->withoutExceptionHandling();
Folio::route(__DIR__.'/resources/views/pages');

$response = $this->get('/wildcards/1/2');

$response->assertStatus(200);

$response->assertSee('2');
});
23 changes: 23 additions & 0 deletions tests/resources/views/pages/methods/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

@switch(request()->method())
@case('GET')
<h1>get</h1>
@break
@case('POST')
<h1>post</h1>
@break
@case('PUT')
<h1>put</h1>
@break
@case('PATCH')
<h1>patch</h1>
@break
@case('DELETE')
<h1>delete</h1>
@break
@default
@php
throw new Exception('This should never happen');
@endphp
@endswitch

2 changes: 2 additions & 0 deletions tests/resources/views/pages/wildcards/[...ids].blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

{{ count($ids) }}
35 changes: 35 additions & 0 deletions workbench/app/Providers/FolioServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Workbench\App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Laravel\Folio\Folio;

class FolioServiceProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
//
}

/**
* Bootstrap services.
*/
public function boot(): void
{
Route::view('/', 'welcome');

View::addLocation(__DIR__.'/../../../tests/resources/views');

Folio::path(__DIR__.'/../../../tests/resources/views/pages')->middleware([
'*' => [
//
],
]);
}
}

0 comments on commit daaf728

Please sign in to comment.