1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Dmitry Gladyshev <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Rucaptcha\Test; |
7
|
|
|
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Rucaptcha\ConfigurableTrait; |
10
|
|
|
|
11
|
|
|
class ConfigurableTraitTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @expectedException \Rucaptcha\Exception\InvalidArgumentException |
15
|
|
|
*/ |
16
|
|
|
public function testSetOptionThrowsExceptionWithoutIgnoreFlag() |
17
|
|
|
{ |
18
|
|
|
$mock = $this->buildConfigurableClass(); |
19
|
|
|
$mock->setOptions(['incorrectName' => 100], false); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testSetOptionDoNotThrowsExceptionWithIgnoreFlag() |
23
|
|
|
{ |
24
|
|
|
$mock = $this->buildConfigurableClass(); |
25
|
|
|
$mock->setOptions(['incorrectName' => 100], true); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
public function testUseSetterIfExist() |
30
|
|
|
{ |
31
|
|
|
$mock = $this->buildConfigurableClass(); |
32
|
|
|
$mock->setOptions(['withSetterProperty' => 2]); |
33
|
|
|
$this->assertEquals($mock->withSetterProperty, 3); // Setter logic: 2 + 1 |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testInitPublicProperty() |
37
|
|
|
{ |
38
|
|
|
$mock = $this->buildConfigurableClass(); |
39
|
|
|
$mock->setOptions(['withoutSetterProperty' => 2]); |
40
|
|
|
$this->assertEquals($mock->withoutSetterProperty, 2); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
protected function buildConfigurableClass() |
45
|
|
|
{ |
46
|
|
|
return new MockClassForTestingConfigurableTrait(); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
class MockClassForTestingConfigurableTrait |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
use ConfigurableTrait; |
53
|
|
|
|
54
|
|
|
public $withoutSetterProperty; |
55
|
|
|
public $withSetterProperty; |
56
|
|
|
|
57
|
|
|
public function setWithSetterProperty($value) |
58
|
|
|
{ |
59
|
|
|
$this->withSetterProperty = $value + 1; |
60
|
|
|
} |
61
|
|
|
} |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.