|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Console; |
| 4 | + |
| 5 | +use Illuminate\Support\Carbon; |
| 6 | +use Illuminate\Support\Facades\DB; |
| 7 | +use Orchestra\Testbench\Concerns\WithWorkbench; |
| 8 | +use Orchestra\Testbench\TestCase; |
| 9 | + |
| 10 | +class PurgeCommand extends TestCase |
| 11 | +{ |
| 12 | + use WithWorkbench; |
| 13 | + |
| 14 | + public function test_it_can_purge_tokens() |
| 15 | + { |
| 16 | + $this->travelTo(Carbon::create(2000, 1, 8)); |
| 17 | + |
| 18 | + $query = DB::pretend(function () { |
| 19 | + $this->artisan('passport:purge') |
| 20 | + ->expectsOutputToContain('Purged revoked items and items expired for more than 1 week.'); |
| 21 | + }); |
| 22 | + |
| 23 | + $this->assertSame([ |
| 24 | + 'delete from "oauth_access_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')', |
| 25 | + 'delete from "oauth_auth_codes" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')', |
| 26 | + 'delete from "oauth_refresh_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')', |
| 27 | + ], array_column($query, 'query')); |
| 28 | + } |
| 29 | + |
| 30 | + public function test_it_can_purge_revoked_tokens() |
| 31 | + { |
| 32 | + $query = DB::pretend(function () { |
| 33 | + $this->artisan('passport:purge', ['--revoked' => true]) |
| 34 | + ->expectsOutputToContain('Purged revoked items.'); |
| 35 | + }); |
| 36 | + |
| 37 | + $this->assertSame([ |
| 38 | + 'delete from "oauth_access_tokens" where ("revoked" = 1)', |
| 39 | + 'delete from "oauth_auth_codes" where ("revoked" = 1)', |
| 40 | + 'delete from "oauth_refresh_tokens" where ("revoked" = 1)', |
| 41 | + ], array_column($query, 'query')); |
| 42 | + } |
| 43 | + |
| 44 | + public function test_it_can_purge_expired_tokens() |
| 45 | + { |
| 46 | + $this->travelTo(Carbon::create(2000, 1, 8)); |
| 47 | + |
| 48 | + $query = DB::pretend(function () { |
| 49 | + $this->artisan('passport:purge', ['--expired' => true]) |
| 50 | + ->expectsOutputToContain('Purged items expired for more than 1 week.'); |
| 51 | + }); |
| 52 | + |
| 53 | + $this->assertSame([ |
| 54 | + 'delete from "oauth_access_tokens" where ("expires_at" < \'2000-01-01 00:00:00\')', |
| 55 | + 'delete from "oauth_auth_codes" where ("expires_at" < \'2000-01-01 00:00:00\')', |
| 56 | + 'delete from "oauth_refresh_tokens" where ("expires_at" < \'2000-01-01 00:00:00\')', |
| 57 | + ], array_column($query, 'query')); |
| 58 | + } |
| 59 | + |
| 60 | + public function test_it_can_purge_revoked_and_expired_tokens() |
| 61 | + { |
| 62 | + $this->travelTo(Carbon::create(2000, 1, 8)); |
| 63 | + |
| 64 | + $query = DB::pretend(function () { |
| 65 | + $this->artisan('passport:purge', ['--revoked' => true, '--expired' => true]) |
| 66 | + ->expectsOutputToContain('Purged revoked items and items expired for more than 1 week.'); |
| 67 | + }); |
| 68 | + |
| 69 | + $this->assertSame([ |
| 70 | + 'delete from "oauth_access_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')', |
| 71 | + 'delete from "oauth_auth_codes" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')', |
| 72 | + 'delete from "oauth_refresh_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')', |
| 73 | + ], array_column($query, 'query')); |
| 74 | + } |
| 75 | + |
| 76 | + public function test_it_can_purge_tokens_by_hours() |
| 77 | + { |
| 78 | + $this->travelTo(Carbon::create(2000, 1, 1, 2)); |
| 79 | + |
| 80 | + $query = DB::pretend(function () { |
| 81 | + $this->artisan('passport:purge', ['--hours' => 2]) |
| 82 | + ->expectsOutputToContain('Purged revoked items and items expired for more than 2 hours.'); |
| 83 | + }); |
| 84 | + |
| 85 | + $this->assertSame([ |
| 86 | + 'delete from "oauth_access_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')', |
| 87 | + 'delete from "oauth_auth_codes" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')', |
| 88 | + 'delete from "oauth_refresh_tokens" where ("revoked" = 1 or "expires_at" < \'2000-01-01 00:00:00\')', |
| 89 | + ], array_column($query, 'query')); |
| 90 | + } |
| 91 | +} |
0 commit comments