Skip to content

Commit

Permalink
Merge pull request #116 from hotwired-laravel/workbench
Browse files Browse the repository at this point in the history
Workbench
  • Loading branch information
tonysm authored Sep 23, 2023
2 parents e12dc68 + 886dea6 commit 2bcbef1
Show file tree
Hide file tree
Showing 128 changed files with 2,233 additions and 1,335 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
/.php_cs.dist export-ignore
/psalm.xml export-ignore
/psalm.xml.dist export-ignore
/workbench export-ignore
/testbench.yaml export-ignore
4 changes: 2 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.1, 8.2]
os: [ubuntu-latest, macos-latest]
php: [8.2]
laravel: [10.*]
stability: [prefer-lowest, prefer-stable]
include:
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed re

### Contributing

We're using [Workbench](https://github.com/orchestral/workbench), so we have a companion Laravel app for local development of the package living in the [workbench/](./workbench/) at the root level.

You may run the Workbench app like this:

```bash
composer serve
```

After that, the app should be available at [http://localhost:8000](http://localhost:8000).

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

### Security Vulnerabilities
Expand Down
33 changes: 27 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,58 @@
}
],
"require": {
"php": "^8.1",
"php": "^8.2",
"illuminate/support": "^10.0"
},
"require-dev": {
"laravel/pint": "^1.10",
"orchestra/testbench": "^8.0",
"orchestra/testbench": "^8.9",
"orchestra/workbench": "^0.2.5",
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
"HotwiredLaravel\\TurboLaravel\\": "src"
},
"files": ["src/helpers.php", "src/globals.php"]
"files": [
"src/helpers.php",
"src/globals.php"
]
},
"autoload-dev": {
"psr-4": {
"HotwiredLaravel\\TurboLaravel\\Tests\\": "tests"
"HotwiredLaravel\\TurboLaravel\\Tests\\": "tests",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
}
},
"scripts": {
"psalm": "vendor/bin/psalm",
"test": "vendor/bin/phpunit --colors=always",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
"post-autoload-dump": [
"@clear",
"@prepare"
],
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": "@php vendor/bin/testbench workbench:build --ansi",
"serve": [
"@build",
"@php vendor/bin/testbench serve"
],
"lint": [
"@php vendor/bin/pint"
]
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"\\HotwiredLaravel\\TurboLaravel\\TurboServiceProvider"
"\\HotwiredLaravel\\TurboLaravel\\TurboServiceProvider"
],
"aliases": {
"Turbo": "\\HotwiredLaravel\\TurboLaravel\\Facades\\Turbo",
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
verbose="true"
>
<testsuites>
<testsuite name="Tonysm Test Suite">
<testsuite name="Turbo Laravel Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Middleware/TurboMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ private function guessFormRedirectUrl($request, string $defaultRedirectUrl = nul

protected function guessRouteName(string $routeName): ?string
{
if (! Str::endsWith($routeName, ['.store', '.update'])) {
if (! Str::endsWith($routeName, ['.store', '.update', '.destroy'])) {
return null;
}

return str_replace(['.store', '.update'], ['.create', '.edit'], $routeName);
return str_replace(['.store', '.update', '.destroy'], ['.create', '.edit', '.delete'], $routeName);
}

protected function inExceptArray(Request $request): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Http/MultiplePendingTurboStreamResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;

class MultiplePendingTurboStreamResponse implements Responsable, Htmlable, Renderable
class MultiplePendingTurboStreamResponse implements Htmlable, Renderable, Responsable
{
/** @var Collection|PendingTurboStreamResponse[] */
private Collection $pendingStreams;
Expand Down
5 changes: 3 additions & 2 deletions src/Http/PendingTurboStreamResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use HotwiredLaravel\TurboLaravel\Broadcasting\PendingBroadcast;
use HotwiredLaravel\TurboLaravel\Broadcasting\Rendering;
use function HotwiredLaravel\TurboLaravel\dom_id;
use HotwiredLaravel\TurboLaravel\Facades\TurboStream;
use HotwiredLaravel\TurboLaravel\Models\Naming\Name;
use Illuminate\Contracts\Support\Htmlable;
Expand All @@ -15,7 +14,9 @@
use Illuminate\Support\HtmlString;
use Illuminate\Support\Traits\Macroable;

class PendingTurboStreamResponse implements Responsable, Htmlable, Renderable
use function HotwiredLaravel\TurboLaravel\dom_id;

class PendingTurboStreamResponse implements Htmlable, Renderable, Responsable
{
use Macroable;

Expand Down
3 changes: 2 additions & 1 deletion src/Models/Broadcasts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use HotwiredLaravel\TurboLaravel\Broadcasting\PendingBroadcast;
use HotwiredLaravel\TurboLaravel\Broadcasting\Rendering;
use function HotwiredLaravel\TurboLaravel\dom_id;
use HotwiredLaravel\TurboLaravel\Facades\TurboStream;
use HotwiredLaravel\TurboLaravel\Models\Naming\Name;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Support\Collection;

use function HotwiredLaravel\TurboLaravel\dom_id;

/**
* @mixin \Illuminate\Database\Eloquent\Model
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Views/Components/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace HotwiredLaravel\TurboLaravel\Views\Components;

use function HotwiredLaravel\TurboLaravel\dom_id;
use Illuminate\Database\Eloquent\Model;
use Illuminate\View\Component;

use function HotwiredLaravel\TurboLaravel\dom_id;

class Frame extends Component
{
/** @var string|Model|array */
Expand Down
3 changes: 2 additions & 1 deletion src/Views/Components/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace HotwiredLaravel\TurboLaravel\Views\Components;

use function HotwiredLaravel\TurboLaravel\dom_id;
use HotwiredLaravel\TurboLaravel\Exceptions\TurboStreamTargetException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\View\Component;

use function HotwiredLaravel\TurboLaravel\dom_id;

class Stream extends Component
{
const DEFAULT_ACTIONS = [
Expand Down
9 changes: 5 additions & 4 deletions src/globals.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php

use function HotwiredLaravel\TurboLaravel\dom_class as base_dom_class;
use function HotwiredLaravel\TurboLaravel\dom_id as base_dom_id;
use HotwiredLaravel\TurboLaravel\Http\MultiplePendingTurboStreamResponse;
use HotwiredLaravel\TurboLaravel\Http\PendingTurboStreamResponse;
use function HotwiredLaravel\TurboLaravel\turbo_stream as base_turbo_stream;
use function HotwiredLaravel\TurboLaravel\turbo_stream_view as base_turbo_stream_view;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Response;
use Illuminate\Support\Collection;

use function HotwiredLaravel\TurboLaravel\dom_class as base_dom_class;
use function HotwiredLaravel\TurboLaravel\dom_id as base_dom_id;
use function HotwiredLaravel\TurboLaravel\turbo_stream as base_turbo_stream;
use function HotwiredLaravel\TurboLaravel\turbo_stream_view as base_turbo_stream_view;

if (! function_exists('dom_id')) {
/**
* Generates the DOM ID for a specific model.
Expand Down
20 changes: 20 additions & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
providers:
- HotwiredLaravel\TurboLaravel\TurboServiceProvider
- Workbench\App\Providers\WorkbenchAppServiceProvider

migrations:
- workbench/database/migrations

components:
- workbench/resources/views/components

workbench:
start: '/articles'
install: true
welcome: true
build:
- create-sqlite-db
- db:wipe
- migrate:refresh
assets: []
sync: []
51 changes: 22 additions & 29 deletions tests/Events/TurboStreamBroadcastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,61 @@
namespace HotwiredLaravel\TurboLaravel\Tests\Events;

use HotwiredLaravel\TurboLaravel\Events\TurboStreamBroadcast;
use HotwiredLaravel\TurboLaravel\Tests\Stubs\Models\TestModel;
use HotwiredLaravel\TurboLaravel\Tests\TestCase;
use View;
use Illuminate\Support\Facades\View;
use Workbench\Database\Factories\ArticleFactory;

class TurboStreamBroadcastTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

View::addLocation(__DIR__.'/../Stubs/views');
}

/** @test */
public function renders_turbo_stream()
{
$article = ArticleFactory::new()->create()->fresh();

$event = new TurboStreamBroadcast(
[],
'replace',
'test_target',
'article_'.$article->id,
null,
'test_models._test_model',
['testModel' => new TestModel(['id' => 1])]
'articles._article',
['article' => $article],
);

$expected = View::make('turbo-laravel::turbo-stream', [
'target' => 'test_target',
'target' => 'article_'.$article->id,
'action' => 'replace',
'partial' => 'test_models._test_model',
'partial' => 'articles._article',
'partialData' => [
'testModel' => new TestModel(['id' => 1]),
'article' => $article,
],
]);

$rendered = $event->render();
])->render();

$this->assertEquals(trim($expected), trim($rendered));
$this->assertEquals(trim($expected), trim($event->render()));
}

/** @test */
public function renders_turbo_stream_targets()
{
$article = ArticleFactory::new()->create()->fresh();

$event = new TurboStreamBroadcast(
[],
'replace',
null,
'.targets',
'test_models._test_model',
['testModel' => new TestModel(['id' => 1])],
'.articles',
'articles._article',
['article' => $article],
);

$expected = View::make('turbo-laravel::turbo-stream', [
'action' => 'replace',
'targets' => '.targets',
'partial' => 'test_models._test_model',
'targets' => '.articles',
'partial' => 'articles._article',
'partialData' => [
'testModel' => new TestModel(['id' => 1]),
'article' => $article,
],
]);

$rendered = $event->render();
])->render();

$this->assertEquals(trim($expected), trim($rendered));
$this->assertEquals(trim($expected), trim($event->render()));
}
}
Loading

0 comments on commit 2bcbef1

Please sign in to comment.