TestResult   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
lcom 1
cbo 3
dl 0
loc 215
ccs 60
cts 60
cp 1
rs 10
c 1
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 9 1
A __toString() 0 15 1
A getId() 0 4 1
A setStatus() 0 7 1
A getStatus() 0 4 1
A isUnsuccessful() 0 4 1
A setCreatedAt() 0 12 3
A getCreatedAt() 0 4 1
A setTest() 0 7 1
A getTest() 0 4 1
A setInfo() 0 10 2
A getInfo() 0 4 1
A isAChange() 0 15 3
1
<?php
2
3
namespace Overwatch\ResultBundle\Entity;
4
5
use Doctrine\ORM\Event\LifecycleEventArgs;
6
use Doctrine\ORM\Mapping as ORM;
7
use Overwatch\ResultBundle\Enum\ResultStatus;
8
use Overwatch\TestBundle\Entity\Test;
9
10
/**
11
 * TestResult
12
 *
13
 * @ORM\Table()
14
 * @ORM\Entity(readOnly=true,repositoryClass="Overwatch\ResultBundle\Entity\TestResultRepository")
15
 * @ORM\HasLifecycleCallbacks
16
 */
17
class TestResult implements \JsonSerializable
18
{
19
    /**
20
     * @var integer
21
     *
22
     * @ORM\Column(name="id", type="integer")
23
     * @ORM\Id
24
     * @ORM\GeneratedValue(strategy="AUTO")
25
     */
26
    private $id;
27
28
    /**
29
     * @ORM\ManyToOne(targetEntity="Overwatch\TestBundle\Entity\Test", inversedBy="results")
30
     * @ORM\JoinColumn(name="test_id", referencedColumnName="id", onDelete="CASCADE")
31
     */
32
    private $test;
33
34
    /**
35
     * @var string
36
     *
37
     * @ORM\Column(name="status", type="string", length=15)
38
     */
39
    private $status;
40
41
    /**
42
     * @var string
43
     *
44
     * @ORM\Column(name="info", type="string", length=100)
45
     */
46
    private $info;
47
48
    /**
49
     * @var \DateTime
50
     *
51
     * @ORM\Column(name="created_at", type="datetime")
52
     */
53
    private $createdAt;
54
55
    /**
56
     * Serialise object to JSON
57
     */
58 14
    public function jsonSerialize()
59
    {
60
        return [
61 14
            'id'        => $this->getId(),
62 14
            'status'    => $this->getStatus(),
63 14
            'info'      => $this->getInfo(),
64 14
            'createdAt' => $this->getCreatedAt()->getTimestamp()
65 14
        ];
66
    }
67
68
    /**
69
     * Serialise object to string
70
     */
71 2
    public function __toString()
72
    {
73 2
        $test = $this->getTest();
74
75 2
        return sprintf(
76 2
            '[%s] %s %s (Expect %s %s %s - %s)',
77 2
            $this->getCreatedAt()->format('Y-m-d H:i:s'),
78 2
            $test->getName(),
79 2
            strtoupper($this->getStatus()),
80 2
            $test->getActual(),
81 2
            $test->getExpectation(),
82 2
            $test->getExpected(),
83 2
            $this->getInfo()
84 2
        );
85
    }
86
87
    /**
88
     * Get id
89
     *
90
     * @return integer
91
     */
92 21
    public function getId()
93
    {
94 21
        return $this->id;
95
    }
96
97
    /**
98
     * Set result
99
     *
100
     * @param string $status
101
     * @return TestResult
102
     */
103 142
    public function setStatus($status)
104
    {
105 142
        ResultStatus::isValid($status);
106 141
        $this->status = $status;
107
108 141
        return $this;
109
    }
110
111
    /**
112
     * Get result
113
     *
114
     * @return string
115
     */
116 138
    public function getStatus()
117
    {
118 138
        return $this->status;
119
    }
120
121
    /**
122
     * Was the result unsucessful?
123
     *
124
     * @return bool
125
     */
126 4
    public function isUnsuccessful()
127
    {
128 4
        return in_array($this->getStatus(), [ResultStatus::ERROR, ResultStatus::FAILED]);
129
    }
130
131
    /**
132
     * Set createdAt
133
     *
134
     * @ORM\PrePersist
135
     * @return TestResult
136
     */
137 129
    public function setCreatedAt($timestamp = 'now')
138
    {
139 129
        if ($this->createdAt === null) {
140 129
            if ($timestamp instanceof LifecycleEventArgs) {
141 7
                $timestamp = 'now';
142 7
            }
143
144 129
            $this->createdAt = new \DateTime($timestamp);
145 129
        }
146
147 129
        return $this;
148
    }
149
150
    /**
151
     * Get createdAt
152
     *
153
     * @return \DateTime
154
     */
155 129
    public function getCreatedAt()
156
    {
157 129
        return $this->createdAt;
158
    }
159
160
    /**
161
     * Set test
162
     *
163
     * @param \Overwatch\TestBundle\Entity\Test $test
164
     * @return TestResult
165
     */
166 135
    public function setTest(Test $test)
167
    {
168 135
        $this->test = $test;
169 135
        $test->setLastResult($this);
170
171 135
        return $this;
172
    }
173
174
    /**
175
     * Get test
176
     *
177
     * @return \Overwatch\TestBundle\Entity\Test
178
     */
179 136
    public function getTest()
180
    {
181 136
        return $this->test;
182
    }
183
184
    /**
185
     * Set info
186
     *
187
     * @param string $info
188
     * @return TestResult
189
     */
190 141
    public function setInfo($info)
191
    {
192 141
        if ($info instanceof \Exception) {
193 1
            $info = $info->getMessage();
194 1
        }
195
196 141
        $this->info = $info;
197
198 141
        return $this;
199
    }
200
201
    /**
202
     * Get info
203
     *
204
     * @return string
205
     */
206 134
    public function getInfo()
207
    {
208 134
        return $this->info;
209
    }
210
211
    /**
212
     * Is this test result a change from the previous one?
213
     *
214
     * @return boolean
215
     */
216 3
    public function isAChange()
217
    {
218 3
        if ($this->getTest() === null) {
219 1
            return true;
220
        }
221
222 2
        $results = $this->getTest()->getResults();
223 2
        $lastResult = $results->get($results->count() - 2);
224
225 2
        if ($lastResult === null) {
226 1
            return true;
227
        }
228
229 1
        return ($this->getStatus() !== $lastResult->getStatus());
230
    }
231
}
232