CategoryControllerTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testNoCategoryWithSlug() 0 6 1
B testSingleCategory() 0 27 1
A testTree() 0 50 1
A testWithPages() 0 51 1
1
<?php
2
3
/*
4
* This file is part of the OrbitaleCmsBundle package.
5
*
6
* (c) Alexandre Rock Ancelet <[email protected]>
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
namespace Orbitale\Bundle\CmsBundle\Tests\Controller;
13
14
use Doctrine\ORM\EntityManager;
15
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\AbstractTestCase;
16
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\TestBundle\Entity\Category;
17
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\TestBundle\Entity\Page;
18
19
class CategoryControllerTest extends AbstractTestCase
20
{
21
    public function testNoCategoryWithSlug()
22
    {
23
        $client = static::createClient();
24
        $client->request('GET', '/category/inexistent-slug');
25
        static::assertEquals(404, $client->getResponse()->getStatusCode());
26
    }
27
28
    public function testSingleCategory()
29
    {
30
        $client = static::createClient();
31
32
        $category = new Category();
33
        $category
34
            ->setSlug('default')
35
            ->setName('Default Category')
36
            ->setDescription('Hello world!')
37
            ->setEnabled(true)
38
        ;
39
40
        /** @var EntityManager $em */
41
        $em = $client->getKernel()->getContainer()->get('doctrine')->getManager();
42
        $em->persist($category);
43
        $em->flush();
44
45
        // Repeat with the homepage directly in the url
46
        // First, check that any right trimming "/" will redirect
47
        $client->request('GET', '/category/default/');
48
        static::assertTrue($client->getResponse()->isRedirect('/category/default'));
49
50
        $crawler = $client->followRedirect();
51
        static::assertEquals($category->getName(), trim($crawler->filter('title')->html()));
52
        static::assertEquals($category->getName(), trim($crawler->filter('article > h1')->html()));
53
        static::assertContains($category->getDescription(), trim($crawler->filter('article')->first()->html()));
54
    }
55
56
    public function testTree()
57
    {
58
        $client = static::createClient();
59
60
        /** @var EntityManager $em */
61
        $em = $client->getKernel()->getContainer()->get('doctrine')->getManager();
62
63
        // Prepare 3 pages : the root, the first level, and the third one that's disabled
64
        $parent = new Category();
65
        $parent
66
            ->setSlug('default')
67
            ->setName('Default Category')
68
            ->setDescription('Hello world!')
69
            ->setEnabled(true)
70
        ;
71
        $em->persist($parent);
72
        $em->flush();
73
74
        $childOne = new Category();
75
        $childOne
76
            ->setEnabled(true)
77
            ->setSlug('first-level')
78
            ->setName('First level')
79
            ->setDescription('This level is the first one')
80
            ->setParent($parent)
81
        ;
82
        $em->persist($childOne);
83
        $em->flush();
84
85
        $childTwoDisabled = new Category();
86
        $childTwoDisabled
87
            ->setEnabled(false)
88
            ->setSlug('second-level')
89
            ->setName('Disabled category')
90
            ->setDescription('This category should render a 404 error')
91
            ->setParent($parent)
92
        ;
93
        $em->persist($childTwoDisabled);
94
        $em->flush();
95
96
        // Repeat with the homepage directly in the url
97
        $crawler = $client->request('GET', '/category/'.$childOne->getTree());
98
        static::assertEquals($childOne->getName(), trim($crawler->filter('title')->html()));
99
        static::assertEquals($childOne->getName(), trim($crawler->filter('article > h1')->html()));
100
        static::assertContains($childOne->getDescription(), trim($crawler->filter('article')->first()->html()));
101
102
        // Repeat with the homepage directly in the url
103
        $client->request('GET', '/category/root/second-level');
104
        static::assertEquals(404, $client->getResponse()->getStatusCode());
105
    }
106
107
    public function testWithPages()
108
    {
109
        $client = static::createClient();
110
111
        $category = new Category();
112
        $category
113
            ->setSlug('default')
114
            ->setName('Default Category')
115
            ->setDescription('Hello world!')
116
            ->setEnabled(true)
117
        ;
118
119
        /** @var EntityManager $em */
120
        $em = $client->getKernel()->getContainer()->get('doctrine')->getManager();
121
        $em->persist($category);
122
        $em->flush();
123
124
        $page1 = new Page();
125
        $page1
126
            ->setEnabled(true)
127
            ->setSlug('home')
128
            ->setTitle('My homepage')
129
            ->setHost('localhost')
130
            ->setContent('Hello world!')
131
            ->setCategory($category)
132
        ;
133
134
        $page2 = new Page();
135
        $page2
136
            ->setEnabled(true)
137
            ->setSlug('about')
138
            ->setTitle('About page')
139
            ->setHost('localhost')
140
            ->setContent('We.are.the.robots.')
141
            ->setCategory($category)
142
        ;
143
144
        $em->persist($page1);
145
        $em->persist($page2);
146
        $em->flush();
147
148
        $crawler = $client->request('GET', '/category/'.$category->getTree());
149
150
        $section1 = $crawler->filter('section')->eq(0);
151
        static::assertEquals($page1->getTitle(), trim($section1->filter('article > h2 > a')->html()));
152
        static::assertContains($page1->getContent(), trim($section1->filter('article')->html()));
153
154
        $section2 = $crawler->filter('section')->eq(1);
155
        static::assertEquals($page2->getTitle(), trim($section2->filter('article > h2 > a')->html()));
156
        static::assertContains($page2->getContent(), trim($section2->filter('article')->html()));
157
    }
158
}
159