Skip to content

Commit

Permalink
Merge pull request #532 from livewire/cp/testing-updates
Browse files Browse the repository at this point in the history
Testing core updates
  • Loading branch information
calebporzio committed Jan 17, 2020
2 parents d7043c2 + 96d0cb8 commit 3c3f5ad
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 109 deletions.
3 changes: 3 additions & 0 deletions src/Component.php
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Str;
use Illuminate\Support\ViewErrorBag;
use Illuminate\Support\Traits\Macroable;
use Livewire\Livewire;

abstract class Component
{
Expand Down Expand Up @@ -102,6 +103,8 @@ public function output($errors = null)

$output = $view->render();

Livewire::dispatch('view:render', $view);

$engine->endLivewireRendering();

return $output;
Expand Down
2 changes: 1 addition & 1 deletion src/Connection/ConnectionHandler.php
Expand Up @@ -21,7 +21,7 @@ public function handle($payload)
$this->processMessage($action['type'], $action['payload'], $instance);
}
} catch (ValidationException $e) {
$this->interceptValidator($e->validator);
Livewire::dispatch('failed-validation', $e->validator);

$errors = $e->validator->errors();
}
Expand Down
8 changes: 7 additions & 1 deletion src/HydrationMiddleware/InterceptRedirects.php
Expand Up @@ -13,7 +13,13 @@ public static function hydrate($unHydratedInstance, $request)
static::$redirectorCache = app('redirect');

app()->bind('redirect', function () use ($unHydratedInstance) {
return app(Redirector::class)->component($unHydratedInstance);
$redirector = app(Redirector::class)->component($unHydratedInstance);

if (app('session.store')) {
$redirector->setSession(app('session.store'));
}

return $redirector;;
});
}

Expand Down
17 changes: 17 additions & 0 deletions src/LivewireManager.php
Expand Up @@ -20,6 +20,7 @@ class LivewireManager
protected $hydrationMiddleware = [];
protected $initialHydrationMiddleware = [];
protected $initialDehydrationMiddleware = [];
protected $listeners = [];

public static $isLivewireRequestTestingOverride;

Expand Down Expand Up @@ -123,6 +124,8 @@ public function mount($name, ...$options)
'initial-data' => array_diff_key($response->toArray(), array_flip(['dom'])),
]);

$this->dispatch('mounted', $response);

return $response;
}

Expand Down Expand Up @@ -362,4 +365,18 @@ public function getRootElementTagName($dom)

return $matches[1][0];
}

public function dispatch($event, ...$params)
{
foreach ($this->listeners[$event] ?? [] as $listener) {
$listener(...$params);
}
}

public function listen($event, $callback)
{
$this->listeners[$event] ?? $this->listeners[$event] = [];

$this->listeners[$event][] = $callback;
}
}
1 change: 0 additions & 1 deletion src/LivewireServiceProvider.php
Expand Up @@ -14,7 +14,6 @@
use Illuminate\Support\Facades\Route as RouteFacade;
use Illuminate\Foundation\Http\Middleware\TrimStrings;
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;
use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Foundation\Testing\TestResponse;
use Livewire\Commands\{
ComponentParser,
Expand Down
64 changes: 16 additions & 48 deletions src/Testing/Concerns/MakesAssertions.php
Expand Up @@ -3,6 +3,7 @@
namespace Livewire\Testing\Concerns;

use Illuminate\Support\MessageBag;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\Assert as PHPUnit;

trait MakesAssertions
Expand Down Expand Up @@ -139,54 +140,6 @@ public function assertHasNoErrors($keys = [])
return $this;
}

public function assertStatus($status)
{
$actual = $this->lastHttpException->getStatusCode();

PHPUnit::assertTrue(
$actual === $status,
"Expected status code [{$status}] but received [{$actual}]."
);

return $this;
}

public function assertNotFound()
{
$actual = $this->lastHttpException->getStatusCode();

PHPUnit::assertTrue(
$actual === 404,
'Response status code ['.$actual.'] is not a not found status code.'
);

return $this;
}

public function assertForbidden()
{
$actual = $this->lastHttpException->getStatusCode();

PHPUnit::assertTrue(
$actual === 403,
'Response status code ['.$actual.'] is not a forbidden status code.'
);

return $this;
}

public function assertUnauthorized()
{
$actual = $this->lastHttpException->getStatusCode();

PHPUnit::assertTrue(
$actual === 401,
'Response status code ['.$actual.'] is not an unauthorized status code.'
);

return $this;
}

public function assertRedirect($uri = null)
{
PHPUnit::assertIsString(
Expand All @@ -200,4 +153,19 @@ public function assertRedirect($uri = null)

return $this;
}

public function assertViewHas($key, $value = null)
{
if (is_null($value)) {
PHPUnit::assertArrayHasKey($key, $this->lastRenderedView->gatherData());
} elseif ($value instanceof \Closure) {
PHPUnit::assertTrue($value($this->lastRenderedView->gatherData()[$key]));
} elseif ($value instanceof Model) {
PHPUnit::assertTrue($value->is($this->lastRenderedView->gatherData()[$key]));
} else {
PHPUnit::assertEquals($value, $this->lastRenderedView->gatherData()[$key]);
}

return $this;
}
}
30 changes: 8 additions & 22 deletions src/Testing/Concerns/MakesCallsToComponent.php
Expand Up @@ -2,11 +2,13 @@

namespace Livewire\Testing\Concerns;

use Livewire\Testing\TestConnectionHandler;
use Symfony\Component\HttpKernel\Exception\HttpException;

trait MakesCallsToComponent
{
public function emit($event, ...$parameters)
{
return $this->fireEvent($event, $parameters);
}

public function fireEvent($event, ...$parameters)
{
$this->sendMessage('fireEvent', [
Expand Down Expand Up @@ -61,26 +63,10 @@ public function updateProperty($name, $value = null)

public function sendMessage($message, $payload)
{
try {
$handler = (new TestConnectionHandler);

$result = $handler->handle([
'id' => $this->payload['id'],
'name' => $this->payload['name'],
'data' => $this->payload['data'],
'children' => $this->payload['children'],
'checksum' => $this->payload['checksum'],
'errorBag' => $this->payload['errorBag'],
'actionQueue' => [['type' => $message, 'payload' => $payload]],
]);

if ($validator = $handler->lastValidator) {
$this->lastValidator = $validator;
}
$this->lastResponse = $this->pretendWereSendingAComponentUpdateRequest($message, $payload);

$this->updateComponent($result);
} catch (HttpException $exception) {
$this->lastHttpException = $exception;
if (! $this->lastResponse->exception) {
$this->updateComponent($this->lastResponse->original);
}
}
}
40 changes: 40 additions & 0 deletions src/Testing/MakesHttpRequestsWrapper.php
@@ -0,0 +1,40 @@
<?php

namespace Livewire\Testing;

use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\Testing\Concerns\InteractsWithExceptionHandling;
use Illuminate\Foundation\Testing\Concerns\MakesHttpRequests;
use Symfony\Component\HttpKernel\Exception\HttpException;

class MakesHttpRequestsWrapper
{
use MakesHttpRequests, InteractsWithExceptionHandling;

public function __construct($app)
{
$this->app = $app;
}

public function temporarilyDisableExceptionHandlingAndMiddleware($callback)
{
$cachedHandler = app(ExceptionHandler::class);
$cachedShouldSkipMiddleware = $this->app->shouldSkipMiddleware();

$this->withoutExceptionHandling([HttpException::class, AuthorizationException::class])->withoutMiddleware();

$callback($this);

$this->app->instance(ExceptionHandler::class, $cachedHandler);
if (! $cachedShouldSkipMiddleware) {
unset($this->app['middleware.disable']);
}
}

public function withoutHandling($except = [])
{
return $this->withoutExceptionHandling($except);
}
}
15 changes: 0 additions & 15 deletions src/Testing/TestConnectionHandler.php

This file was deleted.

0 comments on commit 3c3f5ad

Please sign in to comment.