SubClassDataTest::mock()   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: 1/3/18
7
 * Time: 12:29 PM
8
 */
9
10
namespace Tfboe\FmLib\Tests\Unit\Entity\Helpers;
11
12
13
use Tfboe\FmLib\Entity\Helpers\SubClassData;
14
use Tfboe\FmLib\Exceptions\MethodNotExistingException;
15
use Tfboe\FmLib\Exceptions\PropertyNotExistingException;
16
use Tfboe\FmLib\Tests\Helpers\UnitTestCase;
17
18
/**
19
 * Class SubClassDataTest
20
 * @package Tfboe\FmLib\Tests\Unit\Entity\Helpers
21
 */
22
class SubClassDataTest extends UnitTestCase
23
{
24
//<editor-fold desc="Public Methods">
25
  /**
26
   * @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::addPropertyIfNotExistent
27
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::__call
28
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::getProperty
29
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::hasProperty
30
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData
31
   */
32
  public function testAddPropertyIfNotExistent()
33
  {
34
    $entity = $this->mock();
35
    $entity->initSubClassData([]);
36
    self::assertFalse($entity->hasProperty("prop"));
37
    $entity->addPropertyIfNotExistent("prop", "default");
38
    /** @noinspection PhpUndefinedMethodInspection */
39
    self::assertEquals("default", $entity->getProp());
0 ignored issues
show
Bug introduced by
The method getProp() does not exist on Tfboe\FmLib\Entity\Helpers\SubClassData. Did you maybe mean getProperty()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
40
    $entity->addPropertyIfNotExistent("prop", "other");
41
    /** @noinspection PhpUndefinedMethodInspection */
42
    self::assertEquals("default", $entity->getProp());
0 ignored issues
show
Bug introduced by
The method getProp() does not exist on Tfboe\FmLib\Entity\Helpers\SubClassData. Did you maybe mean getProperty()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
43
  }
44
45
  /**
46
   * @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::__call
47
   * @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::getProperty
48
   * @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::setProperty
49
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::hasProperty
50
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData
51
   */
52
  public function testCall()
53
  {
54
    $entity = $this->mock();
55
    $entity->initSubClassData(["Prop"]);
56
    /** @noinspection PhpUndefinedMethodInspection */
57
    self::assertNull($entity->getProp());
0 ignored issues
show
Bug introduced by
The method getProp() does not exist on Tfboe\FmLib\Entity\Helpers\SubClassData. Did you maybe mean getProperty()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
58
    /** @noinspection PhpUndefinedMethodInspection */
59
    $entity->setProp("test");
0 ignored issues
show
Bug introduced by
The method setProp() does not exist on Tfboe\FmLib\Entity\Helpers\SubClassData. Did you maybe mean setProperty()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
60
    /** @noinspection PhpUndefinedMethodInspection */
61
    self::assertEquals("test", $entity->getProp());
0 ignored issues
show
Bug introduced by
The method getProp() does not exist on Tfboe\FmLib\Entity\Helpers\SubClassData. Did you maybe mean getProperty()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
62
    /** @noinspection PhpUndefinedMethodInspection */
63
    self::assertEquals("test", $entity->isProp());
0 ignored issues
show
Documentation Bug introduced by
The method isProp does not exist on object<Tfboe\FmLib\Entity\Helpers\SubClassData>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
64
  }
65
66
  /**
67
   * @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::cloneSubClassDataFrom
68
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::hasProperty
69
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData
70
   */
71
  public function testCloneSubClassDataFrom()
72
  {
73
    $entity = $this->mock();
74
    $other = $this->getMockForTrait(SubClassData::class, [], get_class($entity));
75
    /** @var SubClassData $other */
76
    $other->initSubClassData(['test']);
77
    $entity->initSubClassData(['other']);
78
    self::assertFalse($entity->hasProperty('test'));
79
    self::assertTrue($entity->hasProperty('other'));
80
    $entity->cloneSubClassDataFrom($other);
81
    self::assertTrue($entity->hasProperty('test'));
82
    self::assertFalse($entity->hasProperty('other'));
83
  }
84
85
  /**
86
   * @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData
87
   * @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::hasProperty
88
   */
89 View Code Duplication
  public function testInitSubClassDataAndHasProperty()
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...
90
  {
91
    $entity = $this->mock();
92
    /** @noinspection SpellCheckingInspection */
93
    $entity->initSubClassData(["TESTUPPER", "testlower"]);
94
    /** @noinspection SpellCheckingInspection */
95
    self::assertTrue($entity->hasProperty("testupper"));
96
    /** @noinspection SpellCheckingInspection */
97
    self::assertTrue($entity->hasProperty("TESTUPPER"));
98
    /** @noinspection SpellCheckingInspection */
99
    self::assertTrue($entity->hasProperty("TESTupper"));
100
    /** @noinspection SpellCheckingInspection */
101
    self::assertTrue($entity->hasProperty("testlower"));
102
    /** @noinspection SpellCheckingInspection */
103
    self::assertTrue($entity->hasProperty("TESTLOWER"));
104
    /** @noinspection SpellCheckingInspection */
105
    self::assertTrue($entity->hasProperty("TESTlower"));
106
    /** @noinspection SpellCheckingInspection */
107
    self::assertFalse($entity->hasProperty("notexisting"));
108
    /** @noinspection SpellCheckingInspection */
109
    self::assertFalse($entity->hasProperty("NOTEXISTING"));
110
    /** @noinspection SpellCheckingInspection */
111
    self::assertFalse($entity->hasProperty("NOTexistING"));
112
    self::assertTrue($entity->hasProperty("subClassData"));
113
    /** @noinspection SpellCheckingInspection */
114
    self::assertFalse($entity->hasProperty("SUBCLASSDATA"));
115
  }
116
117
  /**
118
   * @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::methodExists
119
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::addPropertyIfNotExistent
120
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::hasProperty
121
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData
122
   */
123 View Code Duplication
  public function testMethodExists()
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...
124
  {
125
    $entity = $this->mock();
126
    $entity->initSubClassData(["test"]);
127
    self::assertTrue($entity->methodExists("methodExists"));
128
    self::assertFalse($entity->methodExists("notExistingMethod"));
129
    self::assertFalse($entity->methodExists("getProp"));
130
    self::assertFalse($entity->methodExists("isProp"));
131
    self::assertFalse($entity->methodExists("setProp"));
132
    self::assertTrue($entity->methodExists("getTest"));
133
    self::assertTrue($entity->methodExists("isTest"));
134
    self::assertTrue($entity->methodExists("setTest"));
135
    $entity->addPropertyIfNotExistent("prop", null);
136
    self::assertTrue($entity->methodExists("getProp"));
137
    self::assertTrue($entity->methodExists("isProp"));
138
    self::assertTrue($entity->methodExists("setProp"));
139
  }
140
141
  /**
142
   * @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::__call
143
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData
144
   * @uses   \Tfboe\FmLib\Exceptions\MethodNotExistingException::__construct
145
   */
146 View Code Duplication
  public function testNotExistingMethodCall()
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...
147
  {
148
    $entity = $this->mock();
149
    $entity->initSubClassData([]);
150
    $this->expectException(MethodNotExistingException::class);
151
    $this->expectExceptionMessage("An object of the class " . get_class($entity) . " had no method notExistingMethod");
152
    /** @noinspection PhpUndefinedMethodInspection */
153
    $entity->notExistingMethod();
0 ignored issues
show
Documentation Bug introduced by
The method notExistingMethod does not exist on object<Tfboe\FmLib\Entity\Helpers\SubClassData>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
154
  }
155
156
  /**
157
   * @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::__call
158
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData
159
   * @uses   \Tfboe\FmLib\Exceptions\MethodNotExistingException::__construct
160
   */
161 View Code Duplication
  public function testNotExistingMethodSetterNoArgument()
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...
162
  {
163
    $entity = $this->mock();
164
    $entity->initSubClassData([]);
165
    $this->expectException(MethodNotExistingException::class);
166
    $this->expectExceptionMessage("An object of the class " . get_class($entity) . " had no method setProp");
167
    /** @noinspection PhpUndefinedMethodInspection */
168
    $entity->setProp();
0 ignored issues
show
Bug introduced by
The method setProp() does not exist on Tfboe\FmLib\Entity\Helpers\SubClassData. Did you maybe mean setProperty()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
169
  }
170
171
  /**
172
   * @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::__call
173
   * @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::getProperty
174
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData
175
   * @uses   \Tfboe\FmLib\Exceptions\PropertyNotExistingException::__construct
176
   */
177 View Code Duplication
  public function testNotExistingPropertyGetCall()
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...
178
  {
179
    $entity = $this->mock();
180
    $entity->initSubClassData([]);
181
    $this->expectException(PropertyNotExistingException::class);
182
    $this->expectExceptionMessage("An object of the class " . get_class($entity) .
183
      " had no property prop via getProperty");
184
    /** @noinspection PhpUndefinedMethodInspection */
185
    $entity->getProp();
0 ignored issues
show
Bug introduced by
The method getProp() does not exist on Tfboe\FmLib\Entity\Helpers\SubClassData. Did you maybe mean getProperty()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
186
  }
187
188
  /**
189
   * @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::__call
190
   * @covers \Tfboe\FmLib\Entity\Helpers\SubClassData::setProperty
191
   * @uses   \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData
192
   * @uses   \Tfboe\FmLib\Exceptions\PropertyNotExistingException::__construct
193
   */
194 View Code Duplication
  public function testNotExistingPropertySetCall()
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...
195
  {
196
    $entity = $this->mock();
197
    $entity->initSubClassData([]);
198
    $this->expectException(PropertyNotExistingException::class);
199
    $this->expectExceptionMessage("An object of the class " . get_class($entity) .
200
      " had no property prop via setProperty");
201
    /** @noinspection PhpUndefinedMethodInspection */
202
    $entity->setProp(5);
0 ignored issues
show
Bug introduced by
The method setProp() does not exist on Tfboe\FmLib\Entity\Helpers\SubClassData. Did you maybe mean setProperty()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
203
  }
204
//</editor-fold desc="Public Methods">
205
206
//<editor-fold desc="Private Methods">
207
  /**
208
   * @return \PHPUnit_Framework_MockObject_MockObject|SubClassData
209
   */
210
  private function mock(): \PHPUnit_Framework_MockObject_MockObject
211
  {
212
    return $this->getMockForTrait(SubClassData::class);
213
  }
214
//</editor-fold desc="Private Methods">
215
}