[增添]添加了datasource的setting数据库以及默认值
This commit is contained in:
124
vendor/danharrin/livewire-rate-limiting/tests/RateLimitingTest.php
vendored
Normal file
124
vendor/danharrin/livewire-rate-limiting/tests/RateLimitingTest.php
vendored
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace DanHarrin\LivewireRateLimiting\Tests;
|
||||
|
||||
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
|
||||
use Livewire\Livewire;
|
||||
use Livewire\Volt\Volt;
|
||||
|
||||
class RateLimitingTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function can_rate_limit()
|
||||
{
|
||||
$component = Livewire::test(Component::class);
|
||||
|
||||
$component
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0)
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0)
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0)
|
||||
->call('limit')
|
||||
->assertNotSet('secondsUntilAvailable', 0);
|
||||
|
||||
sleep(1);
|
||||
|
||||
$component
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function can_hit_and_clear_rate_limiter()
|
||||
{
|
||||
Livewire::test(Component::class)
|
||||
->call('hit')
|
||||
->call('hit')
|
||||
->call('hit')
|
||||
->call('limit')
|
||||
->assertNotSet('secondsUntilAvailable', 0)
|
||||
->call('clear')
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function can_rate_limit_volt()
|
||||
{
|
||||
$this->mountVolt();
|
||||
$component = Volt::test('volt-component');
|
||||
|
||||
$component
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0)
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0)
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0)
|
||||
->call('limit')
|
||||
->assertNotSet('secondsUntilAvailable', 0);
|
||||
|
||||
sleep(1);
|
||||
|
||||
$component
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function can_hit_and_clear_rate_limiter_volt()
|
||||
{
|
||||
$this->mountVolt();
|
||||
Volt::test('volt-component')
|
||||
->call('hit')
|
||||
->call('hit')
|
||||
->call('hit')
|
||||
->call('limit')
|
||||
->assertNotSet('secondsUntilAvailable', 0)
|
||||
->call('clear')
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0);
|
||||
}
|
||||
|
||||
protected function mountVolt()
|
||||
{
|
||||
Volt::mount([
|
||||
__DIR__ . '/views',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
class Component extends \Livewire\Component
|
||||
{
|
||||
use \DanHarrin\LivewireRateLimiting\WithRateLimiting;
|
||||
|
||||
public $secondsUntilAvailable;
|
||||
|
||||
public function clear()
|
||||
{
|
||||
$this->clearRateLimiter('limit');
|
||||
}
|
||||
|
||||
public function hit()
|
||||
{
|
||||
$this->hitRateLimiter('limit', 1);
|
||||
}
|
||||
|
||||
public function limit()
|
||||
{
|
||||
try {
|
||||
$this->rateLimit(3, 1);
|
||||
} catch (TooManyRequestsException $exception) {
|
||||
return $this->secondsUntilAvailable = $exception->secondsUntilAvailable;
|
||||
}
|
||||
|
||||
$this->secondsUntilAvailable = 0;
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('component');
|
||||
}
|
||||
}
|
||||
28
vendor/danharrin/livewire-rate-limiting/tests/TestCase.php
vendored
Normal file
28
vendor/danharrin/livewire-rate-limiting/tests/TestCase.php
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace DanHarrin\LivewireRateLimiting\Tests;
|
||||
|
||||
use Livewire\LivewireServiceProvider;
|
||||
use Livewire\Volt\Volt;
|
||||
use Livewire\Volt\VoltServiceProvider;
|
||||
|
||||
class TestCase extends \Orchestra\Testbench\TestCase
|
||||
{
|
||||
protected function getEnvironmentSetUp($app)
|
||||
{
|
||||
$app['config']->set('view.paths', [
|
||||
__DIR__ . '/views',
|
||||
resource_path('views'),
|
||||
]);
|
||||
|
||||
$app['config']->set('app.key', 'base64:Hupx3yAySikrM2/edkZQNQHslgDWYfiBfCuSThJ5SK8=');
|
||||
}
|
||||
|
||||
protected function getPackageProviders($app)
|
||||
{
|
||||
return [
|
||||
LivewireServiceProvider::class,
|
||||
VoltServiceProvider::class,
|
||||
];
|
||||
}
|
||||
}
|
||||
1
vendor/danharrin/livewire-rate-limiting/tests/views/component.blade.php
vendored
Normal file
1
vendor/danharrin/livewire-rate-limiting/tests/views/component.blade.php
vendored
Normal file
@@ -0,0 +1 @@
|
||||
<div></div>
|
||||
36
vendor/danharrin/livewire-rate-limiting/tests/views/volt-component.blade.php
vendored
Normal file
36
vendor/danharrin/livewire-rate-limiting/tests/views/volt-component.blade.php
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Livewire\Volt\Component;
|
||||
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
|
||||
|
||||
new class extends Component {
|
||||
use \DanHarrin\LivewireRateLimiting\WithRateLimiting;
|
||||
|
||||
public $secondsUntilAvailable;
|
||||
|
||||
public function clear()
|
||||
{
|
||||
$this->clearRateLimiter('limit', component: 'VoltComponent');
|
||||
}
|
||||
|
||||
public function hit()
|
||||
{
|
||||
$this->hitRateLimiter('limit', 1, component: 'VoltComponent');
|
||||
}
|
||||
|
||||
public function limit()
|
||||
{
|
||||
try {
|
||||
$this->rateLimit(3, 1, component: 'VoltComponent');
|
||||
} catch (TooManyRequestsException $exception) {
|
||||
return $this->secondsUntilAvailable = $exception->secondsUntilAvailable;
|
||||
}
|
||||
|
||||
$this->secondsUntilAvailable = 0;
|
||||
}
|
||||
|
||||
}; ?>
|
||||
|
||||
<div>
|
||||
//
|
||||
</div>
|
||||
Reference in New Issue
Block a user