Completed
Pull Request — master (#13)
by Fabien
13:53 queued 10:30
created

AgeCalculatorTest::testFirstDayOfTheYear()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 2
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 $assert;
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->assert      = 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);
45
    }
46
47
    public function testSameMonth()
48
    {
49
        $this->assertEqualsForAge($this->actualMonth, $this->minDay);
50
    }
51
52
    public function testEndOfTheYear()
53
    {
54
        $this->assertEqualsForAge($this->maxMonth, $this->maxDay, TRUE);
55
    }
56
57
    public function testSameMonthAndLastDay()
58
    {
59
        $this->assertEqualsForAge($this->actualMonth, $this->maxDay, TRUE);
60
    }
61
62
    public function testFirstDayOfTheYear()
63
    {
64
        $this->assertEqualsForAge($this->minMonth, $this->minDay);
65
    }
66
67
    /**
68
     * @param integer $day
69
     */
70
    private function assertEqualsForAge($month, $day, $beforeBirthday = FALSE)
71
    {
72
        $assert = $this->assert;
73
        if($beforeBirthday) {
74
            if(!($day == $this->actualDay && $month == $this->actualMonth)) {
75
                $assert = $this->assert - 1;
76
            }
77
        }
78
79
        $this->calculator = new AgeCalculator($this->birthYear.'-'.$month.'-'.$day);
80
81
        $age = $this->calculator->age();
82
83
        $this->assertEquals($assert, $age);
84
    }
85
86
    /**
87
     * @expectedException InvalidArgumentException
88
     */
89
    public function testBadFormat()
90
    {
91
        $this->calculator = new AgeCalculator('0000/0/0');
92
    }
93
94
    /**
95
     * @expectedException InvalidArgumentException
96
     */
97
    public function testBadDate()
98
    {
99
        $this->calculator = new AgeCalculator('0000-0-0');
100
    }
101
}
102