Skip to content

Commit

Permalink
Merge pull request #1079 from spiral/feature/scoped-pagination
Browse files Browse the repository at this point in the history
Added `PaginationProviderInterface` binding in scope
  • Loading branch information
msmakouz committed Feb 16, 2024
2 parents 287abf0 + 3a2f629 commit d9281b7
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Core/src/Config/DeprecationProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getInterface(): string
$message = $this->message ?? \sprintf(
'Using `%s` outside of the `%s` scope is deprecated and will be impossible in version %s.',
$this->interface,
$this->scope,
$this->scope instanceof \BackedEnum ? $this->scope->value : $this->scope,
$this->version
);

Expand Down
30 changes: 30 additions & 0 deletions src/Core/tests/Internal/Proxy/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Spiral\Core\Container;
use Spiral\Core\Exception\Container\ContainerException;
use Spiral\Core\Scope;
use Spiral\Tests\Core\Fixtures\ScopeEnum;
use Spiral\Tests\Core\Internal\Proxy\Stub\EmptyInterface;
use Spiral\Tests\Core\Internal\Proxy\Stub\MockInterface;
use Spiral\Tests\Core\Internal\Proxy\Stub\MockInterfaceImpl;
Expand Down Expand Up @@ -281,6 +282,35 @@ interface: $interface,
\restore_error_handler();
}

#[DataProvider('interfacesProvider')]
#[WithoutErrorHandler]
public function testDeprecationProxyConfigWithEnumScope(string $interface): void
{
\set_error_handler(static function (int $errno, string $error) use ($interface): void {
self::assertSame(
\sprintf('Using `%s` outside of the `a` scope is deprecated and will be ' .
'impossible in version 4.0.', $interface),
$error
);
});

$root = new Container();
$root->getBinder('foo')->bindSingleton($interface, Stub\MockInterfaceImpl::class);
$root->bindSingleton($interface, new Config\DeprecationProxy($interface, true, ScopeEnum::A, '4.0'));

$proxy = $root->get($interface);
$this->assertInstanceOf($interface, $proxy);

$root->runScope(new Scope('foo'), static function () use ($proxy) {
$proxy->bar(name: 'foo'); // Possible to run
self::assertSame('foo', $proxy->baz('foo', 42));
self::assertSame(123, $proxy->qux(age: 123));
self::assertSame(69, $proxy->space(test age: 69));
});

\restore_error_handler();
}

#[DataProvider('interfacesProvider')]
#[WithoutErrorHandler]
public function testDeprecationProxyConfigDontThrowIfNotConstructed(string $interface): void
Expand Down
33 changes: 27 additions & 6 deletions src/Framework/Bootloader/Http/PaginationBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,37 @@
namespace Spiral\Bootloader\Http;

use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Core\BinderInterface;
use Spiral\Core\Config\DeprecationProxy;
use Spiral\Framework\Spiral;
use Spiral\Http\PaginationFactory;
use Spiral\Pagination\PaginationProviderInterface;

final class PaginationBootloader extends Bootloader
{
protected const DEPENDENCIES = [
HttpBootloader::class,
];
public function __construct(
private readonly BinderInterface $binder,
) {
}

protected const SINGLETONS = [
PaginationProviderInterface::class => PaginationFactory::class,
];
public function defineDependencies(): array
{
return [
HttpBootloader::class,
];
}

public function defineSingletons(): array
{
$this->binder
->getBinder(Spiral::HttpRequest)
->bindSingleton(PaginationProviderInterface::class, PaginationFactory::class);

$this->binder->bind(
PaginationProviderInterface::class,
new DeprecationProxy(PaginationProviderInterface::class, true, Spiral::HttpRequest, '4.0')
);

return [];
}
}
3 changes: 3 additions & 0 deletions src/Framework/Http/PaginationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Spiral\Core\Attribute\Scope;
use Spiral\Core\Exception\ScopeException;
use Spiral\Core\FactoryInterface;
use Spiral\Framework\Spiral;
use Spiral\Pagination\PaginationProviderInterface;
use Spiral\Pagination\Paginator;
use Spiral\Pagination\PaginatorInterface;

/**
* Paginators factory binded to active request scope in order to select page number.
*/
#[Scope(Spiral::HttpRequest)]
final class PaginationFactory implements PaginationProviderInterface
{
public function __construct(
Expand Down
19 changes: 19 additions & 0 deletions tests/Framework/Bootloader/Http/PaginationBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,33 @@

namespace Framework\Bootloader\Http;

use PHPUnit\Framework\Attributes\WithoutErrorHandler;
use Spiral\Framework\Spiral;
use Spiral\Http\PaginationFactory;
use Spiral\Pagination\PaginationProviderInterface;
use Spiral\Testing\Attribute\TestScope;
use Spiral\Tests\Framework\BaseTestCase;

final class PaginationBootloaderTest extends BaseTestCase
{
#[TestScope(Spiral::HttpRequest)]
public function testPaginationProviderInterfaceBinding(): void
{
$this->assertContainerBoundAsSingleton(PaginationProviderInterface::class, PaginationFactory::class);
}

#[WithoutErrorHandler]
public function testPaginationProviderInterfaceBindingInRootScope(): void
{
\set_error_handler(static function (int $errno, string $error): void {
self::assertSame(\sprintf(
'Using `%s` outside of the `http.request` scope is deprecated and will be impossible in version 4.0.',
PaginationProviderInterface::class
), $error);
});

$this->assertContainerBoundAsSingleton(PaginationProviderInterface::class, PaginationProviderInterface::class);

\restore_error_handler();
}
}
1 change: 1 addition & 0 deletions tests/Framework/Http/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function testPaginate(): void
$this->fakeHttp()->get('/paginate')->assertBodySame('1');
}

#[TestScope(Spiral::HttpRequest)]
public function testPaginateError(): void
{
$this->expectException(ScopeException::class);
Expand Down
4 changes: 2 additions & 2 deletions tests/app/src/Controller/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Spiral\App\Request\BadRequest;
use Spiral\App\Request\TestRequest;
use Spiral\Filter\InputScope;
use Spiral\Http\PaginationFactory;
use Spiral\Pagination\PaginationProviderInterface;
use Spiral\Pagination\Paginator;
use Spiral\Router\RouteInterface;
use Spiral\Translator\Traits\TranslatorTrait;
Expand All @@ -22,7 +22,7 @@ public function index(string $name = 'Dave')
return "Hello, {$name}.";
}

public function paginate(PaginationFactory $paginationFactory)
public function paginate(PaginationProviderInterface $paginationFactory): int
{
/** @var Paginator $p */
$p = $paginationFactory->createPaginator('page');
Expand Down

0 comments on commit d9281b7

Please sign in to comment.