Skip to content

Commit 735f387

Browse files
author
Daniel Opitz
committed
Added tests
1 parent 34abe6d commit 735f387

File tree

6 files changed

+129
-38
lines changed

6 files changed

+129
-38
lines changed

src/Validation/StatusCodeMessage.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Odan\Validation;
44

55
/**
6-
* StatusMessage.
6+
* Status Code Message.
77
*
88
* Represents a status code ('5001', 'success', 'danger', etc...) and a status message.
99
*/
@@ -12,7 +12,7 @@ class StatusCodeMessage
1212
/**
1313
* @var string
1414
*/
15-
protected $status;
15+
protected $code;
1616

1717
/**
1818
* @var string
@@ -22,26 +22,27 @@ class StatusCodeMessage
2222
/**
2323
* Constructor.
2424
*
25-
* @param string $status Alert status
25+
* @param string $code Alert status
2626
* @param string $message The message
2727
*/
28-
public function __construct(string $status, string $message)
28+
public function __construct(string $code, string $message)
2929
{
30-
$this->status = $status;
30+
$this->code = $code;
3131
$this->message = $message;
3232
}
3333

3434
/**
35-
* Clone object.
35+
* Set values.
3636
*
37-
* @param string $status Status code
37+
* @param string $code Status code
3838
* @param string $message The message
3939
*
40-
* @return self
40+
* @return void
4141
*/
42-
public function with(string $status, string $message): self
42+
public function set(string $code, string $message)
4343
{
44-
return new self($status, $message);
44+
$this->code = $code;
45+
$this->message = $message;
4546
}
4647

4748
/**
@@ -57,11 +58,11 @@ public function message(): string
5758
/**
5859
* Returns the status code.
5960
*
60-
* @return string The status
61+
* @return string The status code
6162
*/
6263
public function status(): string
6364
{
64-
return $this->status;
65+
return $this->code;
6566
}
6667

6768
/**

src/Validation/SuccessMessage.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Odan\Validation;
44

55
/**
6-
* SuccessMessage.
6+
* Success Message.
77
*
88
* Represents a boolean success status and a success message.
99
*/
@@ -32,16 +32,17 @@ public function __construct(bool $success, string $message)
3232
}
3333

3434
/**
35-
* Get message.
35+
* Set values.
3636
*
3737
* @param bool $success
3838
* @param string $message
3939
*
40-
* @return self
40+
* @return void
4141
*/
42-
public function with(bool $success, string $message): self
42+
public function set(bool $success, string $message)
4343
{
44-
return new self($success, $message);
44+
$this->success = $success;
45+
$this->message = $message;
4546
}
4647

4748
/**

src/Validation/ValidationMessage.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Odan\Validation;
44

55
/**
6-
* ValidationMessage.
6+
* Validation Message.
77
*
88
* Represents a container for the results of a validation request.
99
*/
@@ -24,7 +24,7 @@ class ValidationMessage
2424
*
2525
* @return array Errors
2626
*/
27-
public function getErrors()
27+
public function getErrors(): array
2828
{
2929
return $this->errors;
3030
}
@@ -34,7 +34,7 @@ public function getErrors()
3434
*
3535
* @return mixed Error
3636
*/
37-
public function getError()
37+
public function getFirstError()
3838
{
3939
return reset($this->errors);
4040
}
@@ -44,7 +44,7 @@ public function getError()
4444
*
4545
* @return string|null
4646
*/
47-
public function getMessage()
47+
public function getMessage(): ?string
4848
{
4949
return $this->message;
5050
}
@@ -54,13 +54,11 @@ public function getMessage()
5454
*
5555
* @param string $message The default success message
5656
*
57-
* @return self self
57+
* @return void
5858
*/
5959
public function setMessage(string $message)
6060
{
6161
$this->message = $message;
62-
63-
return $this;
6462
}
6563

6664
/**
@@ -86,29 +84,31 @@ public function failed(): bool
8684
/**
8785
* Clear errors and message.
8886
*
89-
* @return self
87+
* @return void
9088
*/
9189
public function clear()
9290
{
9391
$this->message = null;
9492
$this->errors = [];
95-
96-
return $this;
9793
}
9894

9995
/**
10096
* Add error message.
10197
*
102-
* @param string $field Field name
103-
* @param string $message Message
98+
* @param string $field the field name containing the error
99+
* @param string $message A String providing a short description of the error.
100+
* The message SHOULD be limited to a concise single sentence.
101+
* @param string|null $code A numeric or alphanumeric value that indicates the error type that occurred. (optional)
104102
*
105-
* @return self
103+
* @return void
106104
*/
107-
public function addError(string $field, string $message)
105+
public function addError(string $field, string $message, string $code = null)
108106
{
109-
$this->errors[] = ['field' => $field, 'message' => $message];
110-
111-
return $this;
107+
if ($code === null) {
108+
$this->errors[] = ['field' => $field, 'message' => $message];
109+
} else {
110+
$this->errors[] = ['field' => $field, 'message' => $message, 'code' => $code];
111+
}
112112
}
113113

114114
/**

tests/StatusCodeMessageTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Odan\Validation\Test;
4+
5+
use Odan\Validation\StatusCodeMessage;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* Tests.
10+
*
11+
* @coversDefaultClass \Odan\Validation\StatusCodeMessage
12+
*/
13+
class StatusCodeMessageTest extends TestCase
14+
{
15+
public function testConstruct()
16+
{
17+
$actual = new StatusCodeMessage(0, 'message');
18+
$this->assertInstanceOf(StatusCodeMessage::class, $actual);
19+
}
20+
21+
public function testToArray()
22+
{
23+
$this->markTestSkipped();
24+
}
25+
26+
public function testSet()
27+
{
28+
$this->markTestSkipped();
29+
}
30+
31+
public function testMessage()
32+
{
33+
$this->markTestSkipped();
34+
}
35+
36+
public function testStatus()
37+
{
38+
$this->markTestSkipped();
39+
}
40+
41+
42+
}

tests/SuccessMessageTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Odan\Validation\Test;
4+
5+
use Odan\Validation\SuccessMessage;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* Tests.
10+
*
11+
* @coversDefaultClass \Odan\Validation\SuccessMessage
12+
*/
13+
class SuccessMessageTest extends TestCase
14+
{
15+
public function testConstruct()
16+
{
17+
$actual = new SuccessMessage(true, 'Ok');
18+
$this->assertInstanceOf(SuccessMessage::class, $actual);
19+
}
20+
21+
public function testFailed()
22+
{
23+
$this->markTestSkipped();
24+
}
25+
26+
public function testSet()
27+
{
28+
$this->markTestSkipped();
29+
}
30+
31+
public function testMessage()
32+
{
33+
$this->markTestSkipped();
34+
}
35+
36+
37+
public function testSuccess()
38+
{
39+
$this->markTestSkipped();
40+
}
41+
42+
public function testToArray()
43+
{
44+
$this->markTestSkipped();
45+
}
46+
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
/**
99
* ValidationResult tests.
1010
*
11-
* @coversDefaultClass \Odan\Validation\ValidationResult
11+
* @coversDefaultClass \Odan\Validation\ValidationMessage
1212
*/
13-
class ValidationResultTest extends TestCase
13+
class ValidationMessageTest extends TestCase
1414
{
1515
/**
1616
* Test instance.
@@ -102,7 +102,7 @@ public function testErrorsWithMessage()
102102
$val->addError('email', 'required');
103103
$result = $val->failed();
104104
$this->assertTrue($result);
105-
$this->assertEquals(['field' => 'email', 'message' => 'required'], $val->getError());
105+
$this->assertEquals(['field' => 'email', 'message' => 'required'], $val->getFirstError());
106106
}
107107

108108
/**
@@ -174,12 +174,13 @@ public function testToArray()
174174
$val = new ValidationMessage();
175175
$val->setMessage('Errors');
176176
$val->addError('error1', 'error');
177-
$val->addError('error2', 'error');
177+
$val->addError('error2', 'error', '5000');
178178
$result = $val->toArray();
179179
$this->assertSame($result['message'], 'Errors');
180180
$this->assertSame($result['errors'][0]['field'], 'error1');
181181
$this->assertSame($result['errors'][0]['message'], 'error');
182182
$this->assertSame($result['errors'][1]['field'], 'error2');
183183
$this->assertSame($result['errors'][1]['message'], 'error');
184+
$this->assertSame($result['errors'][1]['code'], '5000');
184185
}
185186
}

0 commit comments

Comments
 (0)