Completed
Push — master ( b9874a...3faeb6 )
by Alan
04:12
created

TwigFakerExtensionTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 12.73 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 2
dl 14
loc 110
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A it_registers_the_extension_name_correctly() 0 6 1
A it_registers_a_fake_function() 0 6 1
A it_throws_an_exception_when_an_invalid_factory_is_used() 0 6 1
A it_returns_one_fake_item_when_no_count_is_passed() 0 6 1
A it_returns_fifteen_fake_items_when_the_count_is_passed() 0 6 1
A it_caches_a_single_fake_item_when_given_a_cache_key() 7 7 1
A it_caches_multiple_fake_item_when_given_a_cache_key() 7 7 1
A it_caches_single_fake_items_with_different_cache_keys() 0 12 1
A it_allows_the_same_cache_key_to_be_used_for_different_factories() 0 7 1

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
use Ablett\TwigFaker\TwigFakerExtension;
4
use Ablett\TwigFaker\InvalidFactoryException;
5
6
class TwigFakerExtensionTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    protected $twigFaker;
9
10
    public function setUp()
11
    {
12
        TwigFakerExtension::$factoriesPath = 'tests/factories/';
13
14
        $this->twigFaker = new TwigFakerExtension;
15
    }
16
17
    /**
18
     * @test
19
     */
20
    public function it_registers_the_extension_name_correctly()
21
    {
22
        $name = $this->twigFaker->getName();
23
24
        $this->assertEquals('faker', $name);
25
    }
26
27
    /**
28
     * @test
29
     */
30
    public function it_registers_a_fake_function()
31
    {
32
        $registeredFunctions = $this->twigFaker->getFunctions();
33
34
        $this->assertArrayHasKey('fake', $registeredFunctions);
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function it_throws_an_exception_when_an_invalid_factory_is_used()
41
    {
42
        $this->expectException(InvalidFactoryException::class);
43
44
        $data = $this->twigFaker->fakerData('does-not-exist');
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
    }
46
47
    /**
48
     * @test
49
     */
50
    public function it_returns_one_fake_item_when_no_count_is_passed()
51
    {
52
        $fakePerson = $this->twigFaker->fakerData('person');
53
54
        $this->assertCount(1, $fakePerson);
55
    }
56
57
    /**
58
     * @test
59
     */
60
    public function it_returns_fifteen_fake_items_when_the_count_is_passed()
61
    {
62
        $fakePeople = $this->twigFaker->fakerData('person', 15);
63
64
        $this->assertCount(15, $fakePeople);
65
    }
66
67
    /**
68
     * @test
69
     */
70 View Code Duplication
    public function it_caches_a_single_fake_item_when_given_a_cache_key()
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...
71
    {
72
        $fakePerson = $this->twigFaker->fakerData('person', 1, 'my-cache-key');
73
        $cachedFakePerson = $this->twigFaker->fakerData('person', 1, 'my-cache-key');
74
75
        $this->assertSame($fakePerson, $cachedFakePerson);
76
    }
77
78
    /**
79
     * @test
80
     */
81 View Code Duplication
    public function it_caches_multiple_fake_item_when_given_a_cache_key()
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...
82
    {
83
        $fakePeople = $this->twigFaker->fakerData('person', 35, 'my-cache-key');
84
        $cachedFakePeople = $this->twigFaker->fakerData('person', 35, 'my-cache-key');
85
86
        $this->assertSame($fakePeople, $cachedFakePeople);
87
    }
88
89
    /**
90
     * @test
91
     */
92
    public function it_caches_single_fake_items_with_different_cache_keys()
93
    {
94
        $fakePerson = $this->twigFaker->fakerData('person', 1, 'cache-key-one');
95
        $cachedfakePerson = $this->twigFaker->fakerData('person', 1, 'cache-key-one');
96
97
        $anotherFakePerson = $this->twigFaker->fakerData('person', 1, 'cache-key-two');
98
        $cachedAnotherFakePerson = $this->twigFaker->fakerData('person', 1, 'cache-key-two');
99
100
        $this->assertSame($fakePerson, $cachedfakePerson);
101
        $this->assertSame($anotherFakePerson, $cachedAnotherFakePerson);
102
        $this->assertNotSame($fakePerson, $anotherFakePerson);
103
    }
104
105
    /**
106
     * @test
107
     */
108
    public function it_allows_the_same_cache_key_to_be_used_for_different_factories()
109
    {
110
        $fakePerson = $this->twigFaker->fakerData('person', 1, 'cache-key-one');
111
        $fakeAnimal = $this->twigFaker->fakerData('animal', 1, 'cache-key-one');
112
113
        $this->assertNotSame($fakePerson, $fakeAnimal);
114
    }
115
}