testTransformValueByReference()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Created by PhpStorm.
6
 * User: benedikt
7
 * Date: 9/17/17
8
 * Time: 12:33 AM
9
 */
10
11
namespace Tfboe\FmLib\Tests\Unit\Http\Controllers;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use Illuminate\Http\Request;
15
use PHPUnit\Framework\MockObject\MockObject;
16
use Tfboe\FmLib\Entity\Helpers\BaseEntity;
17
use Tfboe\FmLib\Http\Controllers\BaseController;
18
use Tfboe\FmLib\Http\Controllers\UserController;
19
use Tfboe\FmLib\TestHelpers\TestEnum;
20
use Tfboe\FmLib\Tests\Entity\User;
21
use Tfboe\FmLib\Tests\Helpers\UnitTestCase;
22
23
/**
24
 * Class BaseControllerTest
25
 * @package Tests\Unit\App\Http\Controllers
26
 */
27
class BaseControllerTest extends UnitTestCase
28
{
29
  //tests also private method disable this tests as soon as all are used in public interfaces
30
//<editor-fold desc="Public Methods">
31
32
  /**
33
   * @covers \Tfboe\FmLib\Http\Controllers\BaseController::__construct
34
   */
35 View Code Duplication
  public function testConstruct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
  {
37
    $entityManager = $this->createMock(EntityManagerInterface::class);
38
    $controller = $this->getMockForAbstractClass(BaseController::class, [
39
      $entityManager
40
    ]);
41
    self::assertInstanceOf(BaseController::class, $controller);
42
    /** @noinspection PhpUnhandledExceptionInspection */
43
    self::assertEquals($entityManager, self::getProperty(get_class($controller), 'entityManager')
44
      ->getValue($controller));
45
  }
46
47
  /**
48
   * @covers \Tfboe\FmLib\Http\Controllers\BaseController::datetimetzTransformer()
49
   * @uses   \Tfboe\FmLib\Http\Controllers\BaseController::__construct
50
   */
51
  public function testDatetimetzTransformer()
52
  {
53
    $controller = $this->controller();
54
    /** @noinspection PhpUnhandledExceptionInspection */
55
    $closure = self::getMethod(BaseController::class, 'datetimetzTransformer')
56
      ->invokeArgs($controller, [TestEnum::class]);
57
    $string = "2017-01-01 00:00:00 Europe/Vienna";
58
    $datetime = new \DateTime($string);
59
    /** @var \DateTime $result */
60
    $result = $closure($string);
61
    self::assertEquals($datetime, $result);
62
    self::assertEquals($datetime->getTimezone(), $result->getTimezone());
63
  }
64
65
  /**
66
   * @covers \Tfboe\FmLib\Http\Controllers\BaseController::enumTransformer
67
   * @uses   \Tfboe\FmLib\Helpers\BasicEnum
68
   * @uses   \Tfboe\FmLib\Http\Controllers\BaseController::__construct
69
   */
70
  public function testEnumTransformer()
71
  {
72
    $controller = $this->controller();
73
    /** @noinspection PhpUnhandledExceptionInspection */
74
    $closure = self::getMethod(BaseController::class, 'enumTransformer')->invokeArgs($controller, [TestEnum::class]);
75
    self::assertEquals(1, $closure('INT_KEY'));
76
    self::assertEquals('value', $closure('KEY'));
77
  }
78
79
  /**
80
   * @covers \Tfboe\FmLib\Http\Controllers\BaseController::setFromSpecification
81
   * @uses   \Tfboe\FmLib\Entity\Helpers\BaseEntity::methodExists
82
   * @uses   \Tfboe\FmLib\Http\Controllers\BaseController::__construct
83
   */
84 View Code Duplication
  public function testSetFromSpecificationWithDefault()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
  {
86
    $value = "test-value";
87
    $specification['prop'] = ['default' => $value];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$specification was never initialized. Although not strictly required by PHP, it is generally a good practice to add $specification = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
88
    $object = self::getMockForAbstractClass(BaseEntity::class, [], '', true, true, true, ['setProp']);
89
    $object->expects(static::once())->method('setProp')->with($value)->willReturnSelf();
90
    $controller = $this->controller();
91
    /** @noinspection PhpUnhandledExceptionInspection */
92
    $method = self::getMethod(UserController::class, 'setFromSpecification');
93
    $method->invokeArgs($controller, [$object, $specification, []]);
94
  }
95
96
  /**
97
   * @covers \Tfboe\FmLib\Http\Controllers\BaseController::setFromSpecification
98
   * @uses   \Tfboe\FmLib\Http\Controllers\BaseController::__construct
99
   * @uses   \Tfboe\FmLib\Http\Controllers\BaseController::transformValue
100
   */
101 View Code Duplication
  public function testSetFromSpecificationWithProperty()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
  {
103
    $value = 'test-value';
104
    $specification['attr'] = ['property' => 'prop'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$specification was never initialized. Although not strictly required by PHP, it is generally a good practice to add $specification = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
105
    $object = self::getMockForAbstractClass(BaseEntity::class, [], '', true, true, true, ['setProp']);
106
    $object->expects(static::once())->method('setProp')->with($value)->willReturnSelf();
107
    $controller = $this->controller();
108
    /** @noinspection PhpUnhandledExceptionInspection */
109
    $method = self::getMethod(UserController::class, 'setFromSpecification');
110
    $method->invokeArgs($controller, [$object, $specification, ['attr' => $value]]);
111
  }
112
113
  /**
114
   * @covers \Tfboe\FmLib\Http\Controllers\BaseController::transformValue
115
   * @uses   \Tfboe\FmLib\Http\Controllers\BaseController::__construct
116
   * @uses   \Tfboe\FmLib\Http\Controllers\BaseController::getEntityManager
117
   */
118
  public function testTransformValueByReference()
119
  {
120
    $user = "resultUser";
121
    $specification = ['reference' => User::class];
122
    $value = 'user-id';
123
124
    $entityManager = $this->createMock(EntityManagerInterface::class);
125
    $entityManager->expects(static::once())->method('find')->with(User::class, 'user-id')->willReturn($user);
126
    $controller = $this->getMockForAbstractClass(BaseController::class, [$entityManager]);
127
    /** @noinspection PhpUnhandledExceptionInspection */
128
    $method = self::getMethod(UserController::class, 'transformValue');
129
    $method->invokeArgs($controller, [&$value, $specification]);
130
131
    self::assertTrue($value === $user);
132
  }
133
134
  /**
135
   * @covers \Tfboe\FmLib\Http\Controllers\BaseController::transformValue
136
   * @uses   \Tfboe\FmLib\Http\Controllers\BaseController::__construct
137
   */
138 View Code Duplication
  public function testTransformValueByTransformer()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
  {
140
    $value = "5";
141
    $transformer = function ($input) {
142
      self::assertEquals("5", $input);
143
      return 6;
144
    };
145
    $specification = ['transformer' => $transformer];
146
147
    $controller = $this->controller();
148
    /** @noinspection PhpUnhandledExceptionInspection */
149
    $method = self::getMethod(UserController::class, 'transformValue');
150
    $method->invokeArgs($controller, [&$value, $specification]);
151
152
    self::assertEquals(6, $value);
153
  }
154
155
  /**
156
   * @covers \Tfboe\FmLib\Http\Controllers\BaseController::transformValue
157
   * @covers \Tfboe\FmLib\Http\Controllers\BaseController::transformByType
158
   * @uses   \Tfboe\FmLib\Http\Controllers\BaseController::__construct
159
   */
160 View Code Duplication
  public function testTransformValueByTypeDateTime()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
  {
162
    $value = "2005-02-28 16:35:01";
163
    $datetime = new \DateTime($value);
164
    $specification = ['type' => 'datetime'];
165
166
    $controller = $this->controller();
167
    /** @noinspection PhpUnhandledExceptionInspection */
168
    $method = self::getMethod(UserController::class, 'transformValue');
169
    $method->invokeArgs($controller, [&$value, $specification]);
170
171
    self::assertEquals($datetime, $value);
172
  }
173
174
  /**
175
   * @covers \Tfboe\FmLib\Http\Controllers\BaseController::transformValue
176
   * @covers \Tfboe\FmLib\Http\Controllers\BaseController::transformByType
177
   * @uses   \Tfboe\FmLib\Http\Controllers\BaseController::__construct
178
   */
179
  public function testTransformValueByTypeDefault()
180
  {
181
    $value = "2005-02-28 16:35:01";
182
    $specification = ['type' => 'default'];
183
184
    $controller = $this->controller();
185
    /** @noinspection PhpUnhandledExceptionInspection */
186
    $method = self::getMethod(UserController::class, 'transformValue');
187
    $method->invokeArgs($controller, [&$value, $specification]);
188
189
    self::assertEquals("2005-02-28 16:35:01", $value);
190
  }
191
192
  /**
193
   * @covers \Tfboe\FmLib\Http\Controllers\BaseController::validateBySpecification
194
   * @uses   \Tfboe\FmLib\Http\Controllers\BaseController::__construct
195
   */
196
  public function testValidateBySpecification()
197
  {
198
    $controller = $this->getMockForAbstractClass(BaseController::class,
199
      [$this->createMock(EntityManagerInterface::class)], '', true, true, true, ['validate']);
200
    $request = $this->createMock(Request::class);
201
    $controller->expects(static::once())->method('validate')
202
      ->with($request, ['withValidation' => 'required|string|min:2']);
203
    /** @var BaseController $controller */
204
    $specification = [
205
      'noValidation' => ['default' => 5],
206
      'withValidation' => ['validation' => 'required|string|min:2']
207
    ];
208
    /** @noinspection PhpUnhandledExceptionInspection */
209
    $method = self::getMethod(UserController::class, 'validateBySpecification');
210
    $method->invokeArgs($controller, [$request, $specification]);
211
  }
212
213
  /**
214
   * @covers \Tfboe\FmLib\Http\Controllers\BaseController::getDatetimetzFormat
215
   * @uses   \Tfboe\FmLib\Http\Controllers\BaseController::__construct
216
   */
217
  public function testGetDatetimetzFormat()
218
  {
219
    $controller = $this->controller();
220
    $format = static::callProtectedMethod($controller, "getDatetimetzFormat");
221
    self::assertEquals('Y-m-d H:i:s e', $format);
222
  }
223
224
  /**
225
   * @covers \Tfboe\FmLib\Http\Controllers\BaseController::getEntityManager
226
   * @uses   \Tfboe\FmLib\Http\Controllers\BaseController::__construct
227
   */
228
  public function testGetEntityManager()
229
  {
230
    $entityManager = $this->createMock(EntityManagerInterface::class);
231
    $controller = $this->getMockForAbstractClass(BaseController::class, [$entityManager]);
232
    $em = static::callProtectedMethod($controller, "getEntityManager");
233
    self::assertEquals($entityManager, $em);
234
  }
235
//</editor-fold desc="Public Methods">
236
237
//<editor-fold desc="Private Methods">
238
  /**
239
   * @return MockObject|BaseController
240
   */
241
  private function controller(): MockObject
242
  {
243
    return $this->getMockForAbstractClass(BaseController::class, [
244
      $this->createMock(EntityManagerInterface::class)
245
    ]);
246
  }
247
//</editor-fold desc="Private Methods">
248
}