Passed
Push — master ( 98e928...797c29 )
by Fabien
02:18
created

AgeCalculatorTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
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 assertEqualsForAge() 0 15 4
A testBadFormat() 0 4 1
A testBadDate() 0 4 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 $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