CaptchaTest::testRender()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 28
rs 8.8571
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__;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = 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...
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);
0 ignored issues
show
Coding Style introduced by
$text_color does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
109
        imagestring($image, 1, 5, 5, 'A Text String', $text_color);
0 ignored issues
show
Coding Style introduced by
$text_color does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
110
111
        // lets pretend that this is the generated captcha
112
        $this->object->captcha = $image;
113
114
        // now test output methods
115
116
        /* @todo
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
          $render_type = 'file';
118
          $this->object->render($render_type);
119
         */
120
121
        $render_type = 'base64';
0 ignored issues
show
Coding Style introduced by
$render_type does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
122
        $result      = $this->object->render($render_type);
0 ignored issues
show
Coding Style introduced by
$render_type does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
123
        $this->assertContains(
124
            '<img alt="Embedded Captcha Image" src="data:image/png;base64,', $result
125
        );
126
127
        /* @todo
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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