|
4 | 4 | use OpenAI\Laravel\Facades\OpenAI;
|
5 | 5 | use OpenAI\Laravel\ServiceProvider;
|
6 | 6 | use OpenAI\Resources\Completions;
|
| 7 | +use OpenAI\Responses\Completions\CreateResponse; |
| 8 | +use PHPUnit\Framework\ExpectationFailedException; |
7 | 9 |
|
8 | 10 | it('resolves resources', function () {
|
9 | 11 | $app = app();
|
|
22 | 24 |
|
23 | 25 | expect($completions)->toBeInstanceOf(Completions::class);
|
24 | 26 | });
|
| 27 | + |
| 28 | +test('fake returns the given response', function () { |
| 29 | + OpenAI::fake([ |
| 30 | + CreateResponse::fake([ |
| 31 | + 'choices' => [ |
| 32 | + [ |
| 33 | + 'text' => 'awesome!', |
| 34 | + ], |
| 35 | + ], |
| 36 | + ]), |
| 37 | + ]); |
| 38 | + |
| 39 | + $completion = OpenAI::completions()->create([ |
| 40 | + 'model' => 'text-davinci-003', |
| 41 | + 'prompt' => 'PHP is ', |
| 42 | + ]); |
| 43 | + |
| 44 | + expect($completion['choices'][0]['text'])->toBe('awesome!'); |
| 45 | +}); |
| 46 | + |
| 47 | +test('fake throws an exception if there is no more given response', function () { |
| 48 | + OpenAI::fake([ |
| 49 | + CreateResponse::fake(), |
| 50 | + ]); |
| 51 | + |
| 52 | + OpenAI::completions()->create([ |
| 53 | + 'model' => 'text-davinci-003', |
| 54 | + 'prompt' => 'PHP is ', |
| 55 | + ]); |
| 56 | + |
| 57 | + OpenAI::completions()->create([ |
| 58 | + 'model' => 'text-davinci-003', |
| 59 | + 'prompt' => 'PHP is ', |
| 60 | + ]); |
| 61 | +})->expectExceptionMessage('No fake responses left'); |
| 62 | + |
| 63 | +test('append more fake responses', function () { |
| 64 | + OpenAI::fake([ |
| 65 | + CreateResponse::fake([ |
| 66 | + 'id' => 'cmpl-1', |
| 67 | + ]), |
| 68 | + ]); |
| 69 | + |
| 70 | + OpenAI::addResponses([ |
| 71 | + CreateResponse::fake([ |
| 72 | + 'id' => 'cmpl-2', |
| 73 | + ]), |
| 74 | + ]); |
| 75 | + |
| 76 | + $completion = OpenAI::completions()->create([ |
| 77 | + 'model' => 'text-davinci-003', |
| 78 | + 'prompt' => 'PHP is ', |
| 79 | + ]); |
| 80 | + |
| 81 | + expect($completion) |
| 82 | + ->id->toBe('cmpl-1'); |
| 83 | + |
| 84 | + $completion = OpenAI::completions()->create([ |
| 85 | + 'model' => 'text-davinci-003', |
| 86 | + 'prompt' => 'PHP is ', |
| 87 | + ]); |
| 88 | + |
| 89 | + expect($completion) |
| 90 | + ->id->toBe('cmpl-2'); |
| 91 | +}); |
| 92 | + |
| 93 | +test('fake can assert a request was sent', function () { |
| 94 | + OpenAI::fake([ |
| 95 | + CreateResponse::fake(), |
| 96 | + ]); |
| 97 | + |
| 98 | + OpenAI::completions()->create([ |
| 99 | + 'model' => 'text-davinci-003', |
| 100 | + 'prompt' => 'PHP is ', |
| 101 | + ]); |
| 102 | + |
| 103 | + OpenAI::assertSent(Completions::class, function (string $method, array $parameters): bool { |
| 104 | + return $method === 'create' && |
| 105 | + $parameters['model'] === 'text-davinci-003' && |
| 106 | + $parameters['prompt'] === 'PHP is '; |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +test('fake throws an exception if a request was not sent', function () { |
| 111 | + OpenAI::fake([ |
| 112 | + CreateResponse::fake(), |
| 113 | + ]); |
| 114 | + |
| 115 | + OpenAI::assertSent(Completions::class, function (string $method, array $parameters): bool { |
| 116 | + return $method === 'create' && |
| 117 | + $parameters['model'] === 'text-davinci-003' && |
| 118 | + $parameters['prompt'] === 'PHP is '; |
| 119 | + }); |
| 120 | +})->expectException(ExpectationFailedException::class); |
| 121 | + |
| 122 | +test('fake can assert a request was sent on the resource', function () { |
| 123 | + OpenAI::fake([ |
| 124 | + CreateResponse::fake(), |
| 125 | + ]); |
| 126 | + |
| 127 | + OpenAI::completions()->create([ |
| 128 | + 'model' => 'text-davinci-003', |
| 129 | + 'prompt' => 'PHP is ', |
| 130 | + ]); |
| 131 | + |
| 132 | + OpenAI::completions()->assertSent(function (string $method, array $parameters): bool { |
| 133 | + return $method === 'create' && |
| 134 | + $parameters['model'] === 'text-davinci-003' && |
| 135 | + $parameters['prompt'] === 'PHP is '; |
| 136 | + }); |
| 137 | +}); |
| 138 | + |
| 139 | +test('fake can assert a request was sent n times', function () { |
| 140 | + OpenAI::fake([ |
| 141 | + CreateResponse::fake(), |
| 142 | + CreateResponse::fake(), |
| 143 | + ]); |
| 144 | + |
| 145 | + OpenAI::completions()->create([ |
| 146 | + 'model' => 'text-davinci-003', |
| 147 | + 'prompt' => 'PHP is ', |
| 148 | + ]); |
| 149 | + |
| 150 | + OpenAI::completions()->create([ |
| 151 | + 'model' => 'text-davinci-003', |
| 152 | + 'prompt' => 'PHP is ', |
| 153 | + ]); |
| 154 | + |
| 155 | + OpenAI::assertSent(Completions::class, 2); |
| 156 | +}); |
| 157 | + |
| 158 | +test('fake throws an exception if a request was not sent n times', function () { |
| 159 | + OpenAI::fake([ |
| 160 | + CreateResponse::fake(), |
| 161 | + CreateResponse::fake(), |
| 162 | + ]); |
| 163 | + |
| 164 | + OpenAI::completions()->create([ |
| 165 | + 'model' => 'text-davinci-003', |
| 166 | + 'prompt' => 'PHP is ', |
| 167 | + ]); |
| 168 | + |
| 169 | + OpenAI::assertSent(Completions::class, 2); |
| 170 | +})->expectException(ExpectationFailedException::class); |
| 171 | + |
| 172 | +test('fake can assert a request was not sent', function () { |
| 173 | + OpenAI::fake(); |
| 174 | + |
| 175 | + OpenAI::assertNotSent(Completions::class); |
| 176 | +}); |
| 177 | + |
| 178 | +test('fake throws an exception if a unexpected request was sent', function () { |
| 179 | + OpenAI::fake([ |
| 180 | + CreateResponse::fake(), |
| 181 | + ]); |
| 182 | + |
| 183 | + OpenAI::completions()->create([ |
| 184 | + 'model' => 'text-davinci-003', |
| 185 | + 'prompt' => 'PHP is ', |
| 186 | + ]); |
| 187 | + |
| 188 | + OpenAI::assertNotSent(Completions::class); |
| 189 | +})->expectException(ExpectationFailedException::class); |
| 190 | + |
| 191 | +test('fake can assert a request was not sent on the resource', function () { |
| 192 | + OpenAI::fake([ |
| 193 | + CreateResponse::fake(), |
| 194 | + ]); |
| 195 | + |
| 196 | + OpenAI::completions()->assertNotSent(); |
| 197 | +}); |
| 198 | + |
| 199 | +test('fake can assert no request was sent', function () { |
| 200 | + OpenAI::fake(); |
| 201 | + |
| 202 | + OpenAI::assertNothingSent(); |
| 203 | +}); |
| 204 | + |
| 205 | +test('fake throws an exception if any request was sent when non was expected', function () { |
| 206 | + OpenAI::fake([ |
| 207 | + CreateResponse::fake(), |
| 208 | + ]); |
| 209 | + |
| 210 | + OpenAI::completions()->create([ |
| 211 | + 'model' => 'text-davinci-003', |
| 212 | + 'prompt' => 'PHP is ', |
| 213 | + ]); |
| 214 | + |
| 215 | + OpenAI::assertNothingSent(); |
| 216 | +})->expectException(ExpectationFailedException::class); |
0 commit comments