Failed Conditions
Pull Request — master (#142)
by Zac
04:15
created

TestResult::setCreatedAt()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 12
ccs 6
cts 8
cp 0.75
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3.1406
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
     */
31
    private $test;
32
33
    /**
34
     * @var string
35
     *
36
     * @ORM\Column(name="status", type="string", length=15)
37
     */
38
    private $status;
39
40
    /**
41
     * @var string
42
     *
43
     * @ORM\Column(name="info", type="string", length=100)
44
     */
45
    private $info;
46
47
    /**
48
     * @var \DateTime
49
     *
50
     * @ORM\Column(name="created_at", type="datetime")
51
     */
52
    private $createdAt;
53
54
    /**
55
     * Serialise object to JSON
56
     */
57 1
    public function jsonSerialize()
58
    {
59
        return [
60 1
            'id'        => $this->getId(),
61 1
            'status'    => $this->getStatus(),
62 1
            'info'      => $this->getInfo(),
63 1
            'createdAt' => $this->getCreatedAt()->getTimestamp()
64 1
        ];
65
    }
66
67
    /**
68
     * Serialise object to string
69
     */
70
    public function __toString()
71
    {
72
        $test = $this->getTest();
73
74
        return sprintf(
75
            '[%s] %s %s (Expect %s %s %s - %s)',
76
            $this->getCreatedAt()->format('Y-m-d H:i:s'),
77
            $test->getName(),
78
            strtoupper($this->getStatus()),
79
            $test->getActual(),
80
            $test->getExpectation(),
81
            $test->getExpected(),
82
            $this->getInfo()
83
        );
84
    }
85
86
    /**
87
     * Get id
88
     *
89
     * @return integer
90
     */
91 1
    public function getId()
92
    {
93 1
        return $this->id;
94
    }
95
96
    /**
97
     * Set result
98
     *
99
     * @param string $status
100
     * @return TestResult
101
     */
102 16
    public function setStatus($status)
103
    {
104 16
        ResultStatus::isValid($status);
105 15
        $this->status = $status;
106
107 15
        return $this;
108
    }
109
110
    /**
111
     * Get result
112
     *
113
     * @return string
114
     */
115 12
    public function getStatus()
116
    {
117 12
        return $this->status;
118
    }
119
120
    /**
121
     * Was the result unsucessful?
122
     *
123
     * @return bool
124
     */
125 4
    public function isUnsuccessful()
126
    {
127 4
        return in_array($this->getStatus(), [ResultStatus::ERROR, ResultStatus::FAILED]);
128
    }
129
130
    /**
131
     * Set createdAt
132
     *
133
     * @ORM\PrePersist
134
     * @return TestResult
135
     */
136 3
    public function setCreatedAt($timestamp = 'now')
137
    {
138 3
        if ($this->createdAt === null) {
139 3
            if ($timestamp instanceof LifecycleEventArgs) {
140
                $timestamp = 'now';
141
            }
142
143 3
            $this->createdAt = new \DateTime($timestamp);
144 3
        }
145
146 3
        return $this;
147
    }
148
149
    /**
150
     * Get createdAt
151
     *
152
     * @return \DateTime
153
     */
154 3
    public function getCreatedAt()
155
    {
156 3
        return $this->createdAt;
157
    }
158
159
    /**
160
     * Set test
161
     *
162
     * @param \Overwatch\TestBundle\Entity\Test $test
163
     * @return TestResult
164
     */
165 9
    public function setTest(Test $test = null)
166
    {
167 9
        $this->test = $test;
168 9
        $test->setLastResult($this);
0 ignored issues
show
Bug introduced by
It seems like $test is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
169
170 9
        return $this;
171
    }
172
173
    /**
174
     * Get test
175
     *
176
     * @return \Overwatch\TestBundle\Entity\Test
177
     */
178 10
    public function getTest()
179
    {
180 10
        return $this->test;
181
    }
182
183
    /**
184
     * Set info
185
     *
186
     * @param string $info
187
     * @return TestResult
188
     */
189 15
    public function setInfo($info)
190
    {
191 15
        if ($info instanceof \Exception) {
192 1
            $info = $info->getMessage();
193 1
        }
194
195 15
        $this->info = $info;
196
197 15
        return $this;
198
    }
199
200
    /**
201
     * Get info
202
     *
203
     * @return string
204
     */
205 8
    public function getInfo()
206
    {
207 8
        return $this->info;
208
    }
209
210
    /**
211
     * Is this test result a change from the previous one?
212
     *
213
     * @return boolean
214
     */
215 3
    public function isAChange()
216
    {
217 3
        if ($this->getTest() === null) {
218 1
            return true;
219
        }
220
221 2
        $results = $this->getTest()->getResults();
222 2
        $lastResult = $results->get($results->count() - 2);
223
224 2
        if ($lastResult === null) {
225 1
            return true;
226
        }
227
228 1
        return ($this->getStatus() !== $lastResult->getStatus());
229
    }
230
}
231