Passed
Push — master ( a22ed6...3efdf7 )
by Fabien
02:48
created

AgeCalculatorTest::isBirthday()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 2
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 __construct()
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(!($this->isBirthday($month, $day))) {
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
     * @return boolean
85
     */
86
    private function isBirthday($month, $day)
87
    {
88
        return ($day == $this->actualDay && $month == $this->actualMonth);
89
    }
90
91
    /**
92
     * @param integer $month
93
     * @param integer $day
94
     * @param integer $yearsOld
95
     */
96
    private function assertEqualsForAge($month, $day, $yearsOld)
97
    {
98
        $this->calculator = new AgeCalculator($this->birthYear.'-'.$month.'-'.$day);
99
100
        $age = $this->calculator->age();
101
102
        $this->assertEquals($yearsOld, $age);
103
    }
104
105
    /**
106
     * @expectedException InvalidArgumentException
107
     */
108
    public function testBadFormat()
109
    {
110
        $this->calculator = new AgeCalculator('0000/0/0');
111
    }
112
113
    /**
114
     * @expectedException InvalidArgumentException
115
     */
116
    public function testBadDate()
117
    {
118
        $this->calculator = new AgeCalculator('0000-0-0');
119
    }
120
}
121