HandlerTest::request()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Created by PhpStorm.
5
 * User: benedikt
6
 * Date: 10/1/17
7
 * Time: 2:08 PM
8
 */
9
10
namespace Tfboe\FmLib\Tests\Unit\Exceptions;
11
12
13
use Illuminate\Http\JsonResponse;
14
use Illuminate\Http\Request;
15
use Illuminate\Support\MessageBag;
16
use Illuminate\Validation\ValidationException;
17
use Illuminate\Validation\Validator;
18
use Symfony\Component\HttpFoundation\Response;
19
use Tfboe\FmLib\Exceptions\AuthenticationException;
20
use Tfboe\FmLib\Exceptions\DuplicateException;
21
use Tfboe\FmLib\Exceptions\Handler;
22
use Tfboe\FmLib\Exceptions\PlayerAlreadyExists;
23
use Tfboe\FmLib\Exceptions\ReferenceException;
24
use Tfboe\FmLib\Exceptions\UnorderedPhaseNumberException;
25
use Tfboe\FmLib\Tests\Helpers\UnitTestCase;
26
27
/**
28
 * Class HandlerTest
29
 * @package Tfboe\FmLib\Tests\Unit\Exceptions
30
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
31
 */
32
class HandlerTest extends UnitTestCase
33
{
34
//<editor-fold desc="Public Methods">
35
  /**
36
   * @covers \Tfboe\FmLib\Exceptions\Handler::render
37
   * @covers \Tfboe\FmLib\Exceptions\Handler::getExceptionHTTPStatusCode
38
   * @covers \Tfboe\FmLib\Exceptions\Handler::getExceptionName
39
   * @covers \Tfboe\FmLib\Exceptions\Handler::getJsonMessage
40
   * @uses   \Tfboe\FmLib\Exceptions\AuthenticationException
41
   * @uses   \Tfboe\FmLib\Exceptions\DuplicateException
42
   * @uses   \Tfboe\FmLib\Exceptions\PlayerAlreadyExists
43
   * @uses   \Tfboe\FmLib\Exceptions\ReferenceException
44
   * @uses   \Tfboe\FmLib\Exceptions\UnorderedPhaseNumberException
45
   */
46
  public function testRender()
47
  {
48
    $handler = $this->handler();
49
    $exception = new \Exception("Exception message");
50
    $res = $handler->render($this->request(), $exception);
51
    self::assertInstanceOf(JsonResponse::class, $res);
52
    /** @var JsonResponse $res */
53
    self::assertEquals(['status' => 500, 'message' => 'Exception message', 'name' => 'InternalException'],
54
      $res->getData(true));
55
56
    $exception = new \Exception("Exception message", 402);
57
    $res = $handler->render($this->request(), $exception);
58
    self::assertInstanceOf(JsonResponse::class, $res);
59
    /** @var JsonResponse $res */
60
    self::assertEquals(['status' => 402, 'message' => 'Exception message', 'name' => 'InternalException'],
61
      $res->getData(true));
62
63
    $exception = new AuthenticationException("Authentication Exception message");
64
    $res = $handler->render($this->request(), $exception);
65
    self::assertInstanceOf(JsonResponse::class, $res);
66
    /** @var JsonResponse $res */
67
    self::assertEquals(['status' => 401, 'message' => 'Authentication Exception message',
68
      'name' => 'AuthenticationException'], $res->getData(true));
69
70
    $exception = new DuplicateException('value', 'name', 'array');
71
    $res = $handler->render($this->request(), $exception);
72
    self::assertInstanceOf(JsonResponse::class, $res);
73
    /** @var JsonResponse $res */
74
    self::assertEquals(['status' => 409, 'message' => 'Duplicate Exception',
75
      'name' => 'DuplicateException', 'duplicateValue' => 'value', 'arrayName' => 'array'], $res->getData(true));
76
77
    $exception = new ReferenceException('value', 'name');
78
    $res = $handler->render($this->request(), $exception);
79
    self::assertInstanceOf(JsonResponse::class, $res);
80
    /** @var JsonResponse $res */
81
    self::assertEquals(['status' => 409, 'message' => 'Reference Exception',
82
      'name' => 'ReferenceException', 'referenceValue' => 'value', 'referenceName' => 'name'], $res->getData(true));
83
84
    $exception = new UnorderedPhaseNumberException(2, 1);
85
    $res = $handler->render($this->request(), $exception);
86
    self::assertInstanceOf(JsonResponse::class, $res);
87
    /** @var JsonResponse $res */
88
    self::assertEquals(['status' => 409, 'message' => 'Unordered Phase Number Exception',
89
      'name' => 'UnorderedPhaseNumberException', 'previousPhaseNumber' => 2, 'nextPhaseNumber' => 1],
90
      $res->getData(true));
91
92
    $exception = new PlayerAlreadyExists([]);
93
    $res = $handler->render($this->request(), $exception);
94
    self::assertInstanceOf(JsonResponse::class, $res);
95
    /** @var JsonResponse $res */
96
    self::assertEquals(['status' => 409, 'message' => 'Some players do already exist',
97
      'name' => 'PlayerAlreadyExistsException', 'players' => []], $res->getData(true));
98
99
    // test trace in debug mode
100
101
    $exception = new PlayerAlreadyExists([]);
102
    $res = $handler->render($this->request(), $exception, true);
103
    self::assertInstanceOf(JsonResponse::class, $res);
104
    /** @var JsonResponse $res */
105
    $data = $res->getData(true);
106
    self::assertArraySubset(['status' => 409, 'message' => 'Some players do already exist',
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertArraySubset() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/3494

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
107
      'name' => 'PlayerAlreadyExistsException', 'players' => []], $data);
108
    self::assertArrayHasKey('trace', $data);
109
    self::assertNotEmpty($data['trace']);
110
  }
111
112
  /**
113
   * @covers \Tfboe\FmLib\Exceptions\Handler::render
114
   * @covers \Tfboe\FmLib\Exceptions\Handler::getExceptionHTTPStatusCode
115
   * @covers \Tfboe\FmLib\Exceptions\Handler::getExceptionName
116
   * @covers \Tfboe\FmLib\Exceptions\Handler::getJsonMessage
117
   */
118
  public function testRenderValidationErrors()
119
  {
120
    $handler = $this->handler();
121
    $validator = $this->createMock(Validator::class);
122
    $errors = $this->createMock(MessageBag::class);
123
    $errors->method('messages')->willReturn(['username' => ['The username field is required.']]);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
    $validator->method('errors')->willReturn($errors);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125
    /** @var \Illuminate\Validation\Validator $validator */
126
    $exception = new ValidationException($validator, new Response('', 422));
127
    $request = $this->request();
128
    $res = $handler->render($request, $exception);
129
    self::assertInstanceOf(JsonResponse::class, $res);
130
    /** @var JsonResponse $res */
131
    self::assertEquals(['message' => 'The given data was invalid.', 'errors' =>
132
      ['username' => ['The username field is required.']],
133
      'status' => 422, 'name' => 'ValidationException'], $res->getData(true));
134
  }
135
//</editor-fold desc="Public Methods">
136
137
//<editor-fold desc="Private Methods">
138
  /**
139
   * @return Handler a new handler
140
   */
141
  private function handler()
142
  {
143
    return new Handler();
144
  }
145
146
  /**
147
   * @return Request a new request
148
   */
149
  private function request()
150
  {
151
    return new Request();
152
  }
153
//</editor-fold desc="Private Methods">
154
}