Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event Assertions #1

Merged
merged 2 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 58 additions & 24 deletions tests/DeploymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Aws\Lambda\Exception\LambdaException;
use Hammerstone\Sidecar\Clients\LambdaClient;
use Hammerstone\Sidecar\Deployment;
use Hammerstone\Sidecar\Events\AfterFunctionsActivated;
use Hammerstone\Sidecar\Events\AfterFunctionsDeployed;
use Hammerstone\Sidecar\Events\BeforeFunctionsActivated;
Expand Down Expand Up @@ -130,18 +131,67 @@ public function mockActivating()
]);
}

public function assertEvents($deployed = true, $activated = true)
{
if ($deployed) {
Event::assertDispatched(BeforeFunctionsDeployed::class);
Event::assertDispatched(AfterFunctionsDeployed::class);
} else {
Event::assertNotDispatched(BeforeFunctionsDeployed::class);
Event::assertNotDispatched(AfterFunctionsDeployed::class);
}

if ($activated) {
Event::assertDispatched(BeforeFunctionsActivated::class);
Event::assertDispatched(AfterFunctionsActivated::class);
} else {
Event::assertNotDispatched(BeforeFunctionsActivated::class);
Event::assertNotDispatched(AfterFunctionsActivated::class);
}
}

/** @test */
public function it_deploys_a_function_that_doesnt_exist()
{
$this->mockCreatingFunction();

DeploymentTestFunction::deploy($activate = false);

Event::assertDispatched(BeforeFunctionsDeployed::class);
Event::assertDispatched(AfterFunctionsDeployed::class);
$this->assertEvents($deployed = true, $activated = false);
}

/** @test */
public function it_deploys_a_function_that_doesnt_exist_from_the_deployment_class()
{
$this->mockCreatingFunction();

Event::assertNotDispatched(BeforeFunctionsActivated::class);
Event::assertNotDispatched(AfterFunctionsActivated::class);
Deployment::make(DeploymentTestFunction::class)->deploy($activate = false);

$this->assertEvents($deployed = true, $activated = false);
}

/** @test */
public function it_deploys_an_array_of_functions()
{
$this->mockCreatingFunction();

Deployment::make([DeploymentTestFunction::class])->deploy($activate = false);

$this->assertEvents($deployed = true, $activated = false);
}

/** @test */
public function it_deploys_the_functions_in_the_config()
{
config()->set('sidecar.functions', [
DeploymentTestFunction::class
]);

$this->mockCreatingFunction();

Deployment::make()->deploy($activate = false);

$this->assertEvents($deployed = true, $activated = false);
}

/** @test */
Expand All @@ -152,11 +202,7 @@ public function it_deploys_and_activates_a_function_that_doesnt_exist()

DeploymentTestFunction::deploy($activate = true);

Event::assertDispatched(BeforeFunctionsDeployed::class);
Event::assertDispatched(AfterFunctionsDeployed::class);

Event::assertDispatched(BeforeFunctionsActivated::class);
Event::assertDispatched(AfterFunctionsActivated::class);
$this->assertEvents($deployed = true, $activated = true);
}

/** @test */
Expand All @@ -166,11 +212,7 @@ public function it_updates_an_existing_function()

DeploymentTestFunction::deploy($activate = false);

Event::assertDispatched(BeforeFunctionsDeployed::class);
Event::assertDispatched(AfterFunctionsDeployed::class);

Event::assertNotDispatched(BeforeFunctionsActivated::class);
Event::assertNotDispatched(AfterFunctionsActivated::class);
$this->assertEvents($deployed = true, $activated = false);
}

/** @test */
Expand All @@ -181,11 +223,7 @@ public function it_updates_and_activates_an_existing_function()

DeploymentTestFunction::deploy($activate = true);

Event::assertDispatched(BeforeFunctionsDeployed::class);
Event::assertDispatched(AfterFunctionsDeployed::class);

Event::assertDispatched(BeforeFunctionsActivated::class);
Event::assertDispatched(AfterFunctionsActivated::class);
$this->assertEvents($deployed = true, $activated = true);
}

/** @test */
Expand All @@ -205,10 +243,6 @@ public function it_doesnt_update_if_nothing_has_changed()

DeploymentTestFunction::deploy($activate = true);

Event::assertDispatched(BeforeFunctionsDeployed::class);
Event::assertDispatched(AfterFunctionsDeployed::class);

Event::assertDispatched(BeforeFunctionsActivated::class);
Event::assertDispatched(AfterFunctionsActivated::class);
$this->assertEvents($deployed = true, $activated = true);
}
}
35 changes: 35 additions & 0 deletions tests/ExecuteMultipleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@
use Aws\Result;
use GuzzleHttp\Promise\PromiseInterface;
use Hammerstone\Sidecar\Clients\LambdaClient;
use Hammerstone\Sidecar\Events\AfterFunctionExecuted;
use Hammerstone\Sidecar\Events\BeforeFunctionExecuted;
use Hammerstone\Sidecar\Results\PendingResult;
use Hammerstone\Sidecar\Sidecar;
use Hammerstone\Sidecar\Tests\Support\EmptyTestFunction;
use Illuminate\Support\Facades\Event;
use Mockery;

class ExecuteMultipleTest extends BaseTest
{
protected function setUp(): void
{
parent::setUp();

Event::fake();
}

protected function expectedArgs($args = [])
{
return array_merge([
Expand All @@ -25,6 +35,17 @@ protected function expectedArgs($args = [])
], $args);
}

public function assertEvents($executed = 1)
{
if ($executed) {
Event::assertDispatched(BeforeFunctionExecuted::class, $executed);
Event::assertDispatched(AfterFunctionExecuted::class, $executed);
} else {
Event::assertNotDispatched(BeforeFunctionExecuted::class);
Event::assertNotDispatched(AfterFunctionExecuted::class);
}
}

protected function mockMultiple()
{
$result = Mockery::mock(PromiseInterface::class)
Expand Down Expand Up @@ -68,6 +89,8 @@ public function execute_many_by_function()
], [
'C' => 3
]]);

$this->assertEvents(3);
}

/** @test */
Expand All @@ -82,6 +105,8 @@ public function execute_many_by_facade()
], [
'C' => 3
]]);

$this->assertEvents(3);
}

/** @test */
Expand All @@ -96,6 +121,8 @@ public function execute_many_by_facade_with_instantiated_class()
], [
'C' => 3
]]);

$this->assertEvents(3);
}

/** @test */
Expand All @@ -112,6 +139,8 @@ public function execute_many_by_function_int()
->andReturn($result);

EmptyTestFunction::executeMany(5);

$this->assertEvents(5);
}

/** @test */
Expand All @@ -128,6 +157,8 @@ public function execute_many_by_facade_int()
->andReturn($result);

Sidecar::executeMany(EmptyTestFunction::class, 5);

$this->assertEvents(5);
}

/** @test */
Expand All @@ -144,6 +175,8 @@ public function execute_many_by_facade_int_with_instantiated_class()
->andReturn($result);

Sidecar::executeMany(new EmptyTestFunction, 5);

$this->assertEvents(5);
}

/** @test */
Expand All @@ -161,5 +194,7 @@ public function execute_many_async_by_function()
$results = EmptyTestFunction::executeMany(2, $async = true);

$this->assertInstanceOf(PendingResult::class, $results[0]);

$this->assertEvents(2);
}
}
39 changes: 39 additions & 0 deletions tests/ExecuteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@

use Aws\Result;
use Hammerstone\Sidecar\Clients\LambdaClient;
use Hammerstone\Sidecar\Events\AfterFunctionExecuted;
use Hammerstone\Sidecar\Events\BeforeFunctionExecuted;
use Hammerstone\Sidecar\Sidecar;
use Hammerstone\Sidecar\Tests\Support\EmptyTestFunction;
use Illuminate\Support\Facades\Event;

class ExecuteTest extends BaseTest
{
protected function setUp(): void
{
parent::setUp();

Event::fake();
}

protected function mockMethod($method, $with, $return)
{
if (is_null($return)) {
Expand Down Expand Up @@ -47,12 +57,25 @@ protected function mockInvokeAsync($with = [], $return = null)
return $this->mockMethod('invokeAsync', $with, $return);
}

public function assertEvents($executed = 1)
{
if ($executed) {
Event::assertDispatched(BeforeFunctionExecuted::class, $executed);
Event::assertDispatched(AfterFunctionExecuted::class, $executed);
} else {
Event::assertNotDispatched(BeforeFunctionExecuted::class);
Event::assertNotDispatched(AfterFunctionExecuted::class);
}
}

/** @test */
public function basic_execution_by_function()
{
$this->mockInvoke();

EmptyTestFunction::execute();

$this->assertEvents();
}

/** @test */
Expand All @@ -61,6 +84,8 @@ public function basic_execution_by_facade()
$this->mockInvoke();

Sidecar::execute(EmptyTestFunction::class);

$this->assertEvents();
}

/** @test */
Expand All @@ -69,6 +94,8 @@ public function basic_execution_by_facade_with_instantiated_class()
$this->mockInvoke();

Sidecar::execute(new EmptyTestFunction);

$this->assertEvents();
}

/** @test */
Expand All @@ -81,6 +108,8 @@ public function execution_with_payload_by_function()
EmptyTestFunction::execute([
'foo' => 'bar'
]);

$this->assertEvents();
}

/** @test */
Expand All @@ -93,6 +122,8 @@ public function execution_with_payload_by_facade()
Sidecar::execute(EmptyTestFunction::class, [
'foo' => 'bar'
]);

$this->assertEvents();
}

/** @test */
Expand All @@ -105,6 +136,8 @@ public function execution_with_payload_by_facade_with_instantiated_class()
Sidecar::execute(new EmptyTestFunction, [
'foo' => 'bar'
]);

$this->assertEvents();
}

/** @test */
Expand All @@ -117,6 +150,8 @@ public function async_execution_by_function()
EmptyTestFunction::execute([
'foo' => 'bar'
], $async = true);

$this->assertEvents();
}

/** @test */
Expand All @@ -129,6 +164,8 @@ public function async_execution_by_facade()
Sidecar::execute(EmptyTestFunction::class, [
'foo' => 'bar'
], $async = true);

$this->assertEvents();
}

/** @test */
Expand All @@ -141,5 +178,7 @@ public function async_execution_by_facade_with_instantiated_class()
Sidecar::execute(new EmptyTestFunction, [
'foo' => 'bar'
], $async = true);

$this->assertEvents();
}
}