diff --git a/src/Facades/Telegraph.php b/src/Facades/Telegraph.php index 045ed4f5d..3296c78ed 100644 --- a/src/Facades/Telegraph.php +++ b/src/Facades/Telegraph.php @@ -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'; diff --git a/src/Support/Testing/Fakes/TelegraphFake.php b/src/Support/Testing/Fakes/TelegraphFake.php index afa6ef95f..eade1f277 100644 --- a/src/Support/Testing/Fakes/TelegraphFake.php +++ b/src/Support/Testing/Fakes/TelegraphFake.php @@ -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 @@ -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); diff --git a/tests/Regression/Issue-432-ClearTelegraphFakeFilesForNewRequestTest.php b/tests/Regression/Issue-432-ClearTelegraphFakeFilesForNewRequestTest.php new file mode 100644 index 000000000..82c4a7a51 --- /dev/null +++ b/tests/Regression/Issue-432-ClearTelegraphFakeFilesForNewRequestTest.php @@ -0,0 +1,13 @@ +photo(Storage::path('photo.jpg'))->message('test'); + $telegraphB = $chat->message('123'); + $filesB = $telegraphB->toArray()['files']; + + expect($filesB)->toBeEmpty(); +}); diff --git a/tests/Unit/Storage/FileStoreDriverTest.php b/tests/Unit/Storage/FileStoreDriverTest.php index 44a40b9e8..98a36a6ec 100644 --- a/tests/Unit/Storage/FileStoreDriverTest.php +++ b/tests/Unit/Storage/FileStoreDriverTest.php @@ -3,6 +3,7 @@ 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 () { @@ -10,14 +11,7 @@ }); 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); @@ -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(); @@ -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(); diff --git a/tests/Unit/Storage/TestStorable.php b/tests/Unit/Storage/TestStorable.php new file mode 100644 index 000000000..240faf2b2 --- /dev/null +++ b/tests/Unit/Storage/TestStorable.php @@ -0,0 +1,15 @@ +