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