Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 5 | class ImageProviderTest extends AdminTestAbstract |
||
| 6 | { |
||
| 7 | public function testImage() |
||
| 8 | { |
||
| 9 | $provider = 'image'; |
||
| 10 | |||
| 11 | $crawler = $this->client->request('GET', self::BASE_PATH.'create?provider='.$provider); |
||
| 12 | |||
| 13 | $form = $crawler->selectButton('Create')->form(); |
||
| 14 | |||
| 15 | $this->assertSonataFormValues( |
||
| 16 | $form, |
||
| 17 | [ |
||
| 18 | 'provider' => $provider, |
||
| 19 | ] |
||
| 20 | ); |
||
| 21 | |||
| 22 | $this->setFormBinaryContent($form, $this->getFixturesPath().'monk.jpg'); |
||
| 23 | |||
| 24 | $crawler = $this->client->submit($form); |
||
| 25 | $form = $crawler->selectButton('Update')->form(); |
||
| 26 | |||
| 27 | $this->assertSonataFormValues($form, ['title' => 'monk']); |
||
| 28 | |||
| 29 | $this->client->request('GET', self::BASE_PATH.'list'); |
||
| 30 | $this->assertContains('monk', $this->client->getResponse()->getContent()); |
||
| 31 | |||
| 32 | $this->assertNumberOfFilesInPath(1, $this->getMediaPathPrivate()); |
||
| 33 | |||
| 34 | $this->verifyMediaImageIsGenerated(); |
||
| 35 | |||
| 36 | $crawler = $this->client->request('GET', '/twig'); |
||
| 37 | |||
| 38 | $this->assertEquals( |
||
| 39 | 4, |
||
| 40 | $crawler->filter('img')->count() |
||
| 41 | ); |
||
| 42 | |||
| 43 | $this->assertEquals( |
||
| 44 | 0, |
||
| 45 | $crawler->filter('iframe')->count() |
||
| 46 | ); |
||
| 47 | |||
| 48 | $this->assertEquals( |
||
| 49 | 1, |
||
| 50 | $crawler->filter('a')->count() |
||
| 51 | ); |
||
| 52 | |||
| 53 | $this->client->request('GET', '/api'); |
||
| 54 | $data = json_decode($this->client->getResponse()->getContent(), true); |
||
| 55 | |||
| 56 | $this->assertArrayHasKey('url', $data); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function testEmptyFileUpload() |
||
| 60 | { |
||
| 61 | $provider = 'image'; |
||
| 62 | |||
| 63 | $crawler = $this->client->request('GET', self::BASE_PATH.'create?provider='.$provider); |
||
| 64 | |||
| 65 | $form = $crawler->selectButton('Create')->form(); |
||
| 66 | |||
| 67 | $this->assertSonataFormValues( |
||
| 68 | $form, |
||
| 69 | [ |
||
| 70 | 'provider' => $provider, |
||
| 71 | ] |
||
| 72 | ); |
||
| 73 | |||
| 74 | $this->client->submit($form); |
||
| 75 | |||
| 76 | $this->assertContains('This value should not be blank', $this->client->getResponse()->getContent()); |
||
| 77 | $this->assertContains('This value should not be blank', $this->client->getResponse()->getContent()); |
||
| 78 | } |
||
| 79 | } |
||
| 80 |