| Conditions | 2 |
| Paths | 2 |
| Total Lines | 82 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 95 | public function testShowLog() |
||
| 96 | { |
||
| 97 | $this->checkApplication(); |
||
| 98 | |||
| 99 | // Create a new client to browse the application |
||
| 100 | $client = static::createClient(); |
||
| 101 | $client->followRedirects(); |
||
| 102 | |||
| 103 | $manager = $this->getEntityManager(); |
||
| 104 | $eventReop = $manager->getRepository("Azine\MailgunWebhooksBundle\Entity\MailgunEvent"); |
||
| 105 | |||
| 106 | $apiKey = $this->getContainer()->getParameter(AzineMailgunWebhooksExtension::PREFIX."_".AzineMailgunWebhooksExtension::API_KEY); |
||
| 107 | |||
| 108 | // make sure there is some data in the application |
||
| 109 | if (sizeof($eventReop->findAll()) < 102) { |
||
| 110 | TestHelper::addMailgunEvents($manager, 102, $apiKey); |
||
| 111 | } |
||
| 112 | $count = sizeof($eventReop->findAll()); |
||
| 113 | |||
| 114 | // view the list of events |
||
| 115 | $pageSize = 25; |
||
| 116 | $listUrl = substr($this->getRouter()->generate("mailgunevent_list", array('_locale' => "en", 'page' => 1, 'pageSize' => $pageSize, 'clear' => true)), 13); |
||
| 117 | $crawler = $this->loginUserIfRequired($client, $listUrl); |
||
| 118 | $this->assertEquals($pageSize+1, $crawler->filter(".eventsTable tr")->count(), "$pageSize Mailgun events (+1 header row) expected on this page ($listUrl)!"); |
||
| 119 | |||
| 120 | // view a single event |
||
| 121 | $link = $crawler->filter(".eventsTable tr a:first-child")->first()->link(); |
||
| 122 | $posLastSlash = strrpos($link->getUri(), "/"); |
||
| 123 | $posOfIdStart = strrpos($link->getUri(), "/", -6) +1; |
||
| 124 | $eventId = substr($link->getUri(), $posOfIdStart, $posLastSlash-$posOfIdStart); |
||
| 125 | $crawler = $client->click($link); |
||
| 126 | $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Status 200 expected."); |
||
| 127 | $this->assertEquals($eventId, $crawler->filter("td")->first()->text(), "Content of first td should be the eventId ($eventId)"); |
||
| 128 | |||
| 129 | // delete the event from show-page |
||
| 130 | $link = $crawler->selectLink("delete")->link(); |
||
| 131 | $crawler = $client->click($link); |
||
| 132 | |||
| 133 | // check that it is gone from the list |
||
| 134 | $this->assertEquals(0, $crawler->filter("#event$eventId")->count(), "The deleted event should not be in the list anymore."); |
||
| 135 | |||
| 136 | // delete the event from list-page |
||
| 137 | $crawler = $client->followRedirect(); |
||
| 138 | $link = $crawler->filter(".eventsTable tr .deleteLink")->first()->link(); |
||
| 139 | $delUri = $link->getUri(); |
||
| 140 | $eventId = substr($delUri, strrpos($delUri, "/") + 1); |
||
| 141 | $crawler = $client->click($link); |
||
| 142 | |||
| 143 | // check that it is gone from the list |
||
| 144 | $this->assertEquals(0, $crawler->filter("#event$eventId")->count(), "The deleted event should not be in the list anymore."); |
||
| 145 | |||
| 146 | // filter the list for something |
||
| 147 | $crawler = $client->followRedirect(); |
||
| 148 | $form = $crawler->selectButton("Filter")->form(); |
||
| 149 | $form['filter[eventType]']->select("delivered"); |
||
| 150 | $crawler = $client->submit($form); |
||
| 151 | $this->assertEquals($crawler->filter(".eventsTable tr")->count() -1, $crawler->filter(".eventsTable a:contains('delivered')")->count(), "There should only be 'delivered' events in the list"); |
||
| 152 | |||
| 153 | // delete entry with xmlHttpRequest |
||
| 154 | $eventToDelete = $eventReop->findOneBy(array()); |
||
| 155 | $ajaxUrl = substr($this->getRouter()->generate("mailgunevent_delete_ajax", array("_locale" => "en")),13); |
||
| 156 | $client->request("POST", $ajaxUrl, array('eventId' => $eventToDelete->getId()), array(), array('HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')); |
||
| 157 | $this->assertEquals('{"success":true}', $client->getResponse()->getContent(), "JSON response expcted from $ajaxUrl. for event with id:".$eventToDelete->getId()); |
||
| 158 | |||
| 159 | // show/delete inexistent log entry |
||
| 160 | $inexistentEventId = md5("123invalid"); |
||
| 161 | $url = substr($this->getRouter()->generate("mailgunevent_delete", array("_locale" => "en", "eventId" => $inexistentEventId)),13); |
||
| 162 | $client->request("GET", $url); |
||
| 163 | $this->assertEquals(404, $client->getResponse()->getStatusCode(), "404 expected for invalid eventId ($inexistentEventId)."); |
||
| 164 | |||
| 165 | $url = substr($this->getRouter()->generate("mailgunevent_show", array("_locale" => "en", "id" => $inexistentEventId)),13); |
||
| 166 | $client->request("GET", $url); |
||
| 167 | $this->assertEquals(404, $client->getResponse()->getStatusCode(), "404 expected."); |
||
| 168 | |||
| 169 | // show inexistent page |
||
| 170 | $maxPage = floor($count/$pageSize); |
||
| 171 | $beyondListUrl = substr($this->getRouter()->generate("mailgunevent_list", array('_locale' => "en", 'page' => $maxPage + 1, 'pageSize' => $pageSize, 'clear' => true)),13); |
||
| 172 | $client->request("GET", $beyondListUrl); |
||
| 173 | $crawler = $client->followRedirect(); |
||
| 174 | $this->assertEquals(2, $crawler->filter(".pagination .disabled:contains('Next')")->count(), "Expected to be on the last page => the next button should be disabled."); |
||
| 175 | |||
| 176 | } |
||
| 177 | |||
| 277 |