Passed
Push — master ( 5c444f...454f16 )
by Fabien
03:16
created

AgeCalculatorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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
    public function setUp()
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