Passed
Push — master ( 0aa822...632b98 )
by Radosław
07:52
created

AuctionTest::prepareTestAuction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Radowoj\Yaah;
4
5
use PHPUnit\Framework\TestCase;
6
use Radowoj\Yaah\Auction;
7
8
class AuctionTest extends TestCase
9
{
10
11
    /**
12
     * @expectedException InvalidArgumentException
13
     * @expectedExceptionMessage Photo files limit exceeded, 8 allowed, 9 given
14
     */
15
    public function testExceptionOnTooManyPhotos()
16
    {
17
        $auction = new Auction();
18
        $auction->setPhotos([
19
            'maximum', 'number', 'of', 'photos', 'is', 'eight', 'so', 'test', 'nine'
20
        ]);
21
    }
22
23
24
    protected function prepareTestAuction()
25
    {
26
        return new Auction([
27
            1 => 'test string',
28
            2 => 42,
29
            3 => '05-03-2017',
30
            4 => 12.34,
31
        ]);
32
    }
33
34
35
    protected function checkTestAuctionFields(array $fields)
36
    {
37
        $testString = array_shift($fields);
38
        $this->assertArrayHasKey('fvalueString', $testString);
39
        $this->assertSame('test string', $testString['fvalueString']);
40
41
        $testInt = array_shift($fields);
42
        $this->assertArrayHasKey('fvalueInt', $testInt);
43
        $this->assertSame(42, $testInt['fvalueInt']);
44
45
        $testDate = array_shift($fields);
46
        $this->assertArrayHasKey('fvalueDate', $testDate);
47
        $this->assertSame('05-03-2017', $testDate['fvalueDate']);
48
49
        $testFloat = array_shift($fields);
50
        $this->assertArrayHasKey('fvalueFloat', $testFloat);
51
        $this->assertSame(12.34, $testFloat['fvalueFloat']);
52
    }
53
54
55
    public function testApiRepresentationWithoutPhotos()
56
    {
57
        $auction = $this->prepareTestAuction();
58
59
        $apiRepresentation = $auction->toApiRepresentation();
60
61
        $this->assertArrayHasKey('fields', $apiRepresentation);
62
        $this->assertEquals(4, count($apiRepresentation['fields']));
63
64
        $this->checkTestAuctionFields($apiRepresentation['fields']);
65
    }
66
67
68 View Code Duplication
    public function testApiRepresentationWithPhotos()
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...
69
    {
70
        $auction = $this->prepareTestAuction();
71
72
        $auction->setPhotos([
73
            __DIR__ . '/i.am.a.photo.file.txt'
74
        ]);
75
76
        $apiRepresentation = $auction->toApiRepresentation();
77
78
        $this->assertArrayHasKey('fields', $apiRepresentation);
79
        $this->assertEquals(5, count($apiRepresentation['fields']));
80
81
        $this->checkTestAuctionFields($apiRepresentation['fields']);
82
    }
83
84
85
    /**
86
     * @expectedException Radowoj\Yaah\Exception
87
     * @expectedExceptionMessage Photo file is not readable
88
     */
89 View Code Duplication
    public function testApiRepresentationWithInvalidPhotos()
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...
90
    {
91
        $auction = $this->prepareTestAuction();
92
93
        $auction->setPhotos([
94
            __DIR__ . '/i.am.not.a.photo.file.txt'
95
        ]);
96
97
        $apiRepresentation = $auction->toApiRepresentation();
98
99
        $this->assertArrayHasKey('fields', $apiRepresentation);
100
        $this->assertEquals(5, count($apiRepresentation['fields']));
101
102
        $fields = $apiRepresentation['fields'];
0 ignored issues
show
Unused Code introduced by
$fields 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...
103
104
        $this->checkTestAuctionFields($apiRepresentation['fields']);
105
    }
106
107
}
108