mageekguy\atoum\writers\file: lines coverage

83% of 280

OPs

90% of 40

Lines

69% of 29

Branches

82% of 17

Paths
Method OPs OPs % Lines Line % Branches Branches % Paths Path %
mageekguy\atoum\writers\file::__construct() 18 100% 3 100% 1 0% 1 100%
mageekguy\atoum\writers\file::__destruct() 8 100% 2 100% 1 0% 1 100%
mageekguy\atoum\writers\file::clear() 33 97% 4 100% 4 75% 2 100%
mageekguy\atoum\writers\file::writeRealtimeReport() 13 0% 1 0% 1 0% 1 0%
mageekguy\atoum\writers\file::writeAsynchronousReport() 16 0% 1 0% 1 0% 1 0%
mageekguy\atoum\writers\file::setFilename() 18 100% 2 100% 1 100% 1 100%
mageekguy\atoum\writers\file::getFilename() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\writers\file::reset() 15 0% 2 0% 1 0% 1 0%
mageekguy\atoum\writers\file::doWrite() 46 98% 5 100% 4 75% 2 100%
mageekguy\atoum\writers\file::openFile() 74 97% 12 100% 10 80% 4 100%
mageekguy\atoum\writers\file::closeFile() 33 100% 7 100% 4 100% 2 100%
#
1
<?php
2

                    
3
namespace mageekguy\atoum\writers;
4

                    
5
use
6
	mageekguy\atoum,
7
	mageekguy\atoum\reports,
8
	mageekguy\atoum\exceptions,
9
	mageekguy\atoum\report\writers
10
;
11

                    
12
class file extends atoum\writer implements writers\realtime, writers\asynchronous
13
{
14
	protected $filename = null;
15

                    
16
	private $resource = null;
17

                    
18
	const defaultFileName = 'atoum.log';
19

                    
20
	public function __construct($filename = null, atoum\adapter $adapter = null)100%
21
	{
22
		parent::__construct($adapter);
23

                    
24
		$this->setFilename($filename);
25
	}
26

                    
27
	public function __destruct()100%
28
	{
29
		$this->closeFile();
30
	}
31

                    
32
	public function clear()100%
33
	{
34
		if ($this->openFile()->adapter->ftruncate($this->resource, 0) === false)
35
		{
36
			throw new exceptions\runtime('Unable to truncate file \'' . $this->filename . '\'');
37
		}
38

                    
39
		return $this;
40
	}
41

                    
42
	public function writeRealtimeReport(reports\realtime $report, $event)0%
43
	{
44
		return $this->write((string) $report);
45
	}
46

                    
47
	public function writeAsynchronousReport(reports\asynchronous $report)0%
48
	{
49
		return $this->write((string) $report)->closeFile();
50
	}
51

                    
52
	public function setFilename($filename = null)100%
53
	{
54
		$this->closeFile()->filename = $filename ?: self::defaultFileName;
55

                    
56
		return $this;
57
	}
58

                    
59
	public function getFilename()100%
60
	{
61
		return $this->filename;
62
	}
63

                    
64
	public function reset()0%
65
	{
66
		$this->closeFile();
67

                    
68
		return parent::reset();
69
	}
70

                    
71
	protected function doWrite($something)100%
72
	{
73
		if (strlen($something) != $this->openFile()->adapter->fwrite($this->resource, $something))
74
		{
75
			throw new exceptions\runtime('Unable to write in file \'' . $this->filename . '\'');
76
		}
77

                    
78
		$this->adapter->fflush($this->resource);
79

                    
80
		return $this;
81
	}
82

                    
83
	private function openFile()100%
84
	{
85
		if ($this->resource === null)
86
		{
87
			$this->resource = @$this->adapter->fopen($this->filename, 'c') ?: null;
88

                    
89
			if ($this->resource === null)
90
			{
91
				throw new exceptions\runtime('Unable to open file \'' . $this->filename . '\'');
92
			}
93

                    
94
			if ($this->adapter->flock($this->resource, LOCK_EX) === false)
95
			{
96
				throw new exceptions\runtime('Unable to lock file \'' . $this->filename . '\'');
97
			}
98

                    
99
			$this->clear();
100
		}
101

                    
102
		return $this;
103
	}
104

                    
105
	private function closeFile()100%
106
	{
107
		if ($this->resource !== null)
108
		{
109
			$this->adapter->flock($this->resource, LOCK_UN);
110
			$this->adapter->fclose($this->resource);
111

                    
112
			$this->resource = null;
113
		}
114

                    
115
		return $this;
116
	}
117
}