Completed
Pull Request — master (#25)
by
unknown
14:43
created

AzineEmailControllerTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
B testAdminEmailsDashboardAction() 0 33 1
B loginUserIfRequired() 0 34 4
A getRouter() 0 4 1
A getContainer() 0 8 2
A getEntityManager() 0 4 1
A checkApplication() 0 10 2
1
<?php
2
namespace Azine\EmailBundle\Tests\Controller;
3
use Azine\PlatformBundle\Services\EmailTemplateProvider;
4
use Symfony\Component\DependencyInjection\ContainerInterface;
5
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
6
use Symfony\Component\HttpFoundation\ParameterBag;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpFoundation\Session\Session;
10
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
11
use Symfony\Component\Routing\RequestContext;
12
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
13
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
14
use Symfony\Component\DomCrawler\Crawler;
15
use Symfony\Bundle\FrameworkBundle\Client;
16
17
class AzineEmailControllerTest extends WebTestCase
18
{
19
    public function testAdminEmailsDashboardAction()
20
    {
21
        $this->checkApplication();
22
23
        // Create a new client to browse the application
24
        $client = static::createClient();
25
        $client->followRedirects();
26
27
        $manager = $this->getEntityManager();
28
        $sentEmailReop = $manager->getRepository("Azine\EmailBundle\Entity\SentEmail");
29
30
        $sentEmails = $sentEmailReop->findAll();
31
32
        $listUrl = substr($this->getRouter()->generate("admin_emails_dashboard", array('_locale' => "en")), 13);
33
        $crawler = $this->loginUserIfRequired($client, $listUrl);
34
35
        $count = count($sentEmails);
36
        $this->assertEquals($count, $crawler->filter(".sentEmail")->count(), "emailsDashboard expected with .".$count." sent emails");
37
        $link = $crawler->filter("tr:contains('".EmailTemplateProvider::NEWSLETTER_TEMPLATE."')")->first()->filter("td")->last()->filter("a")->first()->link();
38
39
        $crawler = $client->click($link);
40
41
        $this->assertEquals(1, $crawler->filter("span:contains('_az.email.hello')")->count(), " div with hello message expected.");
42
43
        $crawler = $this->loginUserIfRequired($client, $listUrl);
44
45
        //click on an email details view link to get to the details page
46
        $link = $crawler->filter("tr:contains('".EmailTemplateProvider::NEWSLETTER_TEMPLATE."')")->first()->filter("td")->last()->filter("a")->last()->link();
47
        $crawler = $client->click($link);
48
49
        $this->assertEquals(1, $crawler->filter("tr:contains('[email protected]')")->count(),"Table cell with email expected");
50
51
    }
52
53
54
    /**
55
     * Load the url and login if required.
56
     * @param  string  $url
57
     * @param  string  $username
58
     * @param  Client  $client
59
     * @return Crawler $crawler of the page of the url or the page after the login
60
     */
61
    private function loginUserIfRequired(Client $client, $url, $username = "dominik", $password = "lkjlkjlkjlkj")
62
    {
63
        // try to get the url
64
        $client->followRedirects();
65
        $crawler = $client->request("GET", $url);
66
67
        $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Status-Code 200 expected.");
68
69
        // if redirected to a login-page, login as admin-user
70
        if ($crawler->filter("input")->count() == 5 && $crawler->filter("#username")->count() == 1 && $crawler->filter("#password")->count() == 1 ) {
71
72
            // set the password of the admin
73
            $userProvider = $this->getContainer()->get('fos_user.user_provider.username_email');
74
            $user = $userProvider->loadUserByUsername($username);
75
            $user->setPlainPassword($password);
76
            $user->addRole("ROLE_ADMIN");
77
78
            $userManager = $this->getContainer()->get('fos_user.user_manager');
79
            $userManager->updateUser($user);
80
81
            $crawler = $crawler->filter("input[type='submit']");
82
            $form = $crawler->form();
83
            $form->get('_username')->setValue($username);
84
            $form->get('_password')->setValue($password);
85
            $crawler = $client->submit($form);
86
        }
87
88
        $this->assertEquals(200, $client->getResponse()->getStatusCode(),"Login failed.");
89
        $client->followRedirects(false);
90
91
        $this->assertStringEndsWith($url, $client->getRequest()->getUri(), "Login failed or not redirected to requested url: $url vs. ".$client->getRequest()->getUri());
92
93
        return $crawler;
94
    }
95
96
    /**
97
     * @var ContainerInterface
98
     */
99
    private $container;
100
101
    /**
102
     * @return UrlGeneratorInterface
103
     */
104
    private function getRouter()
105
    {
106
        return $this->getContainer()->get('router');
107
    }
108
109
    /**
110
     * Get the current container
111
     * @return \Symfony\Component\DependencyInjection\ContainerInterface
112
     */
113
    private function getContainer()
114
    {
115
        if ($this->container == null) {
116
            $this->container = static::$kernel->getContainer();
117
        }
118
119
        return $this->container;
120
    }
121
122
    /**
123
     * @return EntityManager
124
     */
125
    private function getEntityManager()
126
    {
127
        return $this->getContainer()->get('doctrine.orm.entity_manager');
128
    }
129
130
    /**
131
     * Check if the current setup is a full application.
132
     * If not, mark the test as skipped else continue.
133
     */
134
    private function checkApplication()
135
    {
136
        try {
137
            static::$kernel = static::createKernel(array());
138
        } catch (\RuntimeException $ex) {
139
            $this->markTestSkipped("There does not seem to be a full application available (e.g. running tests on travis.org). So this test is skipped.");
140
141
            return;
142
        }
143
    }
144
145
}
146