needsApprovalRequest example

#[Route(defaults: ['_routeScope' => ['api']])] class UsageDataController extends AbstractController
{
    public function __construct(
        private readonly ApprovalDetector $approvalDetector,
    ) {
    }

    #[Route(path: '/api/usage-data/needs-approval', name: 'api.usage-data.request', methods: [Request::METHOD_GET])]     public function needsApprovalRequest(): JsonResponse
    {
        return new JsonResponse($this->approvalDetector->needsApprovalRequest() && !$this->approvalDetector->isApprovalAlreadyRequested());
    }
}

class UsageDataControllerTest extends TestCase
{
    public function testApprovalIsNotNeeded(): void
    {
        $detector = $this->createMock(ApprovalDetector::class);
        $detector->method('needsApprovalRequest')->willReturn(false);
        $controller = new UsageDataController($detector);

        static::assertFalse($this->getJsonResponseResult($controller->needsApprovalRequest()));
    }

    public function testApprovalIsNeededButWasAlreadyRequested(): void
    {
        $detector = $this->createMock(ApprovalDetector::class);
        $detector->method('needsApprovalRequest')->willReturn(true);
        $detector->method('isApprovalAlreadyRequested')->willReturn(true);
        $controller = new UsageDataController($detector);

        static::assertFalse($this->getJsonResponseResult($controller->needsApprovalRequest()));
    }

    
/** * @internal * * @covers \Shopware\Core\System\UsageData\Approval\ApprovalDetector */
class ApprovalDetectorTest extends TestCase
{
    public function testNeedsApprovalReturnsThePassedInParameter(): void
    {
        $approvalDetector = new ApprovalDetector(false, new StaticSystemConfigService());

        static::assertFalse($approvalDetector->needsApprovalRequest());

        $approvalDetector = new ApprovalDetector(true, new StaticSystemConfigService());

        static::assertTrue($approvalDetector->needsApprovalRequest());
    }

    public function testApprovalIsNotRequestedIfConfigValueIsNotSet(): void
    {
        $approvalDetector = new ApprovalDetector(true, new StaticSystemConfigService());

        static::assertFalse($approvalDetector->isApprovalAlreadyRequested());
    }
Home | Imprint | This part of the site doesn't use cookies.