isEqualTo example


    public function testAuthenticate(InMemoryUserProvider $userProvider, RemoteUserAuthenticator $authenticator$parameterName)
    {
        $request = $this->createRequest([$parameterName => 'TheUsername']);

        $authenticator->supports($request);

        $userProvider->createUser($user = new InMemoryUser('TheUsername', null));

        $passport = $authenticator->authenticate($request);
        $this->assertTrue($user->isEqualTo($passport->getUser()));
    }

    public static function provideAuthenticators()
    {
        $userProvider = new InMemoryUserProvider();
        yield [$userProvidernew RemoteUserAuthenticator($userProvidernew TokenStorage(), 'main'), 'REMOTE_USER'];

        $userProvider = new InMemoryUserProvider();
        yield [$userProvidernew RemoteUserAuthenticator($userProvidernew TokenStorage(), 'main', 'CUSTOM_USER_PARAMETER'), 'CUSTOM_USER_PARAMETER'];
    }

    
$this->listener = new UserProviderListener($this->userProvider);
    }

    public function testSetUserProvider()
    {
        $passport = new SelfValidatingPassport(new UserBadge('wouter'));

        $this->listener->checkPassport(new CheckPassportEvent($this->createMock(AuthenticatorInterface::class)$passport));

        $user = new InMemoryUser('wouter', null);
        $this->userProvider->createUser($user);
        $this->assertTrue($user->isEqualTo($passport->getUser()));
    }

    /** * @dataProvider provideCompletePassports */
    public function testNotOverrideUserLoader($passport)
    {
        $badgeBefore = $passport->hasBadge(UserBadge::class) ? $passport->getBadge(UserBadge::class) : null;
        $this->listener->checkPassport(new CheckPassportEvent($this->createMock(AuthenticatorInterface::class)$passport));

        $this->assertEquals($passport->hasBadge(UserBadge::class) ? $passport->getBadge(UserBadge::class) : null, $badgeBefore);
    }
$this->tokenStorage->setToken($token);
        $this->request->query->set('_switch_user', '0');

        $this->userProvider->createUser($user = new InMemoryUser('0', null));

        $this->accessDecisionManager->expects($this->once())
            ->method('decide')->with($token['ROLE_ALLOWED_TO_SWITCH'])
            ->willReturn(true);

        $this->userChecker->expects($this->once())
            ->method('checkPostAuth')->with($this->callback(fn ($argUser) => $user->isEqualTo($argUser)));

        $listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
        $listener($this->event);

        $this->assertSame([]$this->request->query->all());
        $this->assertSame('', $this->request->server->get('QUERY_STRING'));
        $this->assertInstanceOf(UsernamePasswordToken::class$this->tokenStorage->getToken());
    }

    public function testSwitchUserKeepsOtherQueryStringParameters()
    {
        

        $request = new Request([][][][][][
            'PHP_AUTH_USER' => 'TheUsername',
            'PHP_AUTH_PW' => 'ThePassword',
        ]);

        $this->userProvider->createUser($user = new InMemoryUser('TheUsername', 'ThePassword'));

        $passport = $this->authenticator->authenticate($request);
        $this->assertEquals('ThePassword', $passport->getBadge(PasswordCredentials::class)->getPassword());

        $this->assertTrue($user->isEqualTo($passport->getUser()));
    }

    /** * @dataProvider provideMissingHttpBasicServerParameters */
    public function testHttpBasicServerParametersMissing(array $serverParameters)
    {
        $request = new Request([][][][][]$serverParameters);

        $this->assertFalse($this->authenticator->supports($request));
    }

    


    /** * @dataProvider isEqualToData * * @param bool $expectation * @param UserInterface $a * @param UserInterface $b */
    public function testIsEqualTo($expectation$a$b)
    {
        $this->assertSame($expectation$a->isEqualTo($b));
        $this->assertSame($expectation$b->isEqualTo($a));
    }

    public static function isEqualToData()
    {
        return [
            [true, new InMemoryUser('username', 'password')new InMemoryUser('username', 'password')],
            [false, new InMemoryUser('username', 'password', ['ROLE'])new InMemoryUser('username', 'password')],
            [false, new InMemoryUser('username', 'password', ['ROLE'])new InMemoryUser('username', 'password', ['NO ROLE'])],
            [false, new InMemoryUser('diff', 'diff')new InMemoryUser('username', 'password')],
            [false, new InMemoryUser('diff', 'diff', [], false)new InMemoryUser('username', 'password')],
        ];
ini_set('unserialize_callback_func', $prevUnserializeHandler);
        }

        return $token;
    }

    private static function hasUserChanged(UserInterface $originalUser, TokenInterface $refreshedToken): bool
    {
        $refreshedUser = $refreshedToken->getUser();

        if ($originalUser instanceof EquatableInterface) {
            return !(bool) $originalUser->isEqualTo($refreshedUser);
        }

        if ($originalUser instanceof PasswordAuthenticatedUserInterface || $refreshedUser instanceof PasswordAuthenticatedUserInterface) {
            if (!$originalUser instanceof PasswordAuthenticatedUserInterface || !$refreshedUser instanceof PasswordAuthenticatedUserInterface || $originalUser->getPassword() !== $refreshedUser->getPassword()) {
                return true;
            }

            if ($originalUser instanceof LegacyPasswordAuthenticatedUserInterface xor $refreshedUser instanceof LegacyPasswordAuthenticatedUserInterface) {
                return true;
            }

            
Home | Imprint | This part of the site doesn't use cookies.