|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KochTest\Captcha; |
|
4
|
|
|
|
|
5
|
|
|
use Koch\Captcha\Captcha; |
|
6
|
|
|
|
|
7
|
|
|
class CaptchaTest extends \PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var Captcha |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $object; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Sets up the fixture, for example, opens a network connection. |
|
16
|
|
|
* This method is called before a test is executed. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
if (extension_loaded('gd') === false) { |
|
21
|
|
|
$this->markTestSkipped('This test requires the PHP extension "gd".'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
// set captcha folder |
|
25
|
|
|
$options['captcha_dir'] = __DIR__; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
$this->object = new Captcha($options); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Tears down the fixture, for example, closes a network connection. |
|
32
|
|
|
* This method is called after a test is executed. |
|
33
|
|
|
*/ |
|
34
|
|
|
public function tearDown() |
|
35
|
|
|
{ |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testSetFontFolder() |
|
39
|
|
|
{ |
|
40
|
|
|
// accepts string |
|
41
|
|
|
$this->object->setFontFolder('folder'); |
|
42
|
|
|
|
|
43
|
|
|
$expectedFolders = $this->object->getFontFolders(); |
|
44
|
|
|
// note: $expected_folders[0] is the path to the framework's font folder |
|
45
|
|
|
$this->assertEquals('folder', $expectedFolders[1]); |
|
46
|
|
|
|
|
47
|
|
|
// accepts array |
|
48
|
|
|
$folders = ['folder/A', 'folder/B']; |
|
49
|
|
|
|
|
50
|
|
|
$this->object->setFontFolder($folders); |
|
51
|
|
|
|
|
52
|
|
|
$expectedFolders = array_merge($expectedFolders, $folders); |
|
53
|
|
|
$this->assertEquals($expectedFolders, $this->object->getFontFolders()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @covers Koch\Captcha\Captcha::getRandomFont |
|
58
|
|
|
*/ |
|
59
|
|
|
public function testGetRandomFont() |
|
60
|
|
|
{ |
|
61
|
|
|
$font = $this->object->getRandomFont(); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertContains('.ttf', $font); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @covers Koch\Captcha\Captcha::generateRandomString |
|
68
|
|
|
*/ |
|
69
|
|
|
public function testGenerateRandomString() |
|
70
|
|
|
{ |
|
71
|
|
|
$length = 5; |
|
72
|
|
|
$randomString = $this->object->generateRandomString($length); |
|
73
|
|
|
|
|
74
|
|
|
// test valid chars, length, excluded chars |
|
75
|
|
|
$constraint = $this->logicalAnd( |
|
76
|
|
|
$this->isType('string'), $this->matchesRegularExpression('/[a-zA-Z0-9]{5}/i'), $this->logicalNot( |
|
77
|
|
|
$this->matchesRegularExpression('/[017IO]/') // not case-[i]nsensitve |
|
78
|
|
|
) |
|
79
|
|
|
); |
|
80
|
|
|
$this->assertThat($randomString, $constraint); |
|
81
|
|
|
|
|
82
|
|
|
// silly random test |
|
83
|
|
|
$this->assertNotEquals($randomString, $this->object->generateRandomString($length)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @covers Koch\Captcha\Captcha::generateCaptchaImage |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testGenerateCaptchaImage() |
|
90
|
|
|
{ |
|
91
|
|
|
$font = $this->object->getRandomFont(); |
|
92
|
|
|
$this->object->setFont($font); |
|
93
|
|
|
|
|
94
|
|
|
// base64 embedded captcha image |
|
95
|
|
|
$result = $this->object->generateCaptchaImage(); |
|
96
|
|
|
$this->assertContains( |
|
97
|
|
|
'<img alt="Embedded Captcha Image" src="data:image/png;base64,', $result |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @covers Koch\Captcha\Captcha::render |
|
103
|
|
|
*/ |
|
104
|
|
|
public function testRender() |
|
105
|
|
|
{ |
|
106
|
|
|
// lets generate a simple image |
|
107
|
|
|
$image = imagecreatetruecolor(120, 20); |
|
108
|
|
|
$text_color = imagecolorallocate($image, 233, 14, 91); |
|
|
|
|
|
|
109
|
|
|
imagestring($image, 1, 5, 5, 'A Text String', $text_color); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
// lets pretend that this is the generated captcha |
|
112
|
|
|
$this->object->captcha = $image; |
|
113
|
|
|
|
|
114
|
|
|
// now test output methods |
|
115
|
|
|
|
|
116
|
|
|
/* @todo |
|
|
|
|
|
|
117
|
|
|
$render_type = 'file'; |
|
118
|
|
|
$this->object->render($render_type); |
|
119
|
|
|
*/ |
|
120
|
|
|
|
|
121
|
|
|
$render_type = 'base64'; |
|
|
|
|
|
|
122
|
|
|
$result = $this->object->render($render_type); |
|
|
|
|
|
|
123
|
|
|
$this->assertContains( |
|
124
|
|
|
'<img alt="Embedded Captcha Image" src="data:image/png;base64,', $result |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
/* @todo |
|
|
|
|
|
|
128
|
|
|
$render_type = 'png'; |
|
129
|
|
|
$this->object->render($render_type); |
|
130
|
|
|
*/ |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @covers Koch\Captcha\Captcha::collectGarbage |
|
135
|
|
|
*/ |
|
136
|
|
|
public function testCollectGarbage() |
|
137
|
|
|
{ |
|
138
|
|
|
$this->assertTrue($this->object->collectGarbage()); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
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:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.