Passed
Push — master ( f437aa...c74702 )
by Sam
04:15
created

ItemTest::itemBasics()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace App\Tests;
4
5
use App\Item;
6
use App\Config;
7
use App\User;
8
use Intervention\Image\ImageManagerStatic as Image;
9
10
class ItemTest extends Base
11
{
12
13
    /** @var User */
14
    protected $testUser;
15
16
    public function setUp()
17
    {
18
        parent::setUp();
19
        $this->setUpDb();
20
        $this->testUser = new User($this->db);
21
        $this->testUser->register('Test User');
22
    }
23
24
    /**
25
     * @testdox An Item has an ID and title and has by default plain text contents.
26
     * @test
27
     */
28
    public function itemBasics()
29
    {
30
        $item = new Item(null, $this->testUser);
31
        $item->save(['title' => 'Test']);
32
        $this->assertEquals(1, $item->getId());
33
        $this->assertEquals('Test', $item->getTitle());
34
        $this->assertEquals(1, $this->db->query("SELECT COUNT(*) FROM `items`")->fetchColumn());
35
        $this->assertTrue($item->isLoaded());
36
        $this->assertEquals('c4/ca/1/v1', $item->getFilePath());
37
        $this->assertEquals('text/plain', $item->getMimeType());
38
    }
39
40
    public function testDates()
41
    {
42
        $item = new Item(null, $this->testUser);
43
        $item->save(['title' => 'Test', 'date' => '2013-01-12 13:45']);
44
        $this->assertEquals('2013-01-12 13:45:00', $item->getDate());
45
        $this->assertEquals(Item::DATE_GRANULARITY_DEFAULT, $item->getDateGranularity());
46
47
        $item->save(['title' => 'Test', 'date' => '2013-01-12 13:45', 'date_granularity' => 3]);
48
        $this->assertEquals('2013-01-12 13:45:00', $item->getDate());
49
        $this->assertEquals(3, $item->getDateGranularity());
50
        $this->assertEquals('January 2013', $item->getDateFormatted());
51
52
        $item->save(['title' => 'Test', 'date' => '2013-01-12 13:45', 'date_granularity' => 5]);
53
        $this->assertEquals('c. 2013', $item->getDateFormatted());
54
    }
55
56
    /**
57
     * @testdox An item can be created and modified.
58
     */
59
    public function modification()
60
    {
61
        $item = new Item(null, $this->testUser);
62
        $item->save(['title' => 'Test']);
63
        $this->assertEquals(1, $item->getId());
64
        $this->assertEquals('Test', $item->getTitle());
65
        $this->assertEquals(1, $this->db->query("SELECT COUNT(*) FROM `items`")->fetchColumn());
66
        $item->save(['id' => 1, 'title' => 'Testing Title']);
67
        $this->assertEquals(1, $item->getId());
68
        $this->assertEquals('Testing Title', $item->getTitle());
69
        $this->assertEquals(1, $this->db->query("SELECT COUNT(*) FROM `items`")->fetchColumn());
70
    }
71
72
    /**
73
     * @testdox
74
     * @test
75
     */
76
    public function tags()
77
    {
78
        $item = new Item(null, $this->testUser);
79
        $item->save([], 'one,two');
80
        $this->assertCount(2, $item->getTags());
81
    }
82
83
    /**
84
     * @testdox An item can have a single file attached.
85
     * @test
86
     */
87
    public function files()
88
    {
89
        $item = new Item(null, $this->testUser);
90
        // First version.
91
        $item->save([], null, null, 'Test file contents.');
92
        $this->assertSame(1, $item->getId());
93
        $this->assertSame('c4/ca/1/v1', $item->getFilePath());
94
        $this->assertFileExists(__DIR__ . '/data/storage/c4/ca/1/v1');
95
        $this->assertSame('Test file contents.', $item->getFileContents());
96
        $this->assertSame(1, $item->getVersionCount());
97
        // Second version.
98
        $item->save(['id' => 1], null, null, 'New file contents.');
99
        $this->assertSame(1, $item->getId());
100
        $this->assertSame(2, $item->getVersionCount());
101
        $this->assertSame('c4/ca/1/v2', $item->getFilePath());
102
        $this->assertFileExists(__DIR__ . '/data/storage/c4/ca/1/v1');
103
        $this->assertFileExists(__DIR__ . '/data/storage/c4/ca/1/v2');
104
        $this->assertSame('New file contents.', $item->getFileContents());
105
        $this->assertSame('Test file contents.', $item->getFileContents(1));
106
        // Upload a different file.
107
        $item2 = new Item(null, $this->testUser);
108
        $item2->save(['title' => 'Second test'], null, null, 'Second test.');
109
        $this->assertSame(2, $item2->getId());
110
        $this->assertSame('c8/1e/2/v1', $item2->getFilePath());
111
        $this->assertTrue($item2->isText());
112
    }
113
114
    public function testLocalCacheFile()
115
    {
116
        $config = new Config();
117
        $this->assertArrayHasKey('cache', $config->filesystems());
118
        $item = new Item(null, $this->testUser);
119
        $item->save([], null, null, 'Test file contents.');
120
        $this->assertSame(__DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR
121
            . 'cache' . DIRECTORY_SEPARATOR . '1_v1_o', $item->getCachePath());
122
        $this->assertFileExists(__DIR__ . '/data/cache/1_v1_o');
123
    }
124
125
    /**
126
     * @testdox A text file can be updated to be a text file.
127
     * @test
128
     */
129
    public function fileModifyTextToText()
130
    {
131
        // Create an item, and save it twice with different text content.
132
        $item = new Item(null, $this->testUser);
133
        $item->save([], null, null, 'First contents');
134
        $this->assertEquals('First contents', $item->getFileContents());
135
        $item->save(null, null, null, 'Second contents');
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
136
        $this->assertEquals('Second contents', $item->getFileContents());
137
138
        // Check that the version numbers and mime types are what we'd expect.
139
        $this->assertEquals('First contents', $item->getFileContents(1));
140
        $this->assertEquals('text/plain', $item->getMimeType(1));
141
        $this->assertEquals('Second contents', $item->getFileContents(2));
142
        $this->assertEquals('text/plain', $item->getMimeType(2));
143
    }
144
145
    /**
146
     * @testdox A text file can be updated to be an image file.
147
     * @test
148
     */
149
    public function fileModifyTextToImage()
150
    {
151
        $img = Image::canvas(200, 100, '#ccc');
152
        $tmpFilename = $this->dataDir() . '/test-image.jpg';
153
        $img->save($tmpFilename);
154
155
        // Create an item, and save it twice with different text content.
156
        $item = new Item(null, $this->testUser);
157
        $item->save([], null, null, 'First contents');
158
        $this->assertEquals('First contents', $item->getFileContents());
159
        $item->save(null, null, $tmpFilename);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
160
        $this->assertFileEquals($tmpFilename, $item->getCachePath());
161
162
        // Check that the version numbers and mime types are what we'd expect.
163
        $this->assertEquals('First contents', $item->getFileContents(1));
164
        $this->assertEquals('text/plain', $item->getMimeType(1));
165
        $this->assertFileEquals($tmpFilename, $item->getCachePath('o', 2));
166
        $this->assertEquals('image/jpeg', $item->getMimeType(2));
167
    }
168
169
    /**
170
     * @testdox Image thumbnails are correctly resized.
171
     * @test
172
     */
173
    public function correctSizedThumbnails()
174
    {
175
        // Create a large image.
176
        $img = Image::canvas(1000, 400, '#ccc');
177
        $tmpFilename = $this->dataDir() . '/test-image.jpg';
178
        $img->save($tmpFilename);
179
        // Add it to an Item.
180
        $item = new Item(null, $this->testUser);
181
        $item->save(null, null, $tmpFilename);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
182
        // Check that the various sizes returned are correct.
183
        $this->assertEquals('image/jpeg', $item->getMimeType());
184
        $this->assertFileEquals($tmpFilename, $item->getCachePath('o'));
185
        // Load the 'display' size.
186
        $display = Image::make($item->getCachePath('d'));
187
        $this->assertEquals(700, $display->getWidth());
188
        $this->assertEquals(280, $display->getHeight());
189
        // The thumbnail is always 200 x 200.
190
        $thumb = Image::make($item->getCachePath('t'));
191
        $this->assertEquals(200, $thumb->getWidth());
192
        $this->assertEquals(200, $thumb->getHeight());
193
    }
194
}
195