Skip to content

Commit ecc7a13

Browse files
committed
Update tests
1 parent f6ad0e8 commit ecc7a13

File tree

7 files changed

+58
-112
lines changed

7 files changed

+58
-112
lines changed

tests/Encoder/JsonEncoderTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ public function testEncode()
2828

2929
/**
3030
* Test.
31-
*
32-
* @return void
3331
*/
3432
public function testInvalidEncoding(): void
3533
{
3634
$this->expectException(UnexpectedValueException::class);
37-
$this->expectExceptionMessage('JSON encoding failed. Code: 5. Error: Malformed UTF-8 characters, possibly incorrectly encoded.');
35+
$this->expectExceptionMessage('JSON encoding failed. Code: 5. Error: Malformed UTF-8 characters,' .
36+
' possibly incorrectly encoded.');
3837

3938
$encoder = new JsonEncoder();
4039
$encoder->encode(['key' => "\x00\x81"]);

tests/Exception/ValidationExceptionTest.php

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

55
use PHPUnit\Framework\TestCase;
6-
use RuntimeException;
76
use Selective\Validation\Exception\ValidationException;
87
use Selective\Validation\Test\TestService;
98

@@ -17,62 +16,21 @@ class ValidationExceptionTest extends TestCase
1716
/**
1817
* Test pseudo action.
1918
*/
20-
public function testSuccessAction()
19+
public function testSuccessAction(): void
2120
{
2221
$service = new TestService();
2322
$result = $service->process(1);
24-
$json = $this->withJson($result);
2523

26-
$this->assertSame('{"success":true,"message":"Successfully","code":0,"data":{"foo":"value"}}', $json);
24+
$this->assertSame('{"success":true}', (json_encode($result)));
2725
}
2826

2927
/**
3028
* Test pseudo action.
3129
*/
32-
public function testValidationFailedAction()
30+
public function testValidationFailedAction(): void
3331
{
34-
try {
35-
$service = new TestService();
36-
$result = $service->process(0);
37-
$json = $this->withJson($result);
38-
} catch (ValidationException $exception) {
39-
$validation = $exception->getValidation();
40-
$json = $this->withStatus(422)->withJson($validation);
41-
}
42-
43-
$this->assertSame('{"message":"Please check your input","errors":[{"message":"invalid","field":"id"}]}', $json);
44-
}
45-
46-
/**
47-
* Pseudo response method.
48-
*
49-
* @param mixed $data Data
50-
*
51-
* @return string Json
52-
*/
53-
protected function withJson($data): string
54-
{
55-
$result = json_encode($data);
56-
57-
if ($result === false) {
58-
throw new RuntimeException('Json encoding failed');
59-
}
60-
61-
return $result;
62-
}
63-
64-
/**
65-
* Pseudo response method.
66-
*
67-
* @param mixed $code Code
68-
*
69-
* @return $this self
70-
*/
71-
protected function withStatus($code)
72-
{
73-
// pseudo usage for phpstan only
74-
trim($code);
75-
76-
return $this;
32+
$this->expectException(ValidationException::class);
33+
$service = new TestService();
34+
$service->process(0);
7735
}
7836
}

tests/Middleware/MiddlewareTestTrait.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ protected function runQueue(array $queue): ResponseInterface
3131

3232
/**
3333
* Factory.
34-
*
35-
* @return ServerRequestInterface
3634
*/
3735
protected function createRequest(): ServerRequestInterface
3836
{

tests/Middleware/ValidationExceptionMiddlewareTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPUnit\Framework\TestCase;
77
use Selective\Validation\Encoder\JsonEncoder;
88
use Selective\Validation\Middleware\ValidationExceptionMiddleware;
9+
use Selective\Validation\Transformer\ErrorDetailsTransformer;
910
use Slim\Psr7\Factory\ResponseFactory;
1011

1112
/**
@@ -26,6 +27,7 @@ public function testNoError()
2627
{
2728
$middleware = new ValidationExceptionMiddleware(
2829
new ResponseFactory(),
30+
new ErrorDetailsTransformer(),
2931
new JsonEncoder()
3032
);
3133

@@ -46,6 +48,7 @@ public function testWithError()
4648
{
4749
$middleware = new ValidationExceptionMiddleware(
4850
new ResponseFactory(),
51+
new ErrorDetailsTransformer(),
4952
new JsonEncoder()
5053
);
5154

@@ -55,7 +58,8 @@ public function testWithError()
5558
]);
5659

5760
static::assertSame(
58-
'{"error":{"message":"Please check your input","errors":[{"message":"Input required","field":"username"}]}}',
61+
'{"error":{"message":"Please check your input","details":' .
62+
'[{"message":"Input required","field":"username"}]}}',
5963
(string)$response->getBody()
6064
);
6165

tests/TestService.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,8 @@ public function process(int $id): array
3333
throw new ValidationException($validation);
3434
}
3535

36-
$result = [];
37-
$result['success'] = true;
38-
$result['message'] = 'Successfully';
39-
$result['code'] = 0;
40-
$result['data']['foo'] = 'value';
41-
42-
return $result;
36+
return [
37+
'success' => true,
38+
];
4339
}
4440
}

tests/ValidationErrorTest.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,44 @@
1212
*/
1313
class ValidationErrorTest extends TestCase
1414
{
15-
public function testConstruct()
15+
/**
16+
* Test.
17+
*
18+
* @return void
19+
*/
20+
public function testConstruct(): void
1621
{
1722
$message = new ValidationError('');
1823
$this->assertInstanceOf(ValidationError::class, $message);
1924
}
2025

21-
public function testWithField()
26+
/**
27+
* Test.
28+
*
29+
* @return void
30+
*/
31+
public function testWithField():void
2232
{
2333
$message = new ValidationError('required');
2434
$message->setField('email');
2535

26-
$this->assertEquals([
27-
'field' => 'email',
28-
'message' => 'required',
29-
], $message->toArray());
36+
$this->assertSame('email', $message->getField());
37+
$this->assertSame('required', $message->getMessage());
3038
}
3139

32-
public function testWithFieldAndCode()
40+
/**
41+
* Test.
42+
*
43+
* @return void
44+
*/
45+
public function testWithFieldAndCode(): void
3346
{
3447
$message = new ValidationError('invalid');
3548
$message->setField('email');
3649
$message->setCode('5000');
3750

38-
$this->assertEquals([
39-
'field' => 'email',
40-
'message' => 'invalid',
41-
'code' => '5000',
42-
], $message->toArray());
51+
$this->assertSame('invalid', $message->getMessage());
52+
$this->assertSame('email', $message->getField());
53+
$this->assertSame('5000', $message->getCode());
4354
}
4455
}

tests/ValidationResultTest.php

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ValidationResultTest extends TestCase
1616
/**
1717
* Test instance.
1818
*/
19-
public function testInstance()
19+
public function testInstance(): void
2020
{
2121
$actual = new ValidationResult();
2222
$this->assertInstanceOf(ValidationResult::class, $actual);
@@ -27,7 +27,7 @@ public function testInstance()
2727
*
2828
* @return void
2929
*/
30-
public function testSetSuccessMessage()
30+
public function testSetSuccessMessage(): void
3131
{
3232
$val = new ValidationResult();
3333
$val->setMessage('test');
@@ -41,7 +41,7 @@ public function testSetSuccessMessage()
4141
*
4242
* @return void
4343
*/
44-
public function testErrors()
44+
public function testErrors(): void
4545
{
4646
$val = new ValidationResult();
4747
$val->addError('error1', 'failed');
@@ -55,7 +55,7 @@ public function testErrors()
5555
*
5656
* @return void
5757
*/
58-
public function testErrorsEmptyFieldOne()
58+
public function testErrorsEmptyFieldOne(): void
5959
{
6060
$val = new ValidationResult();
6161
$val->addError('', 'invalid');
@@ -69,7 +69,7 @@ public function testErrorsEmptyFieldOne()
6969
*
7070
* @return void
7171
*/
72-
public function testErrorsWithField()
72+
public function testErrorsWithField(): void
7373
{
7474
$val = new ValidationResult();
7575
$val->addError('field', 'message');
@@ -83,16 +83,18 @@ public function testErrorsWithField()
8383
*
8484
* @return void
8585
*/
86-
public function testErrorsWithMessage()
86+
public function testErrorsWithMessage(): void
8787
{
8888
$val = new ValidationResult();
8989
$val->addError('email', 'required', '5000');
9090
$result = $val->isFailed();
9191
$this->assertTrue($result);
92-
$expected = ['field' => 'email', 'message' => 'required', 'code' => '5000'];
93-
$firstError = $val->getFirstError();
9492

95-
$this->assertEquals($expected, $firstError ? $firstError->toArray() : null);
93+
$firstError = $val->getFirstError();
94+
$this->assertInstanceOf(ValidationError::class, $firstError);
95+
$this->assertSame('email', $firstError->getField());
96+
$this->assertSame('required', $firstError->getMessage());
97+
$this->assertSame('5000', $firstError->getCode());
9698
}
9799

98100
/**
@@ -101,7 +103,7 @@ public function testErrorsWithMessage()
101103
*
102104
* @return void
103105
*/
104-
public function testAddErrorMessage()
106+
public function testAddErrorMessage(): void
105107
{
106108
$val = new ValidationResult();
107109
$message = new ValidationError('required');
@@ -111,11 +113,11 @@ public function testAddErrorMessage()
111113
$result = $val->isFailed();
112114
$this->assertTrue($result);
113115

114-
$expected = ['field' => 'email', 'message' => 'required', 'code' => '5000'];
115-
116116
$firstError = $val->getFirstError();
117117

118-
$this->assertEquals($expected, $firstError ? $firstError->toArray() : null);
118+
$this->assertSame('email', $firstError->getField());
119+
$this->assertSame('required', $firstError->getMessage());
120+
$this->assertSame('5000', $firstError->getCode());
119121
}
120122

121123
/**
@@ -124,7 +126,7 @@ public function testAddErrorMessage()
124126
*
125127
* @return void
126128
*/
127-
public function testNoErrors()
129+
public function testNoErrors(): void
128130
{
129131
$val = new ValidationResult();
130132
$result = $val->isFailed();
@@ -136,7 +138,7 @@ public function testNoErrors()
136138
*
137139
* @return void
138140
*/
139-
public function testGetMessage()
141+
public function testGetMessage(): void
140142
{
141143
$val = new ValidationResult();
142144
$val->setMessage('Check your input');
@@ -151,7 +153,7 @@ public function testGetMessage()
151153
*
152154
* @return void
153155
*/
154-
public function testClear()
156+
public function testClear(): void
155157
{
156158
$val = new ValidationResult();
157159
$val->setMessage('Errors');
@@ -166,7 +168,7 @@ public function testClear()
166168
*
167169
* @return void
168170
*/
169-
public function testGetErrors()
171+
public function testGetErrors(): void
170172
{
171173
$val = new ValidationResult();
172174
$errorFieldName = 'ERROR';
@@ -177,26 +179,4 @@ public function testGetErrors()
177179
$this->assertSame($result[0]->getMessage(), $errorMessage);
178180
$this->assertNull($result[0]->getCode());
179181
}
180-
181-
/**
182-
* Tests toArray function.
183-
*
184-
* @return void
185-
*/
186-
public function testToArray()
187-
{
188-
$val = new ValidationResult();
189-
$val->setCode('error_code');
190-
$val->setMessage('Errors');
191-
$val->addError('error1', 'error');
192-
$val->addError('error2', 'error', '5000');
193-
$result = $val->toArray();
194-
$this->assertSame($result['code'], 'error_code');
195-
$this->assertSame($result['message'], 'Errors');
196-
$this->assertSame($result['errors'][0]['field'], 'error1');
197-
$this->assertSame($result['errors'][0]['message'], 'error');
198-
$this->assertSame($result['errors'][1]['field'], 'error2');
199-
$this->assertSame($result['errors'][1]['message'], 'error');
200-
$this->assertSame($result['errors'][1]['code'], '5000');
201-
}
202182
}

0 commit comments

Comments
 (0)