Completed
Push — master ( 133170...772a04 )
by Dominik
04:23
created

testAdminEmailsDashboardAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 57
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 9.6818
c 0
b 0
f 0
cc 2
eloc 29
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Azine\EmailBundle\Tests\Controller;
3
use Azine\EmailBundle\DependencyInjection\AzineEmailExtension;
4
use Azine\EmailBundle\Services\AzineEmailTwigExtension;
5
use Azine\EmailBundle\Services\Pagination;
6
use Azine\EmailBundle\Tests\FindInFileUtil;
7
use Azine\EmailBundle\Services\AzineTemplateProvider;
8
use Azine\EmailBundle\Entity\SentEmail;
9
use Azine\EmailBundle\Controller\AzineEmailTemplateController;
10
use Azine\PlatformBundle\Services\EmailTemplateProvider;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
13
use Symfony\Component\HttpFoundation\ParameterBag;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpFoundation\Session\Session;
17
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
18
use Symfony\Component\Routing\RequestContext;
19
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
20
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
21
use Symfony\Component\DomCrawler\Crawler;
22
use Symfony\Bundle\FrameworkBundle\Client;
23
use Azine\EmailBundle\Tests\TestHelper;
24
use Doctrine\ORM\EntityManager;
25
26
class AzineEmailControllerTest extends WebTestCase
27
{
28
    public function testAdminEmailsDashboardAction()
29
    {
30
        $this->checkApplication();
31
32
        // Create a new client to browse the application
33
        $client = static::createClient();
34
        $client->followRedirects();
35
36
        $manager = $this->getEntityManager();
37
        $sentEmailRep = $manager->getRepository("Azine\EmailBundle\Entity\SentEmail");
38
39
        $testSentEmails = $sentEmailRep->search(['recipients' => TestHelper::TEST_EMAIL]);
40
41
42
        if (count($testSentEmails) == 0) {
43
44
            TestHelper::addSentEmails($manager);
45
        }
46
47
        $listUrl = substr($this->getRouter()->generate("azine_admin_email_dashboard", array('_locale' => "en")), 13);
48
        $crawler = $this->loginUserIfRequired($client, $listUrl);
49
50
        //click on an email web view link to get to the web page
51
        $link = $crawler->filter(".sentEmail:contains('".EmailTemplateProvider::NEWSLETTER_TEMPLATE."')")->first()->filter("td")->last()->filter("a")->first()->link();
52
        $crawler = $client->click($link);
53
54
        $this->assertEquals(1, $crawler->filter("span:contains('_az.email.hello')")->count(), " div with hello message expected.");
55
56
        $crawler = $this->loginUserIfRequired($client, $listUrl);
57
58
        //Test filtering by email
59
        $crawler = $crawler->selectButton('sentEmail[save]');
60
        $form = $crawler->form();
61
        $form['sentEmail[recipients]'] = TestHelper::TEST_EMAIL;
62
        $crawler = $client->submit($form);
63
64
        $this->assertEquals($crawler->filter(".sentEmail")->count(), $crawler->filter("tr:contains('".TestHelper::TEST_EMAIL."')")->count(),"Table rows only with ".TestHelper::TEST_EMAIL." email are expected");
65
66
67
        //click on email details view link to get to the details page
68
        $link = $crawler->filter(".sentEmail:contains('".TestHelper::TEST_EMAIL."')")->first()->filter("td")->last()->filter("a")->last()->link();
69
        $crawler = $client->click($link);
70
71
        $this->assertEquals(1, $crawler->filter("tr:contains('".TestHelper::TEST_EMAIL."')")->count(),"Table cell with ".TestHelper::TEST_EMAIL." expected");
72
73
        $crawler = $this->loginUserIfRequired($client, $listUrl);
0 ignored issues
show
Unused Code introduced by
$crawler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
74
75
        $form['sentEmail[recipients]'] = '';
76
        $crawler = $client->submit($form);
0 ignored issues
show
Unused Code introduced by
$crawler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
77
78
        //Test filtering by token
79
        $form['sentEmail[token]'] = TestHelper::TEST_TOKEN;
80
        $crawler = $client->submit($form);
81
82
        $this->assertEquals($crawler->filter(".sentEmail")->count(), $crawler->filter(".sentEmail:contains('".TestHelper::TEST_TOKEN."')")->count(),"Table row only with ".TestHelper::TEST_TOKEN." token is expected");
83
84
    }
85
86
    /**
87
     * Load the url and login if required.
88
     * @param  string  $url
89
     * @param  string  $username
90
     * @param  Client  $client
91
     * @return Crawler $crawler of the page of the url or the page after the login
92
     */
93
    private function loginUserIfRequired(Client $client, $url, $username = "dominik", $password = "lkjlkjlkjlkj")
94
    {
95
        // try to get the url
96
        $client->followRedirects();
97
        $crawler = $client->request("GET", $url);
98
99
        $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Status-Code 200 expected.");
100
101
        // if redirected to a login-page, login as admin-user
102
        if ($crawler->filter("input")->count() == 5 && $crawler->filter("#username")->count() == 1 && $crawler->filter("#password")->count() == 1 ) {
103
104
            // set the password of the admin
105
            $userProvider = $this->getContainer()->get('fos_user.user_provider.username_email');
106
            $user = $userProvider->loadUserByUsername($username);
107
            $user->setPlainPassword($password);
108
            $user->addRole("ROLE_ADMIN");
109
110
            $userManager = $this->getContainer()->get('fos_user.user_manager');
111
            $userManager->updateUser($user);
112
113
            $crawler = $crawler->filter("input[type='submit']");
114
            $form = $crawler->form();
115
            $form->get('_username')->setValue($username);
116
            $form->get('_password')->setValue($password);
117
            $crawler = $client->submit($form);
118
        }
119
120
        $this->assertEquals(200, $client->getResponse()->getStatusCode(),"Login failed.");
121
        $client->followRedirects(false);
122
123
        $this->assertStringEndsWith($url, $client->getRequest()->getUri(), "Login failed or not redirected to requested url: $url vs. ".$client->getRequest()->getUri());
124
125
        return $crawler;
126
    }
127
128
    /**
129
     * @var ContainerInterface
130
     */
131
    private $container;
132
133
    /**
134
     * @return UrlGeneratorInterface
135
     */
136
    private function getRouter()
137
    {
138
        return $this->getContainer()->get('router');
139
    }
140
141
    /**
142
     * Get the current container
143
     * @return \Symfony\Component\DependencyInjection\ContainerInterface
144
     */
145
    private function getContainer()
146
    {
147
        if ($this->container == null) {
148
            $this->container = static::$kernel->getContainer();
149
        }
150
151
        return $this->container;
152
    }
153
154
    /**
155
     * @return EntityManager
156
     */
157
    private function getEntityManager()
158
    {
159
        return $this->getContainer()->get('doctrine.orm.entity_manager');
160
    }
161
162
    /**
163
     * Check if the current setup is a full application.
164
     * If not, mark the test as skipped else continue.
165
     */
166
    private function checkApplication()
167
    {
168
        try {
169
            static::$kernel = static::createKernel(array());
170
        } catch (\RuntimeException $ex) {
171
            $this->markTestSkipped("There does not seem to be a full application available (e.g. running tests on travis.org). So this test is skipped.");
172
173
            return;
174
        }
175
    }
176
177
}
178