Completed
Push — master ( ab6ee6...8da333 )
by Kamil
32:30 queued 09:03
created

AdminProductAjaxTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A it_denies_access_to_a_products_list_for_not_authenticated_user() 0 7 1
A it_allows_to_get_a_products_list() 0 17 1
A authenticateAdminUser() 0 15 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\Controller;
15
16
use ApiTestCase\JsonApiTestCase;
17
use Exception;
18
use Symfony\Component\BrowserKit\Cookie;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
21
22
final class AdminProductAjaxTest extends JsonApiTestCase
23
{
24
    /**
25
     * @test
26
     *
27
     * @throws Exception
28
     */
29
    public function it_denies_access_to_a_products_list_for_not_authenticated_user()
30
    {
31
        $this->client->request('GET', '/admin/ajax/products/');
32
33
        $response = $this->client->getResponse();
34
        $this->assertTrue($response->isRedirection());
35
    }
36
37
    /**
38
     * @test
39
     *
40
     * @throws Exception
41
     */
42
    public function it_allows_to_get_a_products_list()
43
    {
44
        $this->loadFixturesFromFile('authentication/administrator.yml');
45
        $this->loadFixturesFromFiles([
46
            'resources/product_association_types.yml',
47
            'resources/products.yml',
48
            'resources/many_products.yml',
49
            'resources/product_associations.yml',
50
        ]);
51
52
        $this->authenticateAdminUser();
53
54
        $this->client->request('GET', '/admin/ajax/products/');
55
56
        $response = $this->client->getResponse();
57
        $this->assertResponse($response, 'ajax/product/index_response', Response::HTTP_OK);
58
    }
59
60
    private function authenticateAdminUser(): void
61
    {
62
        $adminUserRepository = self::$container->get('sylius.repository.admin_user');
63
        $user = $adminUserRepository->findOneByEmail('[email protected]');
64
65
        $session = self::$container->get('session');
66
        $firewallName = 'admin';
67
        $firewallContext = 'admin';
68
        $token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
69
        $session->set(sprintf('_security_%s', $firewallContext), serialize($token));
70
        $session->save();
71
72
        $cookie = new Cookie($session->getName(), $session->getId());
73
        $this->client->getCookieJar()->set($cookie);
74
    }
75
}
76