Completed
Pull Request — master (#16)
by Fabien
06:12 queued 03:13
created

AgeCalculatorTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 12
c 5
b 0
f 0
lcom 1
cbo 2
dl 0
loc 94
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testEarlyYear() 0 4 1
A testSameMonth() 0 4 1
A testEndOfTheYear() 0 4 1
A testSameMonthAndLastDay() 0 4 1
A testFirstDayOfTheYear() 0 4 1
A assertEqualsForAgeBeforeBirthday() 0 9 3
A assertEqualsForAge() 0 8 1
A testBadFormat() 0 4 1
A testBadDate() 0 4 1
A setUp() 0 7 1
1
<?php
2
3
/*
4
 * This file is part of the FabienCrassat\CurriculumVitaeBundle Symfony bundle.
5
 *
6
 * (c) Fabien Crassat <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FabienCrassat\CurriculumVitaeBundle\Tests\Utility;
13
14
use FabienCrassat\CurriculumVitaeBundle\Utility\AgeCalculator;
15
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16
17
class AgeCalculatorTest extends \PHPUnit\Framework\TestCase
18
{
19
    /**
20
     * @var string $today Timestamp that will be returned by date()
21
     */
22
    public static $today;
23
24
    private $yearsOld;
25
    private $minDay = 1;
26
    private $maxDay;
27
    private $minMonth = 1;
28
    private $maxMonth = 12;
29
    private $actualMonth;
30
    private $actualDay;
31
    private $birthYear = 2000;
32
    private $calculator;
33
34
    function setUp()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
    {
36
        $this->yearsOld    = date('Y') - $this->birthYear;
37
        $this->actualMonth = date('n');
38
        $this->actualDay   = date('d');
39
        $this->maxDay      = date('t', strtotime($this->birthYear.'-'.$this->actualMonth.'-23'));
40
    }
41
42
    public function testEarlyYear()
43
    {
44
        $this->assertEqualsForAge($this->minMonth, $this->minDay, $this->yearsOld);
45
    }
46
47
    public function testSameMonth()
48
    {
49
        $this->assertEqualsForAge($this->actualMonth, $this->minDay, $this->yearsOld);
50
    }
51
52
    public function testEndOfTheYear()
53
    {
54
        $this->assertEqualsForAgeBeforeBirthday($this->maxMonth, $this->maxDay);
55
    }
56
57
    public function testSameMonthAndLastDay()
58
    {
59
        $this->assertEqualsForAgeBeforeBirthday($this->actualMonth, $this->maxDay);
60
    }
61
62
    public function testFirstDayOfTheYear()
63
    {
64
        $this->assertEqualsForAge($this->minMonth, $this->minDay, $this->yearsOld);
65
    }
66
67
    /**
68
     * @param integer $month
69
     * @param integer $day
70
     */
71
    private function assertEqualsForAgeBeforeBirthday($month, $day)
72
    {
73
        $yearsOld = $this->yearsOld;
74
        if(!($day == $this->actualDay && $month == $this->actualMonth)) {
75
            $yearsOld = $this->yearsOld - 1;
76
        }
77
78
        $this->assertEqualsForAge($month, $day, $yearsOld);
79
    }
80
81
    /**
82
     * @param integer $month
83
     * @param integer $day
84
     * @param integer $yearsOld
85
     */
86
    private function assertEqualsForAge($month, $day, $yearsOld)
87
    {
88
        $this->calculator = new AgeCalculator($this->birthYear.'-'.$month.'-'.$day);
89
90
        $age = $this->calculator->age();
91
92
        $this->assertEquals($yearsOld, $age);
93
    }
94
95
    /**
96
     * @expectedException InvalidArgumentException
97
     */
98
    public function testBadFormat()
99
    {
100
        $this->calculator = new AgeCalculator('0000/0/0');
101
    }
102
103
    /**
104
     * @expectedException InvalidArgumentException
105
     */
106
    public function testBadDate()
107
    {
108
        $this->calculator = new AgeCalculator('0000-0-0');
109
    }
110
}
111