Passed
Push — master ( fa975d...67ba18 )
by Radosław
02:17
created

AuctionTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 170
rs 10
c 0
b 0
f 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
    protected function getAuctionArray()
12
    {
13
        return [
14
            1 => 'test string',
15
            2 => 42,
16
            3 => '05-03-2017',
17
            4 => 12.34,
18
        ];
19
    }
20
21
22
    protected function getFakeApiRepresentation()
23
    {
24
        return [
25
            [
26
                'fid' => 1337,
27
                'fvalueString' => '',
28
                'fvalueInt' => 0,
29
                'fvalueFloat' => 0,
30
                'fvalueImage' => '',
31
                'fvalueDatetime' => 0,
32
                'fvalueDate' => '09-03-2017',
33
                'fvalueRangeInt' => [
34
                    'fvalueRangeIntMin' => 0,
35
                    'fvalueRangeIntMax' => 0,
36
                ],
37
                'fvalueRangeFloat' => [
38
                    'fvalueRangeFloatMin' => 0,
39
                    'fvalueRangeFloatMax' => 0,
40
                ],
41
                'fvalueRangeDate' => [
42
                    'fvalueRangeDateMin' => '',
43
                    'fvalueRangeDateMax' => '',
44
                ],
45
            ],
46
            [
47
                'fid' => 42,
48
                'fvalueString' => 'Ultimate Question of Life, the Universe, and Everything',
49
                'fvalueInt' => 0,
50
                'fvalueFloat' => 0,
51
                'fvalueImage' => '',
52
                'fvalueDatetime' => 0,
53
                'fvalueDate' => '',
54
                'fvalueRangeInt' => [
55
                    'fvalueRangeIntMin' => 0,
56
                    'fvalueRangeIntMax' => 0,
57
                ],
58
                'fvalueRangeFloat' => [
59
                    'fvalueRangeFloatMin' => 0,
60
                    'fvalueRangeFloatMax' => 0,
61
                ],
62
                'fvalueRangeDate' => [
63
                    'fvalueRangeDateMin' => '',
64
                    'fvalueRangeDateMax' => '',
65
                ],
66
            ],
67
        ];
68
    }
69
70
    public function getFakeApiRepresentationExpectedArray()
71
    {
72
        return [
73
            1337 => '09-03-2017',
74
            42 => 'Ultimate Question of Life, the Universe, and Everything'
75
        ];
76
    }
77
78
    /**
79
     * @expectedException InvalidArgumentException
80
     * @expectedExceptionMessage Photo files limit exceeded, 8 allowed, 9 given
81
     */
82
    public function testExceptionOnTooManyPhotos()
83
    {
84
        $auction = new Auction();
85
        $auction->setPhotos([
86
            'maximum', 'number', 'of', 'photos', 'is', 'eight', 'so', 'test', 'nine'
87
        ]);
88
    }
89
90
91
    protected function prepareTestAuction()
92
    {
93
        return new Auction($this->getAuctionArray());
94
    }
95
96
97
    protected function checkTestAuctionFields(array $fields)
98
    {
99
        $testString = array_shift($fields);
100
        $this->assertArrayHasKey('fvalueString', $testString);
101
        $this->assertSame('test string', $testString['fvalueString']);
102
103
        $testInt = array_shift($fields);
104
        $this->assertArrayHasKey('fvalueInt', $testInt);
105
        $this->assertSame(42, $testInt['fvalueInt']);
106
107
        $testDate = array_shift($fields);
108
        $this->assertArrayHasKey('fvalueDate', $testDate);
109
        $this->assertSame('05-03-2017', $testDate['fvalueDate']);
110
111
        $testFloat = array_shift($fields);
112
        $this->assertArrayHasKey('fvalueFloat', $testFloat);
113
        $this->assertSame(12.34, $testFloat['fvalueFloat']);
114
    }
115
116
117
    public function testApiRepresentationWithoutPhotos()
118
    {
119
        $auction = $this->prepareTestAuction();
120
121
        $apiRepresentation = $auction->toApiRepresentation();
122
123
        $this->assertArrayHasKey('fields', $apiRepresentation);
124
        $this->assertEquals(4, count($apiRepresentation['fields']));
125
126
        $this->checkTestAuctionFields($apiRepresentation['fields']);
127
    }
128
129
130
    public function testApiRepresentationWithPhotos()
131
    {
132
        $auction = $this->prepareTestAuction();
133
134
        $auction->setPhotos([
135
            __DIR__ . '/i.am.a.photo.file.txt'
136
        ]);
137
138
        $apiRepresentation = $auction->toApiRepresentation();
139
140
        $this->assertArrayHasKey('fields', $apiRepresentation);
141
        $this->assertEquals(5, count($apiRepresentation['fields']));
142
143
        $this->checkTestAuctionFields($apiRepresentation['fields']);
144
    }
145
146
147
    public function testArrayRepresentation()
148
    {
149
        $auction = $this->prepareTestAuction();
150
        $this->assertSame($this->getAuctionArray(), $auction->toArray());
151
    }
152
153
154
    /**
155
     * @expectedException Radowoj\Yaah\Exception
156
     * @expectedExceptionMessage Photo file is not readable
157
     */
158
    public function testApiRepresentationWithInvalidPhotos()
159
    {
160
        $auction = $this->prepareTestAuction();
161
162
        $auction->setPhotos([
163
            __DIR__ . '/i.am.not.a.photo.file.txt'
164
        ]);
165
166
        $auction->toApiRepresentation();
167
    }
168
169
170
    public function testFromApiRepresentation()
171
    {
172
        $auction = new Auction();
173
        $auction->fromApiRepresentation($this->getFakeApiRepresentation());
174
        $this->assertSame($this->getFakeApiRepresentationExpectedArray(), $auction->toArray());
175
    }
176
177
}
178