Completed
Push — master ( 14bf01...b7c3ef )
by FX
04:41
created

Suite::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 9.4285
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 AppBundle\Entity;
23
24
use AppBundle\DTO\SuiteDTO;
25
use AppBundle\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="AppBundle\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
     * @Gedmo\SortablePosition
202
     * @ORM\Column(name="position", type="integer")
203
     *
204
     * @Serializer\Exclude
205
     */
206
    private $position;
207
208
    /**
209
     * @var Campaign
210
     *
211
     * @Gedmo\SortableGroup
212
     * @ORM\ManyToOne(targetEntity="Campaign")
213
     * @ORM\JoinColumn(name="campaign_id", referencedColumnName="id", nullable=false, onDelete="cascade")
214
     *
215
     * @Serializer\Exclude
216
     */
217
    private $campaign;
218
219
    /**
220
     * Constructor.
221
     *
222
     * @param Project  $project
223
     * @param Campaign $campaign
224
     */
225
    public function __construct(Project $project, Campaign $campaign)
226
    {
227
        $this->setWarning($project->getWarning());
228
        $this->setSuccess($project->getSuccess());
229
        $this->setCampaign($campaign);
230
    }
231
232
    /**
233
     * Get id.
234
     *
235
     * @return int
236
     */
237
    public function getId(): int
238
    {
239
        return $this->id;
240
    }
241
242
    /**
243
     * Set name.
244
     *
245
     * @param string $name Name
246
     *
247
     * @return Suite
248
     */
249
    public function setName(string $name): Suite
250
    {
251
        $this->name = $name;
252
253
        return $this;
254
    }
255
256
    /**
257
     * Get name.
258
     *
259
     * @return string
260
     */
261
    public function getName(): string
262
    {
263
        return $this->name;
264
    }
265
266
    /**
267
     * Set warning limit.
268
     *
269
     * @param int $warning Warning limit
270
     *
271
     * @return Suite
272
     */
273
    public function setWarning(int $warning): Suite
274
    {
275
        $this->warning = $warning;
276
277
        return $this;
278
    }
279
280
    /**
281
     * Get warning limit.
282
     *
283
     * @return int
284
     */
285
    public function getWarning(): int
286
    {
287
        return $this->warning;
288
    }
289
290
    /**
291
     * Set success limit.
292
     *
293
     * @param int $success Success limit
294
     *
295
     * @return Suite
296
     */
297
    public function setSuccess(int $success): Suite
298
    {
299
        $this->success = $success;
300
301
        return $this;
302
    }
303
304
    /**
305
     * Get success limit.
306
     *
307
     * @return int
308
     */
309
    public function getSuccess(): int
310
    {
311
        return $this->success;
312
    }
313
314
    /**
315
     * Set passed tests count.
316
     *
317
     * @param int $passed Passed tests
318
     *
319
     * @return Suite
320
     */
321
    public function setpassed(int $passed): Suite
322
    {
323
        $this->passed = $passed;
324
325
        return $this;
326
    }
327
328
    /**
329
     * Get passed tests count.
330
     *
331
     * @return int
332
     */
333
    public function getPassed(): int
334
    {
335
        return $this->passed;
336
    }
337
338
    /**
339
     * Set failed tests count.
340
     *
341
     * @param int $failed Failed tests
342
     *
343
     * @return Suite
344
     */
345
    public function setFailed(int $failed): Suite
346
    {
347
        $this->failed = $failed;
348
349
        return $this;
350
    }
351
352
    /**
353
     * Get failed tests count.
354
     *
355
     * @return int
356
     */
357
    public function getFailed(): int
358
    {
359
        return $this->failed;
360
    }
361
362
    /**
363
     * Set errored tests count.
364
     *
365
     * @param int $errored Errored tests
366
     *
367
     * @return Suite
368
     */
369
    public function setErrored(int $errored): Suite
370
    {
371
        $this->errored = $errored;
372
373
        return $this;
374
    }
375
376
    /**
377
     * Get errored tests count.
378
     *
379
     * @return int
380
     */
381
    public function getErrored(): int
382
    {
383
        return $this->errored;
384
    }
385
386
    /**
387
     * Set skipped tests count.
388
     *
389
     * @param int $skipped Skipped tests
390
     *
391
     * @return Suite
392
     */
393
    public function setSkipped(int $skipped): Suite
394
    {
395
        $this->skipped = $skipped;
396
397
        return $this;
398
    }
399
400
    /**
401
     * Get skipped tests count.
402
     *
403
     * @return int
404
     */
405
    public function getSkipped(): int
406
    {
407
        return $this->skipped;
408
    }
409
410
    /**
411
     * Set disabled tests count.
412
     *
413
     * @param int $disabled Disable tests
414
     *
415
     * @return Suite
416
     */
417
    public function setDisabled(int $disabled): Suite
418
    {
419
        $this->disabled = $disabled;
420
421
        return $this;
422
    }
423
424
    /**
425
     * Get disabled tests count.
426
     *
427
     * @return int
428
     */
429
    public function getDisabled(): int
430
    {
431
        return $this->disabled;
432
    }
433
434
    /**
435
     * Set duration of the suite in second.
436
     *
437
     * @param float $duration Duration
438
     *
439
     * @return Suite
440
     */
441
    public function setDuration(float $duration): Suite
442
    {
443
        $this->duration = $duration;
444
445
        return $this;
446
    }
447
448
    /**
449
     * Get duration of the suite in seconds.
450
     *
451
     * @return float
452
     */
453
    public function getDuration(): float
454
    {
455
        return $this->duration;
456
    }
457
458
    /**
459
     * Set datetime of suite.
460
     *
461
     * @param DateTime $datetime DateTime of suite.
462
     *
463
     * @return Suite
464
     */
465
    public function setDateTime(DateTime $datetime): Suite
466
    {
467
        $this->datetime = $datetime;
468
469
        return $this;
470
    }
471
472
    /**
473
     * Get datetime of suite.
474
     *
475
     * @return DateTime
476
     */
477
    public function getDateTime(): DateTime
478
    {
479
        return $this->datetime;
480
    }
481
482
    /**
483
     * Set order.
484
     *
485
     * @param int $position The order.
486
     *
487
     * @return Suite
488
     */
489
    public function setPosition(int $position): Suite
490
    {
491
        $this->position = $position;
492
493
        return $this;
494
    }
495
496
    /**
497
     * Get position.
498
     *
499
     * @return int
500
     */
501
    public function getPosition(): int
502
    {
503
        return $this->position;
504
    }
505
506
    /**
507
     * Get reference id (Incremental integer).
508
     *
509
     * @return int
510
     *
511
     * @Serializer\VirtualProperty
512
     * @Serializer\SerializedName("refid")
513
     * @Serializer\Type("int")
514
     * @Serializer\Groups({"public", "private"})
515
     */
516
    public function getRefid(): int
517
    {
518
        return $this->position + 1;
519
    }
520
521
    /**
522
     * Set campaign.
523
     *
524
     * @param Campaign $campaign Campaign
525
     *
526
     * @return Suite
527
     */
528
    public function setCampaign(Campaign $campaign): Suite
529
    {
530
        $this->campaign = $campaign;
531
532
        return $this;
533
    }
534
535
    /**
536
     * Get campaign.
537
     *
538
     * @return Campaign
539
     */
540
    public function getCampaign(): Campaign
541
    {
542
        return $this->campaign;
543
    }
544
545
    /**
546
     * Get enabled tests.
547
     *
548
     * @return int
549
     */
550
    public function getEnabled(): int
551
    {
552
        return $this->passed
553
            + $this->failed
554
            + $this->errored
555
            + $this->skipped;
556
    }
557
558
    /**
559
     * Get percentage of successful tests.
560
     *
561
     * @return float
562
     */
563
    public function getPercentage(): float
564
    {
565
        if ($this->getEnabled() > 0) {
566
            return round($this->passed / $this->getEnabled() * 100);
567
        }
568
569
        return 0;
570
    }
571
572
    /**
573
     * Get suite status.
574
     *
575
     * @return int Status::FAILED|Status::WARNING|Status::SUCCESS
576
     */
577
    public function getStatus(): int
578
    {
579
        if ($this->getPercentage() < $this->getWarning()) {
580
            return Status::FAILED;
581
        }
582
        if ($this->getPercentage() < $this->getSuccess()) {
583
            return Status::WARNING;
584
        }
585
586
        return Status::SUCCESS;
587
    }
588
589
    /**
590
     * Set from DTO limits suite.
591
     *
592
     * @param SuiteLimitsDTO $dto DTO object
593
     *
594
     * @return Suite
595
     */
596
    public function setFromLimitsDTO(SuiteLimitsDTO $dto): Suite
597
    {
598
        if (null !== $dto->getWarning()) {
599
            $this->setWarning($dto->getWarning());
600
        } else {
601
            $project = $this->getCampaign()->getProject();
602
            $this->setWarning($project->getWarning());
603
        }
604
        if (null !== $dto->getSuccess()) {
605
            $this->setSuccess($dto->getSuccess());
606
        } else {
607
            $project = $this->getCampaign()->getProject();
608
            $this->setSuccess($project->getSuccess());
609
        }
610
611
        return $this;
612
    }
613
614
    /**
615
     * Set from DTO suite.
616
     *
617
     * @param SuiteDTO $dto DTO object
618
     *
619
     * @return Suite
620
     */
621
    public function setFromDTO(SuiteDTO $dto): Suite
622
    {
623
        $this->setFromLimitsDTO($dto);
624
625
        $this->setName($dto->getName());
626
        $this->setDisabled($dto->getDisabled());
627
        $this->setDuration($dto->getDuration());
628
        $this->setDateTime($dto->getDatetime());
629
630
        return $this;
631
    }
632
}
633