Completed
Push — master ( b0dd07...48a5d3 )
by Aydin
02:14
created

DependencyHeavenTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 7
c 4
b 0
f 1
lcom 1
cbo 6
dl 12
loc 63
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testDependencyHeavenExercise() 12 12 1
A testGetRequiredPackages() 0 7 1
A testGetRequests() 0 13 2
A testGetRequestsReturnsMultipleRequestsForEachEndpoint() 0 14 2

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace PhpSchool\LearnYouPhpTest\Exercise;
4
5
use Faker\Factory;
6
use Faker\Generator;
7
use PhpSchool\LearnYouPhp\Exercise\DependencyHeaven;
8
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
9
use PHPUnit_Framework_TestCase;
10
use Psr\Http\Message\RequestInterface;
11
12
/**
13
 * Class DependencyHeavenTest
14
 * @package PhpSchool\LearnYouPhpTest\Exercise
15
 * @author Michael Woodward <[email protected]>
16
 */
17
class DependencyHeavenTest extends PHPUnit_Framework_TestCase
18
{
19
20
    /**
21
     * @var Generator
22
     */
23
    private $faker;
24
25
    public function setUp()
26
    {
27
        $this->faker = Factory::create('Fr_fr');
28
    }
29
30 View Code Duplication
    public function testDependencyHeavenExercise()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32
        $e = new DependencyHeaven($this->faker);
33
34
        $this->assertEquals('Dependency Heaven', $e->getName());
35
        $this->assertEquals('An introduction to Composer dependency management', $e->getDescription());
36
37
        $this->assertInstanceOf(SolutionInterface::class, $e->getSolution());
38
        $this->assertFileExists(realpath($e->getSolution()->getEntryPoint()));
39
        $this->assertFileExists(realpath($e->getProblem()));
40
        $this->assertNull($e->tearDown());
41
    }
42
43
    public function testGetRequiredPackages()
44
    {
45
        $this->assertSame(
46
            ['klein/klein', 'danielstjules/stringy'],
47
            (new DependencyHeaven($this->faker))->getRequiredPackages()
48
        );
49
    }
50
51
    public function testGetRequests()
52
    {
53
        $e = new DependencyHeaven($this->faker);
54
55
        $requests  = $e->getRequests();
56
57
        foreach ($requests as $request) {
58
            $this->assertInstanceOf(RequestInterface::class, $request);
59
            $this->assertSame('POST', $request->getMethod());
60
            $this->assertSame(['application/x-www-form-urlencoded'], $request->getHeader('Content-Type'));
61
            $this->assertNotEmpty($request->getBody());
62
        }
63
    }
64
65
    public function testGetRequestsReturnsMultipleRequestsForEachEndpoint()
66
    {
67
        $e = new DependencyHeaven($this->faker);
68
69
        $endPoints = array_map(function (RequestInterface $request) {
70
            return $request->getUri()->getPath();
71
        }, $e->getRequests());
72
73
        $counts = array_count_values($endPoints);
74
        foreach (['/reverse', '/swapcase', '/titleize'] as $endPoint) {
75
            $this->assertTrue(isset($counts[$endPoint]));
76
            $this->assertGreaterThan(1, $counts[$endPoint]);
77
        }
78
    }
79
}
80