mageekguy\atoum\reports\asynchronous\coveralls: lines coverage

83% of 433

OPs

98% of 86

Lines

71% of 59

Branches

50% of 34

Paths
Method OPs OPs % Lines Line % Branches Branches % Paths Path %
mageekguy\atoum\reports\asynchronous\coveralls::__construct() 57 98% 11 100% 4 50% 2 100%
mageekguy\atoum\reports\asynchronous\coveralls::setBranchFinder() 15 100% 3 100% 1 100% 1 100%
mageekguy\atoum\reports\asynchronous\coveralls::getBranchFinder() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\reports\asynchronous\coveralls::setServiceName() 13 100% 3 100% 1 100% 1 100%
mageekguy\atoum\reports\asynchronous\coveralls::getServiceName() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\reports\asynchronous\coveralls::setServiceJobId() 9 100% 2 100% 1 100% 1 100%
mageekguy\atoum\reports\asynchronous\coveralls::getServiceJobId() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\reports\asynchronous\coveralls::addDefaultWriter() 51 100% 6 100% 1 100% 1 100%
mageekguy\atoum\reports\asynchronous\coveralls::getSourceDir() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\reports\asynchronous\coveralls::handleEvent() 33 73% 5 60% 8 25% 3 33%
mageekguy\atoum\reports\asynchronous\coveralls::build() 31 100% 6 100% 4 75% 2 50%
mageekguy\atoum\reports\asynchronous\coveralls::makeRootElement() 33 100% 8 100% 1 100% 1 100%
mageekguy\atoum\reports\asynchronous\coveralls::makeGitElement() 40 100% 8 100% 4 100% 2 100%
mageekguy\atoum\reports\asynchronous\coveralls::makeSourceElement() 72 25% 11 100% 6 67% 3 33%
mageekguy\atoum\reports\asynchronous\coveralls::makeCoverageElement() 55 82% 19 100% 24 75% 13 8%
#
1
<?php
2

                    
3
namespace mageekguy\atoum\reports\asynchronous;
4

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

                    
12
class coveralls extends atoum\reports\asynchronous
13
{
14
	const defaultServiceName = 'atoum';
15
	const defaultEvent = 'manual';
16
	const defaultCoverallsApiUrl = 'https://coveralls.io/api/v1/jobs';
17
	const defaultCoverallsApiMethod = 'POST';
18
	const defaultCoverallsApiParameter = 'json';
19

                    
20
	protected $sourceDir = null;
21
	protected $repositoryToken = null;
22
	protected $score = null;
23
	protected $branchFinder;
24
	protected $serviceName;
25
	protected $serviceJobId;
26

                    
27
	public function __construct($sourceDir, $repositoryToken, atoum\adapter $adapter = null)100%
28
	{
29
		parent::__construct();
30

                    
31
		$this
32
			->setAdapter($adapter)
33
			->setBranchFinder()
34
			->setServiceName()
35
		;
36

                    
37
		if ($this->adapter->extension_loaded('json') === false)
38
		{
39
			throw new exceptions\runtime('JSON PHP extension is mandatory for coveralls report');
40
		}
41

                    
42
		$this->repositoryToken = $repositoryToken;
43
		$this->sourceDir = new atoum\fs\path($sourceDir);
44
	}
45

                    
46
	public function setBranchFinder(\closure $finder = null)100%
47
	{
48
		$adapter = $this->adapter;
49

                    
50
		$this->branchFinder = $finder ?: function() use ($adapter) { return $adapter->exec('git rev-parse --abbrev-ref HEAD'); };
51

                    
52
		return $this;
53
	}
54

                    
55
	public function getBranchFinder()100%
56
	{
57
		return $this->branchFinder;
58
	}
59

                    
60
	public function setServiceName($name = null)100%
61
	{
62
		$this->serviceName = $name ?: static::defaultServiceName;
63

                    
64
		return $this;
65
	}
66

                    
67
	public function getServiceName()100%
68
	{
69
		return $this->serviceName;
70
	}
71

                    
72
	public function setServiceJobId($id = null)100%
73
	{
74
		$this->serviceJobId = $id;
75

                    
76
		return $this;
77
	}
78

                    
79
	public function getServiceJobId()100%
80
	{
81
		return $this->serviceJobId;
82
	}
83

                    
84
	public function addDefaultWriter(atoum\writers\http $writer = null)100%
85
	{
86
		$writer = $writer ?: new atoum\writers\http($this->adapter);
87
		$writer
88
			->setUrl(static::defaultCoverallsApiUrl)
89
			->setMethod(static::defaultCoverallsApiMethod)
90
			->setParameter(static::defaultCoverallsApiParameter)
91
			->addHeader('Content-Type', 'multipart/form-data')
92
		;
93

                    
94
		return parent::addWriter($writer);
95
	}
96

                    
97
	public function getSourceDir()100%
98
	{
99
		return $this->sourceDir;
100
	}
101

                    
102
	public function handleEvent($event, atoum\observable $observable)60%
103
	{
104
		$this->score = ($event !== atoum\runner::runStop ? null : $observable->getScore());
105

                    
106
		try
107
		{
108
			return parent::handleEvent($event, $observable);
109
		}
110
		catch (atoum\writers\http\exception $exception)
111
		{
112
			return $this;
113
		}
114
	}
115

                    
116
	public function build($event)100%
117
	{
118
		if ($event === atoum\runner::runStop)
119
		{
120
			$coverage = $this->makeRootElement($this->score->getCoverage());
121
			$this->string = json_encode($coverage);
122
		}
123

                    
124
		return $this;
125
	}
126

                    
127
	protected function makeRootElement(score\coverage $coverage)100%
128
	{
129
		return array(
130
			'service_name' => $this->serviceName,
131
			'service_event_type' => static::defaultEvent,
132
			'service_job_id' => $this->serviceJobId,
133
			'repo_token' => $this->repositoryToken,
134
			'run_at' => $this->adapter->date('Y-m-d H:i:s O'),
135
			'source_files' => $this->makeSourceElement($coverage),
136
			'git' => $this->makeGitElement()
137
		);
138
	}
139

                    
140
	protected function makeGitElement()100%
141
	{
142
		$head = $this->adapter->exec('git log -1 --pretty=format:\'{"id":"%H","author_name":"%aN","author_email":"%ae","committer_name":"%cN","committer_email":"%ce","message":"%s"}\'');
143
		$infos = array('head' => json_decode($head));
144

                    
145
		$branch = call_user_func($this->getBranchFinder());
146
		if ($branch != null)
147
		{
148
			$infos['branch'] = $branch;
149
		}
150

                    
151
		return $infos;
152
	}
153

                    
154
	protected function makeSourceElement(score\coverage $coverage)100%
155
	{
156
		$sources = array();
157

                    
158
		foreach ($coverage->getClasses() as $class => $file)
159
		{
160
			$path = new atoum\fs\path($file);
161
			$source = $this->adapter->file_get_contents((string) $path->resolve());
162

                    
163
			$sources[] = array(
164
				'name' => ltrim((string) $path->relativizeFrom($this->sourceDir), './'),
165
				'source' => $source,
166
				'coverage' => $this->makeCoverageElement($coverage->getCoverageForClass($class))
167
			);
168
		}
169

                    
170
		return $sources;
171
	}
172

                    
173
	protected function makeCoverageElement(array $coverage)100%
174
	{
175
		$cover = array();
176

                    
177
		foreach ($coverage as $method)
178
		{
179
			foreach ($method as $number => $line)
180
			{
181
				if ($number > 1)
182
				{
183
					while (sizeof($cover) < ($number - 1))
184
					{
185
						$cover[] = null;
186
					}
187
				}
188

                    
189
				if ($line === 1)
190
				{
191
					$cover[] = 1;
192
				}
193
				elseif ($line >= -1)
194
				{
195
					$cover[] = 0;
196
				}
197
			}
198
		}
199

                    
200
		return $cover;
201
	}
202
}