Completed
Push — master ( bbd2b1...f08c3b )
by Kamil
31:47 queued 09:37
created

UpdatingUserPasswordEncoderTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 9
dl 0
loc 90
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 19 1
A it_updates_the_encoder_when_the_shop_user_logs_in() 0 26 1
A it_updates_the_encoder_when_the_admin_user_logs_in() 0 26 1
A submitForm() 0 8 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Tests\Functional;
15
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Fidry\AliceDataFixtures\LoaderInterface;
18
use Fidry\AliceDataFixtures\Persistence\PurgeMode;
19
use PHPUnit\Framework\Assert;
20
use Sylius\Component\User\Repository\UserRepositoryInterface;
21
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
22
use Symfony\Component\BrowserKit\Client;
23
24
final class UpdatingUserPasswordEncoderTest extends WebTestCase
25
{
26
    /** @var Client */
27
    private $client;
28
29
    protected function setUp(): void
30
    {
31
        $this->client = static::createClient();
32
        $this->client->followRedirects(true);
33
34
        /** @var LoaderInterface $fixtureLoader */
35
        $fixtureLoader = $this->client->getContainer()->get('fidry_alice_data_fixtures.loader.doctrine');
36
37
        $fixtureLoader->load(
38
            [
39
                __DIR__ . '/../DataFixtures/ORM/resources/channels.yml',
40
                __DIR__ . '/../DataFixtures/ORM/resources/customers.yml',
41
                __DIR__ . '/../DataFixtures/ORM/resources/admin_users.yml',
42
            ],
43
            [],
44
            [],
45
            PurgeMode::createDeleteMode()
46
        );
47
    }
48
49
    /** @test */
50
    public function it_updates_the_encoder_when_the_shop_user_logs_in(): void
51
    {
52
        /** @var UserRepositoryInterface $shopUserRepository */
53
        $shopUserRepository = $this->client->getContainer()->get('sylius.repository.shop_user');
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\BrowserKit\Client as the method getContainer() does only exist in the following sub-classes of Symfony\Component\BrowserKit\Client: Symfony\Bundle\FrameworkBundle\Client. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
54
55
        /** @var ObjectManager $shopUserManager */
56
        $shopUserManager = $this->client->getContainer()->get('sylius.manager.shop_user');
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\BrowserKit\Client as the method getContainer() does only exist in the following sub-classes of Symfony\Component\BrowserKit\Client: Symfony\Bundle\FrameworkBundle\Client. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
57
58
        $shopUser = $shopUserRepository->findOneByEmail('[email protected]');
59
        $shopUser->setPlainPassword('testpassword');
60
        $shopUser->setEncoderName('sha512');
61
62
        $shopUserManager->persist($shopUser);
63
        $shopUserManager->flush();
64
65
        $this->client->request('GET', '/en_US/login');
66
67
        $this->submitForm('Login', [
68
            '_username' => '[email protected]',
69
            '_password' => 'testpassword',
70
        ]);
71
72
        Assert::assertSame(200, $this->client->getResponse()->getStatusCode());
73
        Assert::assertSame('/en_US/', parse_url($this->client->getCrawler()->getUri(), \PHP_URL_PATH));
74
        Assert::assertSame('argon2i', $shopUserRepository->findOneByEmail('[email protected]')->getEncoderName());
75
    }
76
77
    /** @test */
78
    public function it_updates_the_encoder_when_the_admin_user_logs_in(): void
79
    {
80
        /** @var UserRepositoryInterface $adminUserRepository */
81
        $adminUserRepository = $this->client->getContainer()->get('sylius.repository.admin_user');
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\BrowserKit\Client as the method getContainer() does only exist in the following sub-classes of Symfony\Component\BrowserKit\Client: Symfony\Bundle\FrameworkBundle\Client. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
82
83
        /** @var ObjectManager $adminUserManager */
84
        $adminUserManager = $this->client->getContainer()->get('sylius.manager.admin_user');
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\BrowserKit\Client as the method getContainer() does only exist in the following sub-classes of Symfony\Component\BrowserKit\Client: Symfony\Bundle\FrameworkBundle\Client. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
85
86
        $adminUser = $adminUserRepository->findOneByEmail('[email protected]');
87
        $adminUser->setPlainPassword('testpassword');
88
        $adminUser->setEncoderName('sha512');
89
90
        $adminUserManager->persist($adminUser);
91
        $adminUserManager->flush();
92
93
        $this->client->request('GET', '/admin/login');
94
95
        $this->submitForm('Login', [
96
            '_username' => '[email protected]',
97
            '_password' => 'testpassword',
98
        ]);
99
100
        Assert::assertSame(200, $this->client->getResponse()->getStatusCode());
101
        Assert::assertSame('/admin/', parse_url($this->client->getCrawler()->getUri(), \PHP_URL_PATH));
102
        Assert::assertSame('argon2i', $adminUserRepository->findOneByEmail('[email protected]')->getEncoderName());
103
    }
104
105
    private function submitForm(string $button, array $fieldValues = []): void
106
    {
107
        $buttonNode = $this->client->getCrawler()->selectButton($button);
108
109
        $form = $buttonNode->form($fieldValues);
110
111
        $this->client->submit($form);
112
    }
113
}
114