Completed
Push — master ( ef9fce...66659a )
by
unknown
14s
created

SitemapTest::runAssertions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Victoire\Bundle\SitemapBundle\Tests;
4
5
use Symfony\Component\DomCrawler\Crawler;
6
use Victoire\Bundle\PageBundle\Entity\BasePage;
7
use Victoire\Bundle\PageBundle\Entity\Page;
8
use Victoire\Bundle\SeoBundle\Entity\PageSeo;
9
use Victoire\Tests\Functional\VictoireWebTestCase;
10
11
class SitemapTest extends VictoireWebTestCase
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
12
{
13
    protected function setUp()
14
    {
15
        parent::setUp();
16
        $this->resetSchema();
17
        $this->loadFixtures();
18
    }
19
20
    public function testPageIndexation()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
21
    {
22
        /* @var BasePage $homepage */
23
        $homepage = $this->entityManager->getRepository('VictoirePageBundle:BasePage')->findOneByHomepage('en');
0 ignored issues
show
Bug introduced by
The method findOneByHomepage() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findOneBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
24
25
        //English first
26
        $pageSeo = new PageSeo();
27
        $pageSeo->setCurrentLocale('en');
28
        $pageSeo->setSitemapIndexed(false);
29
        $pageSeo->setSitemapPriority(0.8);
30
        $this->entityManager->persist($pageSeo);
31
32
        $page = new Page();
33
        $page->setCurrentLocale('en');
34
        $page->setName('Indexed page');
35
        $page->setSeo($pageSeo);
36
        $page->setParent($homepage);
37
        $this->entityManager->persist($page);
38
39
        //French
40
        $page->setCurrentLocale('fr');
41
        $page->setName('Page indexée');
42
        $pageSeo->setCurrentLocale('fr');
43
        $pageSeo->setSitemapIndexed(true);
44
        $pageSeo->setSitemapPriority(0.3);
45
46
        $this->entityManager->flush();
47
        $this->resetViewsReference();
48
49
        $englishAssertions = [
50
            [
51
                'loc'        => '/en/',
52
                'changefreq' => 'monthly',
53
                'priority'   => '0.5',
54
            ],
55
            [
56
                'loc'        => '/en/english-test',
57
                'changefreq' => 'monthly',
58
                'priority'   => '0.5',
59
            ],
60
        ];
61
62
        $frenchAssertions = [
63
            [
64
                'loc'        => '/fr/',
65
                'changefreq' => 'monthly',
66
                'priority'   => '0.5',
67
            ],
68
            [
69
                'loc'        => '/fr/test',
70
                'changefreq' => 'monthly',
71
                'priority'   => '0.5',
72
            ],
73
            [
74
                'loc'        => '/fr/page-indexee',
75
                'changefreq' => 'monthly',
76
                'priority'   => '0.3',
77
            ],
78
        ];
79
80
        $englishPages = $this->getSitemapPages('en');
81
        $this->runAssertions($englishPages, $englishAssertions);
82
83
        $frenchPages = $this->getSitemapPages('fr');
84
        $this->runAssertions($frenchPages, $frenchAssertions);
85
    }
86
87
    /**
88
     * @return Crawler
89
     */
90
    private function getSitemapPages($locale = 'en')
91
    {
92
        $client = $this->createClient();
93
        $this->logIn($client, ['ROLE_ADMIN']);
94
        $client->request('GET', '/'.$locale.'/sitemap.xml');
95
96
        return $client->getCrawler()->filter('urlset > url');
97
    }
98
99
    /**
100
     * @param Crawler $pages
101
     * @param array   $assertions
102
     */
103
    private function runAssertions(Crawler $pages, array $assertions)
104
    {
105
        foreach ($assertions as $i => $v) {
106
            $this->assertStringEndsWith($assertions[$i]['loc'], $pages->eq($i)->filter('loc')->text());
107
            $this->assertEquals($assertions[$i]['changefreq'], $pages->eq($i)->filter('changefreq')->text());
108
            $this->assertEquals($assertions[$i]['priority'], $pages->eq($i)->filter('priority')->text());
109
        }
110
    }
111
}
112