Skip to content

Commit 770fd0d

Browse files
committed
Implement CodeTest
1 parent 8152685 commit 770fd0d

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

tests/Unit/CodeTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit;
6+
7+
use CodeMash\Code;
8+
use CodeMash\Exceptions\RequestValidationException;
9+
use Generator;
10+
11+
final class CodeTest extends CodeMashTestCase
12+
{
13+
/** @var Code */
14+
private $code;
15+
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
$this->code = new Code($this->client);
21+
}
22+
23+
/** @dataProvider provideTestExecuteFunctionFail */
24+
public function testExecuteFunctionFail(
25+
array $params,
26+
string $expectedException,
27+
string $expectedExceptionMessage
28+
): void {
29+
$this->expectException($expectedException);
30+
$this->expectExceptionMessage($expectedExceptionMessage);
31+
32+
$this->code->executeFunction($params);
33+
}
34+
35+
public static function provideTestExecuteFunctionFail(): Generator
36+
{
37+
yield 'Empty params' => [
38+
'$params' => [],
39+
'$expectedException' => RequestValidationException::class,
40+
'$expectedExceptionMessage' => '"id" is required!',
41+
];
42+
}
43+
44+
/**
45+
* @dataProvider provideTestExecuteFunction
46+
*/
47+
public function testExecuteFunction(
48+
array $params,
49+
string $expectedBody,
50+
array $expectedResponse,
51+
array $expectedResult
52+
): void {
53+
$this->client
54+
->expects($this->once())
55+
->method('request')
56+
->with(
57+
'POST',
58+
'v2/serverless/functions/testId/execute',
59+
[
60+
'headers' => [
61+
'Accept' => 'application/json',
62+
'Content-Type' => 'application/json',
63+
],
64+
'body' => $expectedBody,
65+
]
66+
)->willReturn($expectedResponse);
67+
68+
$actualResult = $this->code->executeFunction($params);
69+
70+
$this->assertSame($expectedResult, $actualResult);
71+
}
72+
73+
public static function provideTestExecuteFunction(): Generator
74+
{
75+
yield 'All parameters are set' => [
76+
'$params' => [
77+
'id' => 'testId',
78+
'template' => 'testTemplate',
79+
'qualifier' => 'testQualifier',
80+
],
81+
'$expectedBody' => '{"id":"testId","template":"testTemplate","qualifier":"testQualifier"}',
82+
'$expectedResponse' => ['result' => '{"foo":"bar"}'],
83+
'$expectedResult' => [
84+
'foo' => 'bar',
85+
],
86+
];
87+
88+
yield 'Only id is set' => [
89+
'$params' => [
90+
'id' => 'testId',
91+
],
92+
'$expectedBody' => '{"id":"testId","template":null,"qualifier":null}',
93+
'$expectedResponse' => ['result' => '{"foo":"bar"}'],
94+
'$expectedResult' => [
95+
'foo' => 'bar',
96+
],
97+
];
98+
}
99+
}

0 commit comments

Comments
 (0)