Suite::getFailed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (c) 2017 Francois-Xavier Soubirou.
4
 *
5
 * This file is part of ci-report.
6
 *
7
 * ci-report is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * ci-report is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with ci-report. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
declare(strict_types=1);
21
22
namespace App\Entity;
23
24
use App\DTO\SuiteDTO;
25
use App\DTO\SuiteLimitsDTO;
26
use DateTime;
27
use Doctrine\ORM\Mapping as ORM;
28
use Gedmo\Mapping\Annotation as Gedmo;
29
use JMS\Serializer\Annotation as Serializer;
30
use Symfony\Component\Validator\Constraints as Assert;
31
32
/**
33
 * Suite entity class.
34
 *
35
 * @category  ci-report app
36
 *
37
 * @author    Francois-Xavier Soubirou <[email protected]>
38
 * @copyright 2017 Francois-Xavier Soubirou
39
 * @license   http://www.gnu.org/licenses/   GPLv3
40
 *
41
 * @see      https://www.ci-report.io
42
 *
43
 * @ORM\Table(name="cir_suite")
44
 * @ORM\Entity(repositoryClass="App\Repository\SuiteRepository")
45
 */
46
class Suite
47
{
48
    const DEFAULT_NAME = 'DEFAULT_SUITE_NAME';
49
50
    /**
51
     * @var int
52
     *
53
     * @ORM\Column(name="id", type="integer")
54
     * @ORM\Id
55
     * @ORM\GeneratedValue(strategy="AUTO")
56
     *
57
     * @Serializer\Exclude
58
     */
59
    private $id;
60
61
    /**
62
     * Name of the suite.
63
     *
64
     * @var string
65
     *
66
     * @ORM\Column(name="name", type="string", length=50)
67
     *
68
     * @Assert\NotBlank()
69
     *
70
     * @Serializer\Groups({"public", "private"})
71
     */
72
    private $name;
73
74
    /**
75
     * Tests warning limit.
76
     *
77
     * @var int
78
     *
79
     * @ORM\Column(name="warning", type="smallint")
80
     *
81
     * @Assert\NotBlank()
82
     * @Assert\Range(min=0, max=100)
83
     *
84
     * @Serializer\Groups({"public", "private"})
85
     */
86
    private $warning;
87
88
    /**
89
     * Tests success limit.
90
     *
91
     * @var int
92
     *
93
     * @ORM\Column(name="success", type="smallint")
94
     *
95
     * @Assert\NotBlank()
96
     * @Assert\Range(min=0, max=100)
97
     *
98
     * @Serializer\Groups({"public", "private"})
99
     */
100
    private $success;
101
102
    /**
103
     * Total number of passed tests.
104
     *
105
     * @var int
106
     *
107
     * @ORM\Column(name="passed", type="integer")
108
     *
109
     * @Assert\NotBlank()
110
     * @Assert\Type("integer")
111
     *
112
     * @Serializer\Groups({"public", "private"})
113
     */
114
    private $passed = 0;
115
116
    /**
117
     * Total number of disabled tests.
118
     *
119
     * @var int
120
     *
121
     * @ORM\Column(name="failed", type="integer")
122
     *
123
     * @Assert\NotBlank()
124
     * @Assert\Type("integer")
125
     *
126
     * @Serializer\Groups({"public", "private"})
127
     */
128
    private $failed = 0;
129
130
    /**
131
     * Total number of errored tests.
132
     *
133
     * @var int
134
     *
135
     * @ORM\Column(name="errored", type="integer")
136
     *
137
     * @Assert\NotBlank()
138
     * @Assert\Type("integer")
139
     *
140
     * @Serializer\Groups({"public", "private"})
141
     */
142
    private $errored = 0;
143
144
    /**
145
     * Total number of skipped tests.
146
     *
147
     * @var int
148
     *
149
     * @ORM\Column(name="skipped", type="integer")
150
     *
151
     * @Assert\NotBlank()
152
     * @Assert\Type("integer")
153
     *
154
     * @Serializer\Groups({"public", "private"})
155
     */
156
    private $skipped = 0;
157
158
    /**
159
     * Total number of disabled tests.
160
     *
161
     * @var int
162
     *
163
     * @ORM\Column(name="disabled", type="integer")
164
     *
165
     * @Assert\NotBlank()
166
     * @Assert\Type("integer")
167
     *
168
     * @Serializer\Groups({"public", "private"})
169
     */
170
    private $disabled = 0;
171
172
    /**
173
     * Duration of the suite in seconds.
174
     *
175
     * @var float
176
     *
177
     * @ORM\Column(name="duration", type="float")
178
     *
179
     * @Assert\NotBlank()
180
     * @Assert\Type("float")
181
     *
182
     * @Serializer\Groups({"public", "private"})
183
     */
184
    private $duration = 0;
185
186
    /**
187
     * Date time of the suite in ISO 8601 format (2017-07-01T12:30:01+02:00).
188
     *
189
     * @var DateTime
190
     *
191
     * @ORM\Column(name="datetime_suite", type="datetime")
192
     *
193
     * @Assert\NotBlank()
194
     * @Assert\DateTime()
195
     *
196
     * @Serializer\Groups({"public", "private"})
197
     */
198
    protected $datetime;
199
200
    /**
201
     * UID of attach zip documents.
202
     *
203
     * @var string
204
     *
205
     * @ORM\Column(name="doc_uid", type="string", length=50, nullable=true)
206
     *
207
     * @Serializer\Exclude
208
     */
209
    private $documentUid;
210
211
    /**
212
     * @Gedmo\SortablePosition
213
     * @ORM\Column(name="position", type="integer")
214
     *
215
     * @Serializer\Exclude
216
     */
217
    private $position;
218
219
    /**
220
     * @var Campaign
221
     *
222
     * @Gedmo\SortableGroup
223
     * @ORM\ManyToOne(targetEntity="Campaign")
224
     * @ORM\JoinColumn(name="campaign_id", referencedColumnName="id", nullable=false, onDelete="cascade")
225
     *
226
     * @Serializer\Exclude
227
     */
228
    private $campaign;
229
230
    /**
231
     * Constructor.
232
     *
233
     * @param Project  $project
234
     * @param Campaign $campaign
235
     */
236
    public function __construct(Project $project, Campaign $campaign)
237
    {
238
        $this->setWarning($project->getWarning());
239
        $this->setSuccess($project->getSuccess());
240
        $this->setCampaign($campaign);
241
    }
242
243
    /**
244
     * Get id.
245
     *
246
     * @return int
247
     */
248
    public function getId(): int
249
    {
250
        return $this->id;
251
    }
252
253
    /**
254
     * Set name.
255
     *
256
     * @param string $name Name
257
     *
258
     * @return Suite
259
     */
260
    public function setName(string $name): self
261
    {
262
        $this->name = $name;
263
264
        return $this;
265
    }
266
267
    /**
268
     * Get name.
269
     *
270
     * @return string
271
     */
272
    public function getName(): string
273
    {
274
        return $this->name;
275
    }
276
277
    /**
278
     * Set warning limit.
279
     *
280
     * @param int $warning Warning limit
281
     *
282
     * @return Suite
283
     */
284
    public function setWarning(int $warning): self
285
    {
286
        $this->warning = $warning;
287
288
        return $this;
289
    }
290
291
    /**
292
     * Get warning limit.
293
     *
294
     * @return int
295
     */
296
    public function getWarning(): int
297
    {
298
        return $this->warning;
299
    }
300
301
    /**
302
     * Set success limit.
303
     *
304
     * @param int $success Success limit
305
     *
306
     * @return Suite
307
     */
308
    public function setSuccess(int $success): self
309
    {
310
        $this->success = $success;
311
312
        return $this;
313
    }
314
315
    /**
316
     * Get success limit.
317
     *
318
     * @return int
319
     */
320
    public function getSuccess(): int
321
    {
322
        return $this->success;
323
    }
324
325
    /**
326
     * Set passed tests count.
327
     *
328
     * @param int $passed Passed tests
329
     *
330
     * @return Suite
331
     */
332
    public function setpassed(int $passed): self
333
    {
334
        $this->passed = $passed;
335
336
        return $this;
337
    }
338
339
    /**
340
     * Get passed tests count.
341
     *
342
     * @return int
343
     */
344
    public function getPassed(): int
345
    {
346
        return $this->passed;
347
    }
348
349
    /**
350
     * Set failed tests count.
351
     *
352
     * @param int $failed Failed tests
353
     *
354
     * @return Suite
355
     */
356
    public function setFailed(int $failed): self
357
    {
358
        $this->failed = $failed;
359
360
        return $this;
361
    }
362
363
    /**
364
     * Get failed tests count.
365
     *
366
     * @return int
367
     */
368
    public function getFailed(): int
369
    {
370
        return $this->failed;
371
    }
372
373
    /**
374
     * Set errored tests count.
375
     *
376
     * @param int $errored Errored tests
377
     *
378
     * @return Suite
379
     */
380
    public function setErrored(int $errored): self
381
    {
382
        $this->errored = $errored;
383
384
        return $this;
385
    }
386
387
    /**
388
     * Get errored tests count.
389
     *
390
     * @return int
391
     */
392
    public function getErrored(): int
393
    {
394
        return $this->errored;
395
    }
396
397
    /**
398
     * Set skipped tests count.
399
     *
400
     * @param int $skipped Skipped tests
401
     *
402
     * @return Suite
403
     */
404
    public function setSkipped(int $skipped): self
405
    {
406
        $this->skipped = $skipped;
407
408
        return $this;
409
    }
410
411
    /**
412
     * Get skipped tests count.
413
     *
414
     * @return int
415
     */
416
    public function getSkipped(): int
417
    {
418
        return $this->skipped;
419
    }
420
421
    /**
422
     * Set disabled tests count.
423
     *
424
     * @param int $disabled Disable tests
425
     *
426
     * @return Suite
427
     */
428
    public function setDisabled(int $disabled): self
429
    {
430
        $this->disabled = $disabled;
431
432
        return $this;
433
    }
434
435
    /**
436
     * Get disabled tests count.
437
     *
438
     * @return int
439
     */
440
    public function getDisabled(): int
441
    {
442
        return $this->disabled;
443
    }
444
445
    /**
446
     * Set duration of the suite in second.
447
     *
448
     * @param float $duration Duration
449
     *
450
     * @return Suite
451
     */
452
    public function setDuration(float $duration): self
453
    {
454
        $this->duration = $duration;
455
456
        return $this;
457
    }
458
459
    /**
460
     * Get duration of the suite in seconds.
461
     *
462
     * @return float
463
     */
464
    public function getDuration(): float
465
    {
466
        return $this->duration;
467
    }
468
469
    /**
470
     * Set datetime of suite.
471
     *
472
     * @param DateTime $datetime DateTime of suite.
473
     *
474
     * @return Suite
475
     */
476
    public function setDateTime(DateTime $datetime): self
477
    {
478
        $this->datetime = $datetime;
479
480
        return $this;
481
    }
482
483
    /**
484
     * Get datetime of suite.
485
     *
486
     * @return DateTime
487
     */
488
    public function getDateTime(): DateTime
489
    {
490
        return $this->datetime;
491
    }
492
493
    /**
494
     * Set order.
495
     *
496
     * @param int $position The order.
497
     *
498
     * @return Suite
499
     */
500
    public function setPosition(int $position): self
501
    {
502
        $this->position = $position;
503
504
        return $this;
505
    }
506
507
    /**
508
     * Set document Uid.
509
     *
510
     * @param string $uid Document Uid
511
     *
512
     * @return Suite
513
     */
514
    public function setDocumentUid(?string $uid): self
515
    {
516
        $this->documentUid = $uid;
517
518
        return $this;
519
    }
520
521
    /**
522
     * Get document Uid.
523
     *
524
     * @return string|null
525
     */
526
    public function getDocumentUid(): ?string
527
    {
528
        return $this->documentUid;
529
    }
530
531
    /**
532
     * Get position.
533
     *
534
     * @return int
535
     */
536
    public function getPosition(): int
537
    {
538
        return $this->position;
539
    }
540
541
    /**
542
     * Get reference id (Incremental integer).
543
     *
544
     * @return int
545
     *
546
     * @Serializer\VirtualProperty
547
     * @Serializer\SerializedName("refid")
548
     * @Serializer\Type("int")
549
     * @Serializer\Groups({"public", "private"})
550
     */
551
    public function getRefid(): int
552
    {
553
        return $this->position + 1;
554
    }
555
556
    /**
557
     * Set campaign.
558
     *
559
     * @param Campaign $campaign Campaign
560
     *
561
     * @return Suite
562
     */
563
    public function setCampaign(Campaign $campaign): self
564
    {
565
        $this->campaign = $campaign;
566
567
        return $this;
568
    }
569
570
    /**
571
     * Get campaign.
572
     *
573
     * @return Campaign
574
     */
575
    public function getCampaign(): Campaign
576
    {
577
        return $this->campaign;
578
    }
579
580
    /**
581
     * Get enabled tests.
582
     *
583
     * @return int
584
     */
585
    public function getEnabled(): int
586
    {
587
        return $this->passed
588
            + $this->failed
589
            + $this->errored
590
            + $this->skipped;
591
    }
592
593
    /**
594
     * Get percentage of successful tests.
595
     *
596
     * @return float
597
     */
598
    public function getPercentage(): float
599
    {
600
        if ($this->getEnabled() > 0) {
601
            return round($this->passed / $this->getEnabled() * 100);
602
        }
603
604
        return 0;
605
    }
606
607
    /**
608
     * Get suite status.
609
     *
610
     * @return int Status::FAILED|Status::WARNING|Status::SUCCESS
611
     */
612
    public function getStatus(): int
613
    {
614
        if (0 === $this->getEnabled()) {
615
            if ((0 === $this->getWarning()) && (0 === $this->getSuccess())) {
616
                return Status::SUCCESS;
617
            }
618
619
            return Status::UNKNOWN;
620
        }
621
        if ($this->getPercentage() < $this->getWarning()) {
622
            return Status::FAILED;
623
        }
624
        if ($this->getPercentage() < $this->getSuccess()) {
625
            return Status::WARNING;
626
        }
627
628
        return Status::SUCCESS;
629
    }
630
631
    /**
632
     * Set from DTO limits suite.
633
     *
634
     * @param SuiteLimitsDTO $dto DTO object
635
     *
636
     * @return Suite
637
     */
638
    public function setFromLimitsDTO(SuiteLimitsDTO $dto): self
639
    {
640
        if (null !== $dto->getWarning()) {
0 ignored issues
show
introduced by
The condition null !== $dto->getWarning() is always true.
Loading history...
641
            $this->setWarning($dto->getWarning());
642
        } else {
643
            $project = $this->getCampaign()->getProject();
644
            $this->setWarning($project->getWarning());
645
        }
646
        if (null !== $dto->getSuccess()) {
0 ignored issues
show
introduced by
The condition null !== $dto->getSuccess() is always true.
Loading history...
647
            $this->setSuccess($dto->getSuccess());
648
        } else {
649
            $project = $this->getCampaign()->getProject();
650
            $this->setSuccess($project->getSuccess());
651
        }
652
653
        return $this;
654
    }
655
656
    /**
657
     * Set from DTO suite.
658
     *
659
     * @param SuiteDTO $dto DTO object
660
     *
661
     * @return Suite
662
     */
663
    public function setFromDTO(SuiteDTO $dto): self
664
    {
665
        $this->setFromLimitsDTO($dto);
666
667
        $this->setName($dto->getName());
668
        $this->setDisabled($dto->getDisabled());
669
        $this->setDuration($dto->getDuration());
670
        $this->setDateTime($dto->getDatetime());
671
672
        return $this;
673
    }
674
}
675