AzineEmailControllerTest::getRouter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Azine\EmailBundle\Tests\Controller;
4
5
use Azine\EmailBundle\Entity\Repositories\SentEmailRepository;
6
use Azine\EmailBundle\Services\AzineTemplateProvider;
7
use Azine\EmailBundle\Tests\TestHelper;
8
use Symfony\Bundle\FrameworkBundle\Client;
9
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Symfony\Component\DomCrawler\Crawler;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\DomCrawler\Crawler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13
14
class AzineEmailControllerTest extends WebTestCase
15
{
16
    public function testAdminEmailsDashboardAction()
17
    {
18
        $this->checkApplication();
19
20
        // Create a new client to browse the application
21
        $client = static::createClient();
22
        $client->followRedirects();
23
24
        $manager = $this->getEntityManager();
25
        /** @var SentEmailRepository $sentEmailRep */
26
        $sentEmailRep = $manager->getRepository("Azine\EmailBundle\Entity\SentEmail");
27
28
        $testSentEmails = $sentEmailRep->search(array('recipients' => TestHelper::TEST_EMAIL, 'template' => AzineTemplateProvider::NEWSLETTER_TEMPLATE))->getArrayResult();
29
        $pageLimit = 10;
30
31
        if (count($testSentEmails) < $pageLimit) {
32
            $amountToAdd = ($pageLimit * 2) + 2;
33
            TestHelper::addSentEmails($manager, $amountToAdd);
34
        }
35
36
        $testSentEmails = $sentEmailRep->search(array('template' => AzineTemplateProvider::NEWSLETTER_TEMPLATE))->getArrayResult();
37
38
        $listUrl = substr($this->getRouter()->generate('azine_admin_email_dashboard', array('_locale' => 'en', 'limit' => $pageLimit, 'sentEmail[template]' => AzineTemplateProvider::NEWSLETTER_TEMPLATE)), 13);
39
        $crawler = $this->loginUserIfRequired($client, $listUrl);
40
41
        $this->assertSame($pageLimit, $crawler->filter('.sentEmail')->count(), 'emailsDashboard expected with '.$pageLimit.' sent emails');
42
43
        $numberOfPaginationLinks = intval(ceil(count($testSentEmails) / $pageLimit));
44
45
        $this->assertSame($numberOfPaginationLinks, $crawler->filter('.pagination .page')->count() + $crawler->filter('.pagination .current')->count(), $numberOfPaginationLinks.' pagination links expected');
46
47
        //click on an email web view link to get to the web page
48
        $link = $crawler->filter(".sentEmail:contains('".AzineTemplateProvider::NEWSLETTER_TEMPLATE."')")->filter('a.showWebViewButton')->link();
49
        $crawler = $client->click($link);
50
51
        $this->assertSame(1, $crawler->filter("span:contains('Hello')")->count(), ' div with hello message expected.');
52
53
        $crawler = $client->request('GET', $listUrl);
54
55
        // Test filtering by email
56
        $crawler = $crawler->selectButton('sentEmail[filter]');
57
        $form = $crawler->form();
58
        $form['sentEmail[recipients]'] = TestHelper::TEST_EMAIL;
59
        $crawler = $client->submit($form);
60
61
        $this->assertSame($crawler->filter('.sentEmail')->count(), $crawler->filter("tr:contains('".TestHelper::TEST_EMAIL."')")->count(), 'Table rows only with '.TestHelper::TEST_EMAIL.' email are expected');
62
63
        //click on email details view link to get to the details page
64
        $link = $crawler->filter(".sentEmail:contains('".TestHelper::TEST_EMAIL."')")->filter('a.showDetailsButton')->link();
65
        $crawler = $client->click($link);
66
67
        $this->assertSame(1, $crawler->filter("tr:contains('".TestHelper::TEST_EMAIL."')")->count(), 'Table cell with '.TestHelper::TEST_EMAIL.' expected');
68
69
        $client->request('GET', $listUrl);
70
71
        $form['sentEmail[recipients]'] = '';
72
        $client->submit($form);
73
74
        //Test filtering by token
75
        $form['sentEmail[token]'] = TestHelper::TEST_TOKEN;
76
        $crawler = $client->submit($form);
77
78
        $this->assertSame($crawler->filter('.sentEmail')->count(), $crawler->filter(".sentEmail:contains('".TestHelper::TEST_TOKEN."')")->count(), 'Table row only with '.TestHelper::TEST_TOKEN.' token is expected');
79
    }
80
81
    /**
82
     * Load the url and login if required.
83
     *
84
     * @param string $url
85
     * @param string $username
86
     * @param Client $client
87
     *
88
     * @return Crawler $crawler of the page of the url or the page after the login
89
     */
90
    private function loginUserIfRequired(Client $client, $url, $username = 'dominik', $password = 'lkjlkjlkjlkj')
91
    {
92
        // try to get the url
93
        $client->followRedirects();
94
        $crawler = $client->request('GET', $url);
95
96
        $this->assertSame(200, $client->getResponse()->getStatusCode(), 'Status-Code 200 expected.');
97
98
        // if redirected to a login-page, login as admin-user
99
        if (5 == $crawler->filter('input')->count() && 1 == $crawler->filter('#username')->count() && 1 == $crawler->filter('#password')->count()) {
100
            // set the password of the admin
101
            $userProvider = $this->getContainer()->get('fos_user.user_provider.username_email');
102
            $user = $userProvider->loadUserByUsername($username);
103
            $user->setPlainPassword($password);
104
            $user->addRole('ROLE_ADMIN');
105
106
            $userManager = $this->getContainer()->get('fos_user.user_manager');
107
            $userManager->updateUser($user);
108
109
            $crawler = $crawler->filter("input[type='submit']");
110
            $form = $crawler->form();
111
            $form->get('_username')->setValue($username);
112
            $form->get('_password')->setValue($password);
113
            $crawler = $client->submit($form);
114
        }
115
116
        $this->assertSame(200, $client->getResponse()->getStatusCode(), 'Login failed.');
117
        $client->followRedirects(false);
118
119
        $this->assertStringEndsWith($url, $client->getRequest()->getUri(), "Login failed or not redirected to requested url: $url vs. ".$client->getRequest()->getUri());
120
121
        return $crawler;
122
    }
123
124
    /**
125
     * @var ContainerInterface
126
     */
127
    private $appContainer;
128
129
    /**
130
     * @return UrlGeneratorInterface
131
     */
132
    private function getRouter()
133
    {
134
        return $this->getContainer()->get('router');
135
    }
136
137
    /**
138
     * Get the current container.
139
     *
140
     * @return \Symfony\Component\DependencyInjection\ContainerInterface
141
     */
142
    private function getContainer()
143
    {
144
        if (null == $this->appContainer) {
145
            $this->appContainer = static::$kernel->getContainer();
146
        }
147
148
        return $this->appContainer;
149
    }
150
151
    /**
152
     * @return EntityManager
0 ignored issues
show
Bug introduced by
The type Azine\EmailBundle\Tests\Controller\EntityManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
153
     */
154
    private function getEntityManager()
155
    {
156
        return $this->getContainer()->get('doctrine.orm.entity_manager');
157
    }
158
159
    /**
160
     * Check if the current setup is a full application.
161
     * If not, mark the test as skipped else continue.
162
     */
163
    private function checkApplication()
164
    {
165
        try {
166
            static::$kernel = static::createKernel(array());
167
        } catch (\RuntimeException $ex) {
168
            $this->markTestSkipped('There does not seem to be a full application available (e.g. running tests on travis.org). So this test is skipped.');
169
170
            return;
171
        }
172
    }
173
}
174