This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
EmbeddingsTest.php
135 lines (117 loc) · 3.93 KB
/
EmbeddingsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Copyright (c) 2022 Tectalic (https://tectalic.com)
*
* For copyright and license information, please view the LICENSE file that was distributed with this source code.
*
* Please see the README.md file for usage instructions.
*/
declare(strict_types=1);
namespace Tests\Unit\Handlers;
use Http\Mock\Client;
use Nyholm\Psr7\Response;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionObject;
use Tectalic\OpenAi\Authentication;
use Tectalic\OpenAi\ClientException;
use Tectalic\OpenAi\Handlers\Embeddings;
use Tectalic\OpenAi\Manager;
use Tectalic\OpenAi\Models\Embeddings\CreateRequest;
use Tests\AssertValidateTrait;
final class EmbeddingsTest extends TestCase
{
use AssertValidateTrait;
/** @var Client */
private $mockClient;
protected function setUp(): void
{
$this->mockClient = new Client();
Manager::build(
$this->mockClient,
new Authentication('token')
);
}
protected function tearDown(): void
{
$reflectionClass = new ReflectionClass(Manager::class);
$reflectionProperty = $reflectionClass->getProperty('client');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue(null);
}
public function testMissingRequest(): void
{
$this->expectException(ClientException::class);
$this->expectExceptionMessage('Request not configured.');
(new Embeddings())->getRequest();
}
public function testUnsupportedContentTypeResponse(): void
{
$this->expectException(ClientException::class);
$this->expectExceptionMessage('Unsupported content type: text/plain');
$handler = new Embeddings();
$method = (new ReflectionObject($handler))->getMethod('parseResponse');
$method->setAccessible(true);
$method->invoke($handler, new Response(
200,
['Content-Type' => 'text/plain'],
null
));
}
public function testInvalidJsonResponse(): void
{
$this->expectException(ClientException::class);
$this->expectExceptionMessage('Failed to parse JSON response body: Syntax error');
$handler = new Embeddings();
$method = (new ReflectionObject($handler))->getMethod('parseResponse');
$method->setAccessible(true);
$method->invoke($handler, new Response(
200,
['Content-Type' => 'application/json'],
'invalidJson'
));
}
public function testUnsuccessfulResponseCode(): void
{
$this->expectException(ClientException::class);
$this->expectExceptionMessage("Unsuccessful response. HTTP status code: 418 (I'm a teapot).");
$handler = new Embeddings();
$property = (new ReflectionObject($handler))->getProperty('response');
$property->setAccessible(true);
$property->setValue($handler, new Response(418));
$handler->toModel();
}
public function toArrayDataProvider(): array
{
return [
['{}', []],
['[]', []],
['{"a": "b"}', ['a' => 'b']],
];
}
/**
* @dataProvider toArrayDataProvider
*/
public function testToArray(string $rawJsonResponse, array $expected): void
{
$handler = new Embeddings();
$property = (new ReflectionObject($handler))->getProperty('response');
$property->setAccessible(true);
$property->setValue($handler, new Response(
200,
['Content-Type' => 'application/json'],
$rawJsonResponse
));
$this->assertSame($expected, $handler->toArray());
}
public function testCreateMethod(): void
{
$request = (new Embeddings())
->create(new CreateRequest([
'model' => 'alpha0',
'input' => 'The quick brown fox jumped over the lazy dog',
]))
->getRequest();
$this->assertValidate($request);
}
}