[增添]添加了datasource的setting数据库以及默认值

This commit is contained in:
makotocc0107
2024-08-27 09:57:44 +08:00
parent d111dfaea4
commit 72eb990970
10955 changed files with 978898 additions and 0 deletions

View 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');
}
}

View 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,
];
}
}

View File

@@ -0,0 +1 @@
<div></div>

View 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>