-
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
6 changed files
with
166 additions
and
5 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,3 @@ | ||
<?php | ||
|
||
uses(Tests\TestCase::class)->in('*'); |
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,30 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use Illuminate\Contracts\Config\Repository; | ||
use Illuminate\Encryption\Encrypter; | ||
use Orchestra\Testbench\Concerns\WithWorkbench; | ||
use Orchestra\Testbench\TestCase as BaseTestCase; | ||
use Vessel\VesselManager; | ||
|
||
abstract class TestCase extends BaseTestCase | ||
{ | ||
use WithWorkbench; | ||
|
||
protected $enablesPackageDiscoveries = true; | ||
|
||
protected function defineRoutes($router): void | ||
{ | ||
$router->get('/context', fn (): string => VesselManager::getContextId()); | ||
} | ||
|
||
protected function defineEnvironment($app) | ||
{ | ||
tap($app['config'], function (Repository $config) { | ||
$config->set('app.key', 'base64:'.base64_encode( | ||
Encrypter::generateKey($config['app.cipher']) | ||
)); | ||
}); | ||
} | ||
} |
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,79 @@ | ||
<?php | ||
|
||
use Illuminate\View\ViewException; | ||
use Livewire\Component; | ||
use Vessel\Attributes\Vessel; | ||
use Vessel\BaseVessel; | ||
use Vessel\Exceptions\CannotCallVesselDirectlyException; | ||
|
||
use function Pest\Livewire\livewire; | ||
|
||
class TestVessel extends BaseVessel | ||
{ | ||
public string $filter = 'one'; | ||
} | ||
|
||
class One extends Component | ||
{ | ||
#[Vessel] | ||
public function state(): string | ||
{ | ||
return TestVessel::class; | ||
} | ||
|
||
public function render(): string | ||
{ | ||
return '<div></div>'; | ||
} | ||
} | ||
|
||
class Two extends Component | ||
{ | ||
#[Vessel] | ||
public function state(): string | ||
{ | ||
return TestVessel::class; | ||
} | ||
|
||
public function render(): string | ||
{ | ||
return '<div></div>'; | ||
} | ||
} | ||
|
||
test('should get and set vessel properties', function (): void { | ||
$one = livewire(One::class); | ||
$two = livewire(Two::class); | ||
|
||
expect($one->get('state.filter'))->toBe($two->get('state.filter')); | ||
|
||
$one->state->filter = 'two'; | ||
|
||
expect($one->state->filter)->toBe('two'); | ||
expect($one->get('state.filter'))->toBe($two->get('state.filter')); | ||
|
||
$two->state->filter = 'one'; | ||
|
||
expect($one->state->filter)->toBe('one'); | ||
expect($one->get('state.filter'))->toBe($two->get('state.filter')); | ||
}); | ||
|
||
test('should throw exception when calling vessel method directly', function (): void { | ||
livewire(One::class)->call('state'); | ||
})->throws(CannotCallVesselDirectlyException::class); | ||
|
||
test('should throw exception when vessel method does not return vessel', function (): void { | ||
livewire((new class() extends Component | ||
{ | ||
#[Vessel] | ||
public function state(): string | ||
{ | ||
return 'invalid'; | ||
} | ||
|
||
public function render(): string | ||
{ | ||
return '<div></div>'; | ||
} | ||
})::class)->get('state'); | ||
})->throws(ViewException::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,45 @@ | ||
<?php | ||
|
||
use Livewire\Component; | ||
use Vessel\BaseVessel; | ||
|
||
use function Livewire\invade; | ||
|
||
beforeEach(function (): void { | ||
$component = new class() extends Component | ||
{ | ||
public function render(): string | ||
{ | ||
return '<div></div>'; | ||
} | ||
}; | ||
|
||
$this->vessel = new class(app('livewire')->new($component::class)) extends BaseVessel | ||
{ | ||
public string $property; | ||
|
||
public function init(): void | ||
{ | ||
$this->property = 'value'; | ||
} | ||
}; | ||
}); | ||
|
||
test('should get public properties', function (): void { | ||
$props = $this->vessel->getPublicProperties(); | ||
|
||
expect(count($props))->toBe(1); | ||
expect($props[0]->getName())->toBe('property'); | ||
}); | ||
|
||
test('should transform to array', function (): void { | ||
$props = invade($this->vessel)->toArray(); | ||
|
||
expect($props)->toBe(['property' => 'value']); | ||
}); | ||
|
||
test('should get lifetime from session by default', function (): void { | ||
$lifetime = invade($this->vessel)->lifetime; | ||
|
||
expect($lifetime)->toBe(120); | ||
}); |
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,9 @@ | ||
<?php | ||
|
||
use Vessel\VesselManager; | ||
|
||
use function Pest\Laravel\get; | ||
|
||
test('should set context id from header', function (): void { | ||
get('/context', [VesselManager::CONTEXT_HEADER => 'test'])->assertContent('test'); | ||
}); |