1 | <?php |
||
2 | |||
3 | namespace Azine\MailgunWebhooksBundle\Tests\Controller; |
||
4 | |||
5 | use Doctrine\ORM\EntityManager; |
||
6 | use Symfony\Bundle\FrameworkBundle\Client; |
||
7 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
||
8 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
9 | use Symfony\Component\DomCrawler\Crawler; |
||
10 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||
11 | |||
12 | class MailgunControllerTest extends WebTestCase |
||
13 | { |
||
14 | public function testShowOverviewAction() |
||
15 | { |
||
16 | $this->checkApplication(); |
||
17 | |||
18 | // Create a new client to browse the application |
||
19 | $client = static::createClient(); |
||
20 | |||
21 | $manager = $this->getEntityManager(); |
||
22 | $eventReop = $manager->getRepository("Azine\MailgunWebhooksBundle\Entity\MailgunEvent"); |
||
23 | $events = sizeof($eventReop->findAll()); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
24 | $bounced = sizeof($eventReop->findBy(array('event' => 'bounced'))); |
||
25 | $dropped = sizeof($eventReop->findBy(array('event' => 'dropped'))); |
||
26 | $spam = sizeof($eventReop->findBy(array('event' => 'complained'))); |
||
27 | $unsubscribed = sizeof($eventReop->findBy(array('event' => 'unsubscribed'))); |
||
28 | |||
29 | // view the list of events |
||
30 | $listUrl = substr($this->getRouter()->generate('mailgun_overview', array('_locale' => 'en')), 13); |
||
31 | $crawler = $this->loginUserIfRequired($client, $listUrl); |
||
32 | $pageSize = 10; |
||
33 | $this->assertSame($pageSize + 1, $crawler->filter('.eventsTable tr')->count(), "$pageSize Mailgun events (+1 header row) expected on this page ($listUrl)!"); |
||
34 | $this->assertSame(1, $crawler->filter("li a:contains('bounced ($bounced)')")->count(), "'bounced ($bounced)' expected on page."); |
||
35 | $this->assertSame(1, $crawler->filter("li a:contains('dropped ($dropped)')")->count(), "'dropped ($dropped)' expected on page."); |
||
36 | $this->assertSame(1, $crawler->filter("li a:contains('marked as spam by the user ($spam)')")->count(), "'marked as spam by the user ($spam)' expected on page."); |
||
37 | $this->assertSame(1, $crawler->filter("li a:contains('unsubscribe requests by users ($unsubscribed)')")->count(), "'unsubscribe requests by users ($unsubscribed)' expected on page."); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Load the url and login if required. |
||
42 | * |
||
43 | * @param string $url |
||
44 | * @param string $username |
||
45 | * @param Client $client |
||
46 | * |
||
47 | * @return Crawler $crawler of the page of the url or the page after the login |
||
48 | */ |
||
49 | private function loginUserIfRequired(Client $client, $url, $username = 'dominik', $password = 'lkjlkjlkjlkj') |
||
50 | { |
||
51 | // try to get the url |
||
52 | $client->followRedirects(); |
||
53 | $crawler = $client->request('GET', $url); |
||
54 | |||
55 | $this->assertSame(200, $client->getResponse()->getStatusCode(), 'Status-Code 200 expected.'); |
||
56 | |||
57 | // if redirected to a login-page, login as admin-user |
||
58 | if (5 == $crawler->filter('input')->count() && 1 == $crawler->filter('#username')->count() && 1 == $crawler->filter('#password')->count()) { |
||
59 | // set the password of the admin |
||
60 | $userProvider = $this->getContainer()->get('fos_user.user_provider.username_email'); |
||
61 | $user = $userProvider->loadUserByUsername($username); |
||
62 | $user->setPlainPassword($password); |
||
63 | $user->addRole('ROLE_ADMIN'); |
||
64 | |||
65 | $userManager = $this->getContainer()->get('fos_user.user_manager'); |
||
66 | $userManager->updateUser($user); |
||
67 | |||
68 | $crawler = $crawler->filter("input[type='submit']"); |
||
69 | $form = $crawler->form(); |
||
70 | $form->get('_username')->setValue($username); |
||
71 | $form->get('_password')->setValue($password); |
||
72 | $crawler = $client->submit($form); |
||
73 | } |
||
74 | |||
75 | $this->assertSame(200, $client->getResponse()->getStatusCode(), 'Login failed.'); |
||
76 | $client->followRedirects(false); |
||
77 | |||
78 | $this->assertStringEndsWith($url, $client->getRequest()->getUri(), "Login failed or not redirected to requested url: $url vs. ".$client->getRequest()->getUri()); |
||
79 | |||
80 | return $crawler; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @var ContainerInterface |
||
85 | */ |
||
86 | private $appContainer; |
||
87 | |||
88 | /** |
||
89 | * Get the current container. |
||
90 | * |
||
91 | * @return \Symfony\Component\DependencyInjection\ContainerInterface |
||
92 | */ |
||
93 | private function getContainer() |
||
94 | { |
||
95 | if (null == $this->appContainer) { |
||
96 | $this->appContainer = static::$kernel->getContainer(); |
||
97 | } |
||
98 | |||
99 | return $this->appContainer; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @return UrlGeneratorInterface |
||
104 | */ |
||
105 | private function getRouter() |
||
106 | { |
||
107 | return $this->getContainer()->get('router'); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return EntityManager |
||
112 | */ |
||
113 | private function getEntityManager() |
||
114 | { |
||
115 | return $this->getContainer()->get('doctrine.orm.entity_manager'); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Check if the current setup is a full application. |
||
120 | * If not, mark the test as skipped else continue. |
||
121 | */ |
||
122 | private function checkApplication() |
||
123 | { |
||
124 | try { |
||
125 | static::$kernel = static::createKernel(array()); |
||
126 | static::$kernel->boot(); |
||
127 | } catch (\RuntimeException $ex) { |
||
128 | $this->markTestSkipped('There does not seem to be a full application available (e.g. running tests on travis.org). So this test is skipped.'); |
||
129 | |||
130 | return; |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 |