Skip to content

Commit 873fc97

Browse files
committed
Add command tests
1 parent 9ec5f30 commit 873fc97

File tree

3 files changed

+213
-0
lines changed

3 files changed

+213
-0
lines changed

tests/Commands/ActivateTest.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <aaron@hammerstone.dev>
4+
*/
5+
6+
namespace Hammerstone\Sidecar\Tests;
7+
8+
use Aws\Lambda\Exception\LambdaException;
9+
use Hammerstone\Sidecar\Clients\LambdaClient;
10+
use Hammerstone\Sidecar\Deployment;
11+
use Hammerstone\Sidecar\Events\AfterFunctionsActivated;
12+
use Hammerstone\Sidecar\Events\AfterFunctionsDeployed;
13+
use Hammerstone\Sidecar\Events\BeforeFunctionsActivated;
14+
use Hammerstone\Sidecar\Events\BeforeFunctionsDeployed;
15+
use Hammerstone\Sidecar\Sidecar;
16+
use Hammerstone\Sidecar\Tests\Support\DeploymentTestFunction;
17+
use Illuminate\Support\Facades\Event;
18+
use Mockery;
19+
20+
class ActivateTest extends BaseTest
21+
{
22+
protected $lambda;
23+
24+
protected function setUp(): void
25+
{
26+
parent::setUp();
27+
28+
Event::fake();
29+
30+
$this->lambda = $this->mock(LambdaClient::class);
31+
}
32+
33+
public function mockListingVersions()
34+
{
35+
$this->lambda->shouldReceive('listVersionsByFunction')
36+
->once()
37+
->with([
38+
'FunctionName' => 'test-FunctionName',
39+
'MaxItems' => 100,
40+
'Marker' => null
41+
])
42+
->andReturn([
43+
'Versions' => [[
44+
'FunctionName' => 'test-FunctionName',
45+
'Version' => '10',
46+
], [
47+
'FunctionName' => 'test-FunctionName',
48+
'Version' => '11',
49+
], [
50+
'FunctionName' => 'test-FunctionName',
51+
'Version' => '12',
52+
]]
53+
]);
54+
}
55+
56+
public function mockActivating()
57+
{
58+
$this->mockListingVersions();
59+
60+
$this->lambda->shouldReceive('deleteAlias')->once()->with([
61+
'FunctionName' => 'test-FunctionName',
62+
'Name' => 'active',
63+
]);
64+
65+
$this->lambda->shouldReceive('createAlias')->once()->with([
66+
'FunctionName' => 'test-FunctionName',
67+
'FunctionVersion' => '12',
68+
'Name' => 'active',
69+
]);
70+
}
71+
72+
public function assertEvents($deployed = true, $activated = true)
73+
{
74+
if ($deployed) {
75+
Event::assertDispatched(BeforeFunctionsDeployed::class);
76+
Event::assertDispatched(AfterFunctionsDeployed::class);
77+
} else {
78+
Event::assertNotDispatched(BeforeFunctionsDeployed::class);
79+
Event::assertNotDispatched(AfterFunctionsDeployed::class);
80+
}
81+
82+
if ($activated) {
83+
Event::assertDispatched(BeforeFunctionsActivated::class);
84+
Event::assertDispatched(AfterFunctionsActivated::class);
85+
} else {
86+
Event::assertNotDispatched(BeforeFunctionsActivated::class);
87+
Event::assertNotDispatched(AfterFunctionsActivated::class);
88+
}
89+
}
90+
91+
/** @test */
92+
public function it_should_activate_functions()
93+
{
94+
$this->artisan('sidecar:activate');
95+
96+
$this->assertEvents($deployed = false, $activated = true);
97+
}
98+
99+
/** @test */
100+
public function it_should_activate_functions_with_env()
101+
{
102+
$this->artisan('sidecar:activate', [
103+
'--env' => 'faked'
104+
]);
105+
106+
$this->assertEquals('faked', Sidecar::getEnvironment());
107+
108+
$this->assertEvents($deployed = false, $activated = true);
109+
}
110+
111+
}

tests/Commands/ConfigureTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <aaron@hammerstone.dev>
4+
*/
5+
6+
namespace Hammerstone\Sidecar\Tests;
7+
8+
use Aws\Lambda\Exception\LambdaException;
9+
use Hammerstone\Sidecar\Clients\LambdaClient;
10+
use Hammerstone\Sidecar\Deployment;
11+
use Hammerstone\Sidecar\Events\AfterFunctionsActivated;
12+
use Hammerstone\Sidecar\Events\AfterFunctionsDeployed;
13+
use Hammerstone\Sidecar\Events\BeforeFunctionsActivated;
14+
use Hammerstone\Sidecar\Events\BeforeFunctionsDeployed;
15+
use Hammerstone\Sidecar\Sidecar;
16+
use Hammerstone\Sidecar\Tests\Support\DeploymentTestFunction;
17+
use Illuminate\Support\Facades\Event;
18+
use Mockery;
19+
20+
class ConfigureTest extends BaseTest
21+
{
22+
/** @test */
23+
public function test_todo()
24+
{
25+
$this->markTestIncomplete('Test the configuration command.');
26+
}
27+
28+
}

tests/Commands/DeployTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <aaron@hammerstone.dev>
4+
*/
5+
6+
namespace Hammerstone\Sidecar\Tests;
7+
8+
use Aws\Lambda\Exception\LambdaException;
9+
use Hammerstone\Sidecar\Clients\LambdaClient;
10+
use Hammerstone\Sidecar\Deployment;
11+
use Hammerstone\Sidecar\Events\AfterFunctionsActivated;
12+
use Hammerstone\Sidecar\Events\AfterFunctionsDeployed;
13+
use Hammerstone\Sidecar\Events\BeforeFunctionsActivated;
14+
use Hammerstone\Sidecar\Events\BeforeFunctionsDeployed;
15+
use Hammerstone\Sidecar\Sidecar;
16+
use Hammerstone\Sidecar\Tests\Support\DeploymentTestFunction;
17+
use Illuminate\Support\Facades\Event;
18+
use Mockery;
19+
20+
class DeployTest extends DeploymentTest
21+
{
22+
23+
/** @test */
24+
public function it_deploys_the_functions_in_the_config()
25+
{
26+
config()->set('sidecar.functions', [
27+
DeploymentTestFunction::class
28+
]);
29+
30+
$this->mockCreatingFunction();
31+
32+
$this->artisan('sidecar:deploy');
33+
34+
$this->assertEvents($deployed = true, $activated = false);
35+
}
36+
37+
38+
/** @test */
39+
public function it_deploys_and_activates_the_functions_in_the_config()
40+
{
41+
config()->set('sidecar.functions', [
42+
DeploymentTestFunction::class
43+
]);
44+
45+
$this->mockCreatingFunction();
46+
$this->mockActivating();
47+
48+
$this->artisan('sidecar:deploy', [
49+
'--activate' => true
50+
]);
51+
52+
$this->assertEvents($deployed = true, $activated = true);
53+
}
54+
55+
/** @test */
56+
public function it_uses_a_fake_environment()
57+
{
58+
config()->set('sidecar.functions', [
59+
DeploymentTestFunction::class
60+
]);
61+
62+
$this->mockCreatingFunction();
63+
64+
$this->artisan('sidecar:deploy', [
65+
'--env' => 'faked'
66+
]);
67+
68+
$this->assertEquals('faked', Sidecar::getEnvironment());
69+
70+
$this->assertEvents($deployed = true, $activated = false);
71+
}
72+
73+
74+
}

0 commit comments

Comments
 (0)