Skip to content

Commit a78b016

Browse files
committed
Implement Code tests
1 parent 50d8e6e commit a78b016

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

tests/Unit/CodeTest.php

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

0 commit comments

Comments
 (0)