LumenTestCase::checkProperties()   B
last analyzed

Complexity

Conditions 9
Paths 39

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 39
nop 4
dl 0
loc 36
rs 8.0555
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Class TestCase
6
 */
7
8
namespace Tfboe\FmLib\TestHelpers;
9
10
use Laravel\Lumen\Testing\TestCase;
11
12
/**
13
 * Class TestCase
14
 * @package Tfboe\FmLib\TestHelpers
15
 */
16
abstract class LumenTestCase extends TestCase
17
{
18
  use ReflectionMethods;
19
  use OnlyTestLogging;
20
21
//<editor-fold desc="Protected Methods">
22
23
  /**
24
   * Checks for a given object if the given properties got set correctly by the query data.
25
   * @param mixed[] $data the request data
26
   * @param mixed $object the object whose properties to check
27
   * @param mixed[] $properties the properties to check, property name maps to the default value (if not set in request)
28
   * @param mixed[] $enumProperties the enum properties to check, property name maps to an info array, which contains
29
   *                                the enum name and the default value
30
   */
31
  protected function checkProperties(array $data, $object, array $properties, array $enumProperties = [])
32
  {
33
    foreach ($properties as $property => $default) {
34
      $getter = 'get' . ucfirst($property);
35
      if (!method_exists($object, $getter)) {
36
        $getter = 'is' . ucfirst($property);
37
      }
38
      $transformer = null;
39
      if (is_array($default) && array_key_exists('transformer', $default)) {
40
        $transformer = $default['transformer'];
41
        $default = $default['default'];
42
      }
43
      if (array_key_exists($property, $data)) {
44
        $value = $data[$property];
45
        if ($transformer != null) {
46
          $value = $transformer($value);
47
        }
48
        self::assertEquals($value, $object->$getter());
49
      } else {
50
        self::assertEquals($default, $object->$getter());
51
      }
52
    }
53
54
    foreach ($enumProperties as $property => $info) {
55
      $enumClass = $info['enum'];
56
      $default = $info['default'];
57
      $getter = 'get' . ucfirst($property);
58
      if (array_key_exists($property, $data)) {
59
        $name = $data[$property];
60
        /** @noinspection PhpUndefinedMethodInspection */
61
        self::assertEquals($enumClass::getValue($name), $object->$getter());
62
      } else {
63
        self::assertEquals($default, $object->$getter());
64
      }
65
    }
66
  }
67
//</editor-fold desc="Protected Methods">
68
}
69