Skip to content

Commit

Permalink
Merge pull request #433 from varemel/main
Browse files Browse the repository at this point in the history
Fix telegraph fake attached files not clearing between fake requests (ISSUE-432)
  • Loading branch information
fabio-ivona authored Jan 3, 2024
2 parents 8f90af8 + 563434b commit 1359453
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
9 changes: 9 additions & 0 deletions src/Facades/Telegraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ public static function fake(array $replies = []): TelegraphFake
return $fake;
}

public static function getFacadeRoot()
{
$instance = parent::getFacadeRoot();
if ($instance instanceof TelegraphFake) {
$instance->prepareForNewRequest();
}
return $instance;
}

protected static function getFacadeAccessor(): string
{
return 'telegraph';
Expand Down
5 changes: 5 additions & 0 deletions src/Support/Testing/Fakes/TelegraphFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use DefStudio\Telegraph\ScopedPayloads\TelegraphQuizPayload;
use DefStudio\Telegraph\Telegraph;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Assert;

class TelegraphFake extends Telegraph
Expand All @@ -30,6 +31,10 @@ public function __construct(array $replies = [])
$this->replies = $replies;
}

public function prepareForNewRequest(): void {
$this->files = new Collection();
}

public function editMedia(int $messageId): TelegraphEditMediaFake
{
$fake = new TelegraphEditMediaFake($this->replies);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use DefStudio\Telegraph\Facades\Telegraph as TelegraphFacade;

test('Clear Telegraph Fake attached files for every new request', function() {
TelegraphFacade::fake();
$chat = make_chat();
$chat->photo(Storage::path('photo.jpg'))->message('test');
$telegraphB = $chat->message('123');
$filesB = $telegraphB->toArray()['files'];

expect($filesB)->toBeEmpty();
});
28 changes: 4 additions & 24 deletions tests/Unit/Storage/FileStoreDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@
use DefStudio\Telegraph\Concerns\HasStorage;
use DefStudio\Telegraph\Contracts\Storable;
use DefStudio\Telegraph\Models\TelegraphChat;
use DefStudio\Telegraph\Tests\Unit\Storage\TestStorable;
use Illuminate\Support\Str;

beforeEach(function () {
Storage::fake();
});

it('can store and retrieve data', function (string $key, mixed $value) {
$class = new class () implements Storable {
use HasStorage;

public function storageKey(): string
{
return 'foo';
}
};
$class = new TestStorable();

$class->storage('file')->set($key, $value);

Expand All @@ -43,14 +37,7 @@ public function storageKey(): string
]);

it('can store and retrieve a model', function () {
$class = new class () implements Storable {
use HasStorage;

public function storageKey(): string
{
return 'foo';
}
};
$class = new TestStorable();

$model = TelegraphChat::factory()->create();

Expand All @@ -75,14 +62,7 @@ public function storageKey(): string
});

it('can store and retrieve a nested model', function () {
$class = new class () implements Storable {
use HasStorage;

public function storageKey(): string
{
return 'foo';
}
};
$class = new TestStorable();

$models = TelegraphChat::factory()->count(4)->create();

Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/Storage/TestStorable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace DefStudio\Telegraph\Tests\Unit\Storage;

use DefStudio\Telegraph\Concerns\HasStorage;
use DefStudio\Telegraph\Contracts\Storable;

class TestStorable implements Storable {
use HasStorage;

public function storageKey(): string
{
return 'foo';
}
}

0 comments on commit 1359453

Please sign in to comment.