Completed
Pull Request — master (#438)
by Michael
14:10 queued 07:20
created

FileHandlerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 12.88 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
dl 17
loc 132
rs 10
c 1
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 5 2
A setUp() 0 4 1
A testOpenReturnsNewObjectWhenFileExists() 0 6 1
A testCloseWhenNotInitialized() 0 3 1
A testCloseWhenInitialized() 0 5 1
A testRead() 0 8 1
A testOpenThrowsExceptionWhenFileDoesNotExist() 0 4 1
A testSeek() 8 8 1
A testEofOnEmptyFile() 0 6 1
A testEofForNotEmptyFile() 0 8 1
A testWrite() 8 8 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
/* Copyright (C) 2017 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace DembeloMain\Tests\Service;
21
22
use DembeloMain\Service\FileHandler;
23
use PHPUnit\Framework\TestCase;
24
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
25
26
/**
27
 * Class FileHandlerTest
28
 * @package DembeloMain\Tests\Service
29
 */
30
class FileHandlerTest extends TestCase
31
{
32
    /**
33
     * @var FileHandler
34
     */
35
    private $fileHandler;
36
37
    /**
38
     * @var string
39
     */
40
    private $tmpFileName;
41
42
    /**
43
     * @return void
44
     */
45
    public function setUp(): void
46
    {
47
        $this->fileHandler = new FileHandler();
48
        $this->tmpFileName = @tempnam('/tmp/phpunit', 'fileHandlerTest');
49
    }
50
51
    /**
52
     * @return void
53
     */
54
    public function tearDown(): void
55
    {
56
        $this->fileHandler = null;
57
        if (file_exists($this->tmpFileName)) {
58
            unlink($this->tmpFileName);
59
        }
60
    }
61
62
    /**
63
     * @return void
64
     */
65
    public function testOpenThrowsExceptionWhenFileDoesNotExist(): void
66
    {
67
        $this->expectException(FileNotFoundException::class);
68
        $this->fileHandler->open('unknownFile', 'r');
69
    }
70
71
    /**
72
     * @return void
73
     */
74
    public function testOpenReturnsNewObjectWhenFileExists(): void
75
    {
76
        touch($this->tmpFileName);
77
        $newObject = $this->fileHandler->open($this->tmpFileName, 'r');
78
        self::assertInstanceOf(FileHandler::class, $newObject);
79
        self::assertNotSame($this->fileHandler, $newObject);
80
    }
81
82
    /**
83
     * @return void
84
     */
85
    public function testCloseWhenNotInitialized(): void
86
    {
87
        self::assertFalse($this->fileHandler->close());
88
    }
89
90
    /**
91
     * @return void
92
     */
93
    public function testCloseWhenInitialized(): void
94
    {
95
        touch($this->tmpFileName);
96
        $fileHandler = $this->fileHandler->open($this->tmpFileName, 'r');
97
        self::assertTrue($fileHandler->close());
98
    }
99
100
    /**
101
     * @return void
102
     */
103
    public function testEofOnEmptyFile(): void
104
    {
105
        touch($this->tmpFileName);
106
        $fileHandler = $this->fileHandler->open($this->tmpFileName, 'r');
107
        $fileHandler->read(1);
108
        self::assertTrue($fileHandler->eof());
109
    }
110
111
    /**
112
     * @return void
113
     */
114
    public function testEofForNotEmptyFile(): void
115
    {
116
        file_put_contents($this->tmpFileName, 'one');
117
        $fileHandler = $this->fileHandler->open($this->tmpFileName, 'r');
118
        $fileHandler->read(2);
119
        self::assertFalse($fileHandler->eof());
120
        $fileHandler->read(2);
121
        self::assertTrue($fileHandler->eof());
122
    }
123
124
    /**
125
     * @return void
126
     */
127
    public function testRead(): void
128
    {
129
        file_put_contents($this->tmpFileName, 'onetwothree');
130
        $fileHandler = $this->fileHandler->open($this->tmpFileName, 'r');
131
        self::assertEquals('one', $fileHandler->read(3));
132
        self::assertEquals('two', $fileHandler->read(3));
133
        self::assertEquals('three', $fileHandler->read(6));
134
        self::assertEquals('', $fileHandler->read(1));
135
136
    }
137
138
    /**
139
     * @return void
140
     */
141 View Code Duplication
    public function testSeek(): void
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...
142
    {
143
        file_put_contents($this->tmpFileName, 'onetwothree');
144
        $fileHandler = $this->fileHandler->open($this->tmpFileName, 'r');
145
        $fileHandler->seek(3);
146
        self::assertEquals('two', $fileHandler->read(3));
147
        $fileHandler->seek(0);
148
        self::assertEquals('one', $fileHandler->read(3));
149
    }
150
151
    /**
152
     * @return void
153
     */
154 View Code Duplication
    public function testWrite(): void
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...
155
    {
156
        file_put_contents($this->tmpFileName, 'one');
157
        $fileHandler = $this->fileHandler->open($this->tmpFileName, 'r+');
158
        self::assertEquals('one', $fileHandler->read(3));
159
        $fileHandler->write('two');
160
        $fileHandler->close();
161
        self::assertStringEqualsFile($this->tmpFileName, 'onetwo');
162
    }
163
}