Test Failed
Push — master ( 440ce5...0cdbc1 )
by Siad
07:01
created

FileOutputStreamTest::tearDown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information please see
19
 * <http://phing.info>.
20
 */
21
22
/**
23
 * Unit test for FileOutputStream.
24
 *
25
 * @author Hans Lellelid <[email protected]>
26
 * @package phing.system
27
 */
28
class FileOutputStreamTest extends \PHPUnit\Framework\TestCase
29
{
30
31
    /**
32
     * @var FileOutputStream
33
     */
34
    private $outStream;
35
36
    public function setUp(): void
37
    {
38
        $this->tmpFile = new PhingFile(PHING_TEST_BASE . "/tmp/" . get_class($this) . ".txt");
0 ignored issues
show
Bug Best Practice introduced by
The property tmpFile does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
The constant PHING_TEST_BASE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
        $this->outStream = new FileOutputStream($this->tmpFile);
40
    }
41
42
    public function tearDown(): void
43
    {
44
        if (is_object($this->outStream)) {
45
            $this->outStream->close();
46
        }
47
        FileSystem::getFileSystem()->unlink($this->tmpFile->getAbsolutePath());
48
    }
49
50
    public function assertFileContents($contents)
51
    {
52
        $actual = file_get_contents($this->tmpFile->getAbsolutePath());
53
        $this->assertEquals(
54
            $contents,
55
            $actual,
56
            "Expected file contents to match; expected '" . $contents . "', actual '" . $actual . "'"
57
        );
58
    }
59
60
    public function testWrite()
61
    {
62
        $string = "0123456789";
63
        $this->outStream->write($string);
64
65
        $this->assertFileContents($string);
66
67
        $newstring = $string;
68
69
        // check offset (no len)
70
        $this->outStream->write($string, 1);
71
        $this->outStream->flush();
72
        $newstring .= '123456789';
73
        $this->assertFileContents($newstring);
74
75
        // check len (no offset)
76
        $this->outStream->write($string, 0, 3);
77
        $this->outStream->flush();
78
        $newstring .= '012';
79
        $this->assertFileContents($newstring);
80
    }
81
82
    public function testFlush()
83
    {
84
        $this->outStream->write("Some data");
85
        $this->outStream->flush();
86
        $this->outStream->close();
87
88
        try {
89
            $this->outStream->flush();
90
            $this->fail("Expected IOException when attempting to flush a closed stream.");
91
        } catch (IOException $ioe) {
92
            // exception is expected
93
        }
94
    }
95
}
96