model = $objectManager->get(Reader::class); $this->customerRepo = $objectManager->get(CustomerRepositoryInterface::class); $this->userModel = $objectManager->create(UserModel::class); $this->paramsFactory = $objectManager->get(UserTokenParametersFactory::class); $this->issuer = $objectManager->get(Issuer::class); } /** * Verify that a token can be accepted for a customer. * * @return void * @throws \Throwable * @magentoDataFixture Magento/Customer/_files/customer.php */ public function testReadingCustomer(): void { //Preparing the token $customer = $this->customerRepo->get('customer@example.com'); /** @var UserTokenParameters $params */ $params = $this->paramsFactory->create( ['issued' => $issued = (new \DateTimeImmutable())->sub(new \DateInterval('PT1H'))] ); $token = $this->issuer->create( new CustomUserContext((int) $customer->getId(), UserContextInterface::USER_TYPE_CUSTOMER), $params ); $data = $this->model->read($token); $this->assertEquals(UserContextInterface::USER_TYPE_CUSTOMER, $data->getUserContext()->getUserType()); $this->assertEquals((int) $customer->getId(), $data->getUserContext()->getUserId()); $this->assertEquals($issued->format('Y-m-d H:i:s'), $data->getData()->getIssued()->format('Y-m-d H:i:s')); $this->assertGreaterThan($issued, $data->getData()->getExpires()); } /** * Verify that a token can be accepted for an admin user. * * @return void * @throws \Throwable * @magentoDataFixture Magento/User/_files/user_with_role.php */ public function testRadingAdmin(): void { //Preparing the token $admin = $this->userModel->loadByUsername('adminUser'); /** @var UserTokenParameters $params */ $params = $this->paramsFactory->create(); $token = $this->issuer->create( new CustomUserContext((int) $admin->getId(), UserContextInterface::USER_TYPE_ADMIN), $params ); $data = $this->model->read($token); $this->assertEquals(UserContextInterface::USER_TYPE_ADMIN, $data->getUserContext()->getUserType()); $this->assertEquals((int) $admin->getId(), $data->getUserContext()->getUserId()); $this->assertGreaterThan($data->getData()->getIssued(), $data->getData()->getExpires()); } }