1<?php 2 3/* 4 * This file is part of Respect/Validation. 5 * 6 * (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net> 7 * 8 * For the full copyright and license information, please view the "LICENSE.md" 9 * file that was distributed with this source code. 10 */ 11 12namespace Respect\Validation\Rules; 13 14use DateTime; 15 16/** 17 * @group rule 18 * @covers Respect\Validation\Rules\Age 19 * @covers Respect\Validation\Exceptions\AgeException 20 */ 21class AgeTest extends \PHPUnit_Framework_TestCase 22{ 23 /** 24 * @expectedException Respect\Validation\Exceptions\ComponentException 25 * @expectedExceptionMessage An age interval must be provided 26 */ 27 public function testShouldThrowsExceptionWhenThereIsNoArgumentsOnConstructor() 28 { 29 new Age(); 30 } 31 /** 32 * @expectedException Respect\Validation\Exceptions\ComponentException 33 * @expectedExceptionMessage 20 cannot be greater than or equals to 10 34 */ 35 public function testShouldThrowsExceptionWhenMinimumAgeIsLessThenMaximumAge() 36 { 37 new Age(20, 10); 38 } 39 40 /** 41 * @expectedException Respect\Validation\Exceptions\ComponentException 42 * @expectedExceptionMessage 20 cannot be greater than or equals to 20 43 */ 44 public function testShouldThrowsExceptionWhenMinimumAgeIsEqualsToMaximumAge() 45 { 46 new Age(20, 20); 47 } 48 49 public function providerForValidAge() 50 { 51 return [ 52 [18, null, date('Y-m-d', strtotime('-18 years'))], 53 [18, null, date('Y-m-d', strtotime('-19 years'))], 54 [18, null, new DateTime('-18 years')], 55 [18, null, new DateTime('-19 years')], 56 57 [18, 50, date('Y-m-d', strtotime('-18 years'))], 58 [18, 50, date('Y-m-d', strtotime('-50 years'))], 59 [18, 50, new DateTime('-18 years')], 60 [18, 50, new DateTime('-50 years')], 61 62 [null, 50, date('Y-m-d', strtotime('-49 years'))], 63 [null, 50, date('Y-m-d', strtotime('-50 years'))], 64 [null, 50, new DateTime('-49 years')], 65 [null, 50, new DateTime('-50 years')], 66 ]; 67 } 68 69 /** 70 * @dataProvider providerForValidAge 71 */ 72 public function testShouldValidateValidAge($minAge, $maxAge, $input) 73 { 74 $rule = new Age($minAge, $maxAge); 75 76 $this->assertTrue($rule->validate($input)); 77 } 78 79 public function providerForInvalidAge() 80 { 81 return [ 82 [18, null, date('Y-m-d', strtotime('-17 years'))], 83 [18, null, new DateTime('-17 years')], 84 85 [18, 50, date('Y-m-d', strtotime('-17 years'))], 86 [18, 50, date('Y-m-d', strtotime('-51 years'))], 87 [18, 50, new DateTime('-17 years')], 88 [18, 50, new DateTime('-51 years')], 89 90 [null, 50, date('Y-m-d', strtotime('-51 years'))], 91 [null, 50, new DateTime('-51 years')], 92 ]; 93 } 94 95 /** 96 * @dataProvider providerForInvalidAge 97 */ 98 public function testShouldValidateInvalidAge($minAge, $maxAge, $input) 99 { 100 $rule = new Age($minAge, $maxAge); 101 102 $this->assertFalse($rule->validate($input)); 103 } 104 105 /** 106 * @depends testShouldValidateInvalidAge 107 * 108 * @expectedException Respect\Validation\Exceptions\AgeException 109 * @expectedExceptionMessage "today" must be lower than 18 years ago 110 */ 111 public function testShouldThrowsExceptionWhenMinimumAgeFails() 112 { 113 $rule = new Age(18); 114 $rule->assert('today'); 115 } 116 117 /** 118 * @depends testShouldValidateInvalidAge 119 * 120 * @expectedException Respect\Validation\Exceptions\AgeException 121 * @expectedExceptionMessage "51 years ago" must be greater than 50 years ago 122 */ 123 public function testShouldThrowsExceptionWhenMaximunAgeFails() 124 { 125 $rule = new Age(null, 50); 126 $rule->assert('51 years ago'); 127 } 128 129 /** 130 * @depends testShouldValidateInvalidAge 131 * 132 * @expectedException Respect\Validation\Exceptions\AgeException 133 * @expectedExceptionMessage "today" must be between 18 and 50 years ago 134 */ 135 public function testShouldThrowsExceptionWhenMinimunAndMaximunAgeFails() 136 { 137 $rule = new Age(18, 50); 138 $rule->assert('today'); 139 } 140} 141