Completed
Push — master ( 964662...c81fc8 )
by Tomáš
05:04
created

BasicTestCase::jsonRequest()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 1
nop 4
crap 6
1
<?php
2
3
namespace Webcook\Cms\CoreBundle\Tests;
4
5
use Liip\FunctionalTestBundle\Test\WebTestCase;
6
use Symfony\Component\BrowserKit\Cookie;
7
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
8
use Webcook\Cms\SecurityBundle\Entity\User;
9
use Symfony\Component\Filesystem\Filesystem;
10
11
abstract class BasicTestCase extends WebTestCase
12
{
13
    /**
14
     * @var \Doctrine\ORM\EntityManager
15
     */
16
    protected $em;
17
18
    protected $client;
19
20
    protected $container;
21
22 20
    protected function loadFixtures(array $classNames, $omName = null, $registryName = 'doctrine', $purgeMode = null)
23
    {
24 20
        $classNames[] = 'Webcook\Cms\SecurityBundle\DataFixtures\ORM\LoadResourcesData';
25 20
        $classNames[] = 'Webcook\Cms\SecurityBundle\DataFixtures\ORM\LoadRoleData';
26 20
        $classNames[] = 'Webcook\Cms\SecurityBundle\DataFixtures\ORM\LoadUserData';
27 20
        $classNames[] = 'Webcook\Cms\CoreBundle\DataFixtures\ORM\LoadContentProviderData';
28 20
        $classNames[] = 'Webcook\Cms\CoreBundle\DataFixtures\ORM\LoadSectionData';
29 20
        $classNames[] = 'Webcook\Cms\I18nBundle\DataFixtures\ORM\LoadLanguageData';
30 20
        $classNames[] = 'Webcook\Cms\CoreBundle\DataFixtures\ORM\LoadPageData';
31
32 20
        parent::loadFixtures($classNames, $omName, $registryName, $purgeMode);
33 20
    }
34
35
    public function clearCache()
36
    {
37
        echo 'clear cache';
38
        $fs = new Filesystem();
39
        $fs->remove($this->container->getParameter('kernel.cache_dir'));
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 20
    public function setUp()
46
    {
47 20
        self::bootKernel();
48
        
49 20
        $this->container = static::$kernel->getContainer();
50 20
        $this->em = static::$kernel->getContainer()
51 20
            ->get('doctrine')
52 20
            ->getManager();
53
54 20
        $this->loadFixtures(array());
55 20
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60 20
    public function tearDown()
61
    {
62 20
        parent::tearDown();
63 20
    }
64
65 19
    protected function createTestClient($login = true)
66
    {
67 19
        $this->client = static::createClient();
68
69 19
        if ($login) {
70 19
            $this->logIn();
71
        }
72 19
    }
73
74
    protected function printResponseContent()
75
    {
76
        print_r($this->client->getResponse()->getContent());
77
    }
78
79
    protected function jsonRequest($method, $url, $data = array(), $addPostfix = true)
80
    {
81
        $this->createTestClient();
82
83
        $crawler = $this->client->request(
84
            $method,
85
            $url . ($addPostfix ? '.json' : ''),
86
            array(),
87
            array(),
88
            array(
89
                'CONTENT_TYPE' => 'application/json'
90
            ),
91
            json_encode($data)
92
        );
93
94
        return $crawler;
95
    }
96
97 19
    protected function logIn()
98
    {
99 19
        $session = $this->client->getContainer()->get('session');
100
101 19
        $user = $this->em->getRepository('Webcook\Cms\SecurityBundle\Entity\User')->find(1);
102 19
        $this->em->detach($user);
103
104 19
        $firewall = 'secured_area';
105 19
        $token = new UsernamePasswordToken($user, 'test', $firewall);
106 19
        $session->set('_security_'.$firewall, serialize($token));
107 19
        $session->save();
108
109 19
        $cookie = new Cookie($session->getName(), $session->getId());
110 19
        $this->client->getCookieJar()->set($cookie);
111 19
    }
112
}
113