ValidationExceptionTest.php
3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
 * @package framework
 * @subpackage Testing
 */
class ValidationExceptionTest extends SapphireTest 
{
	/**
	 * Test that ValidationResult object can correctly populate a ValidationException
	 */
	public function testCreateFromValidationResult() {
		
		$result = new ValidationResult(false, 'Not a valid result');
		$exception = new ValidationException($result);
		
		$this->assertEquals(0, $exception->getCode());
		$this->assertEquals('Not a valid result', $exception->getMessage());
		$this->assertEquals(false, $exception->getResult()->valid());
		$this->assertEquals('Not a valid result', $exception->getResult()->message());
		
	}
	
	/**
	 * Test that ValidationResult object with multiple errors can correctly 
	 * populate a ValidationException
	 */
	public function testCreateFromComplexValidationResult() {
		$result = new ValidationResult();
		$result->error('Invalid type')
				->error('Out of kiwis');
		$exception = new ValidationException($result);
		
		$this->assertEquals(0, $exception->getCode());
		$this->assertEquals('Invalid type; Out of kiwis', $exception->getMessage());
		$this->assertEquals(false, $exception->getResult()->valid());
		$this->assertEquals('Invalid type; Out of kiwis', $exception->getResult()->message());
	}
	
	/**
	 * Test that a ValidationException created with no contained ValidationResult
	 * will correctly populate itself with an inferred version
	 */
	public function testCreateFromMessage() {
		$exception = new ValidationException('Error inferred from message', E_USER_ERROR);
		
		$this->assertEquals(E_USER_ERROR, $exception->getCode());
		$this->assertEquals('Error inferred from message', $exception->getMessage());
		$this->assertEquals(false, $exception->getResult()->valid());
		$this->assertEquals('Error inferred from message', $exception->getResult()->message());
	}
	
	/**
	 * Test that ValidationException can be created with both a ValidationResult
	 * and a custom message
	 */
	public function testCreateWithValidationResultAndMessage() {
		$result = new ValidationResult(false, 'Incorrect placement of cutlery');
		$exception = new ValidationException($result, 'An error has occurred', E_USER_WARNING);
		
		$this->assertEquals(E_USER_WARNING, $exception->getCode());
		$this->assertEquals('An error has occurred', $exception->getMessage());
		$this->assertEquals(false, $exception->getResult()->valid());
		$this->assertEquals('Incorrect placement of cutlery', $exception->getResult()->message());
	}
	
	
	/**
	 * Test that ValidationException can be created with both a ValidationResult
	 * and a custom message
	 */
	public function testCreateWithComplexValidationResultAndMessage() {
		$result = new ValidationResult();
		$result->error('A spork is not a knife')
				->error('A knife is not a back scratcher');
		$exception = new ValidationException($result, 'An error has occurred', E_USER_WARNING);
		
		$this->assertEquals(E_USER_WARNING, $exception->getCode());
		$this->assertEquals('An error has occurred', $exception->getMessage());
		$this->assertEquals(false, $exception->getResult()->valid());
		$this->assertEquals('A spork is not a knife; A knife is not a back scratcher',
			$exception->getResult()->message());
	}
	/**
	 * Test combining validation results together
	 */
	public function testCombineResults(){
		$result = new ValidationResult();
		$anotherresult = new ValidationResult();
		$yetanotherresult = new ValidationResult();
		$anotherresult->error("Eat with your mouth closed", "EATING101");
		$yetanotherresult->error("You didn't wash your hands", "BECLEAN");
		$this->assertTrue($result->valid());
		$this->assertFalse($anotherresult->valid());
		$this->assertFalse($yetanotherresult->valid());
		$result->combineAnd($anotherresult)
				->combineAnd($yetanotherresult);
		$this->assertFalse($result->valid());
		$this->assertEquals(array(
			"EATING101" => "Eat with your mouth closed",
			"BECLEAN" => "You didn't wash your hands"
		),$result->messageList());
	}
}