Completed
Pull Request — master (#25)
by
unknown
13:10
created

AzineEmailControllerTest::getContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

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 4
nc 2
nop 0
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
            $testSentEmails = $sentEmailRep->search(['recipients' => TestHelper::TEST_EMAIL]);
0 ignored issues
show
Unused Code introduced by
$testSentEmails 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...
46
47
        }
48
49
        $listUrl = substr($this->getRouter()->generate("azine_admin_emails_dashboard", array('_locale' => "en")), 13);
50
        $crawler = $this->loginUserIfRequired($client, $listUrl);
51
52
        //click on an email web view link to get to the web page
53
        $link = $crawler->filter(".sentEmail:contains('".EmailTemplateProvider::NEWSLETTER_TEMPLATE."')")->first()->filter("td")->last()->filter("a")->first()->link();
54
        $crawler = $client->click($link);
55
56
        $this->assertEquals(1, $crawler->filter("span:contains('_az.email.hello')")->count(), " div with hello message expected.");
57
58
        $crawler = $this->loginUserIfRequired($client, $listUrl);
59
60
        //Test filtering by email
61
        $crawler = $crawler->selectButton('sentEmail[save]');
62
        $form = $crawler->form();
63
        $form['sentEmail[recipients]'] = TestHelper::TEST_EMAIL;
64
        $crawler = $client->submit($form);
65
66
        $this->assertEquals($crawler->filter(".sentEmail")->count(), $crawler->filter("tr:contains('".TestHelper::TEST_EMAIL."')")->count(),"Table rows only with ".TestHelper::TEST_EMAIL." email are expected");
67
68
69
        //click on an email details view link to get to the details page
70
        $link = $crawler->filter(".sentEmail:contains('".TestHelper::TEST_EMAIL."')")->first()->filter("td")->last()->filter("a")->last()->link();
71
        $crawler = $client->click($link);
72
73
        $this->assertEquals(1, $crawler->filter("tr:contains('".TestHelper::TEST_EMAIL."')")->count(),"Table cell with email expected");
74
75
        $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...
76
77
        $form['sentEmail[recipients]'] = '';
78
        $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...
79
80
        //Test filtering by token
81
        $form['sentEmail[token]'] = TestHelper::TEST_TOKEN;
82
        $crawler = $client->submit($form);
83
84
        $this->assertEquals($crawler->filter(".sentEmail")->count(), $crawler->filter(".sentEmail:contains('".TestHelper::TEST_TOKEN."')")->count(),"Table row only with ".TestHelper::TEST_TOKEN." token is expected");
85
86
    }
87
88
    /**
89
     * Load the url and login if required.
90
     * @param  string  $url
91
     * @param  string  $username
92
     * @param  Client  $client
93
     * @return Crawler $crawler of the page of the url or the page after the login
94
     */
95
    private function loginUserIfRequired(Client $client, $url, $username = "dominik", $password = "lkjlkjlkjlkj")
96
    {
97
        // try to get the url
98
        $client->followRedirects();
99
        $crawler = $client->request("GET", $url);
100
101
        $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Status-Code 200 expected.");
102
103
        // if redirected to a login-page, login as admin-user
104
        if ($crawler->filter("input")->count() == 5 && $crawler->filter("#username")->count() == 1 && $crawler->filter("#password")->count() == 1 ) {
105
106
            // set the password of the admin
107
            $userProvider = $this->getContainer()->get('fos_user.user_provider.username_email');
108
            $user = $userProvider->loadUserByUsername($username);
109
            $user->setPlainPassword($password);
110
            $user->addRole("ROLE_ADMIN");
111
112
            $userManager = $this->getContainer()->get('fos_user.user_manager');
113
            $userManager->updateUser($user);
114
115
            $crawler = $crawler->filter("input[type='submit']");
116
            $form = $crawler->form();
117
            $form->get('_username')->setValue($username);
118
            $form->get('_password')->setValue($password);
119
            $crawler = $client->submit($form);
120
        }
121
122
        $this->assertEquals(200, $client->getResponse()->getStatusCode(),"Login failed.");
123
        $client->followRedirects(false);
124
125
        $this->assertStringEndsWith($url, $client->getRequest()->getUri(), "Login failed or not redirected to requested url: $url vs. ".$client->getRequest()->getUri());
126
127
        return $crawler;
128
    }
129
130
    /**
131
     * @var ContainerInterface
132
     */
133
    private $container;
134
135
    /**
136
     * @return UrlGeneratorInterface
137
     */
138
    private function getRouter()
139
    {
140
        return $this->getContainer()->get('router');
141
    }
142
143
    /**
144
     * Get the current container
145
     * @return \Symfony\Component\DependencyInjection\ContainerInterface
146
     */
147
    private function getContainer()
148
    {
149
        if ($this->container == null) {
150
            $this->container = static::$kernel->getContainer();
151
        }
152
153
        return $this->container;
154
    }
155
156
    /**
157
     * @return EntityManager
158
     */
159
    private function getEntityManager()
160
    {
161
        return $this->getContainer()->get('doctrine.orm.entity_manager');
162
    }
163
164
    /**
165
     * Check if the current setup is a full application.
166
     * If not, mark the test as skipped else continue.
167
     */
168
    private function checkApplication()
169
    {
170
        try {
171
            static::$kernel = static::createKernel(array());
172
        } catch (\RuntimeException $ex) {
173
            $this->markTestSkipped("There does not seem to be a full application available (e.g. running tests on travis.org). So this test is skipped.");
174
175
            return;
176
        }
177
    }
178
179
}
180