PHP Classes
elePHPant
Icontem

File: tests/Service/OneClickCaptchaServiceTest.php

Recommend this page to a friend!
  Classes of Kacper Rowinski  >  OneClickCaptcha  >  tests/Service/OneClickCaptchaServiceTest.php  >  Download  
File: tests/Service/OneClickCaptchaServiceTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test script
Class: OneClickCaptcha
CAPTCHA validation based on user clicks on circles
Author: By
Last change: rewrited to php 7.1
Date: 1 year ago
Size: 2,930 bytes
 

 

Contents

Class file image Download
<?php
declare(strict_types=1);

use
Imagine\Draw\DrawerInterface;
use
Imagine\Image\ImageInterface;
use
Imagine\Image\ImagineInterface;
use
Imagine\Image\Palette\Color\ColorInterface;
use
Imagine\Image\Palette\PaletteInterface;
use
OneClickCaptcha\Config\Config;
use
OneClickCaptcha\Proxy\ImageProxy;
use
OneClickCaptcha\Repository\StorageInterface;
use
OneClickCaptcha\Service\OneClickCaptchaService;
use
PHPUnit\Framework\MockObject\MockObject;
use
PHPUnit\Framework\TestCase;

/**
 * Class OneClickCaptchaServiceTest
 */
class OneClickCaptchaServiceTest extends TestCase
{
   
/**
     * @var OneClickCaptchaService
     */
   
private $oneClickCaptchaService;

    public function
setUp()
    {
       
$configStub = new Config();
       
/** @var StorageInterface|MockObject $postStub */
       
$postStub = $this->createMock(StorageInterface::class);
       
$postStub->method('get')->willReturnCallback(
            function (
$name) {
                if (
StorageInterface::LAST_REQUEST === $name) {
                    return [
                       
StorageInterface::POSITION_X => 1,
                       
StorageInterface::POSITION_Y => 1,
                    ];
                }

                return
1;
            }
        );

       
$imageInterface = $this->createMock(ImageInterface::class);
       
$imageInterface->method('draw')->willReturn($this->createMock(DrawerInterface::class));

       
$imagineInterfaceStub = $this->createMock(ImagineInterface::class);
       
$imagineInterfaceStub->method('create')->willReturn($imageInterface);

       
$palette = $this->createMock(PaletteInterface::class);
       
$palette->method('color')->willReturn($this->createMock(ColorInterface::class));

       
/** @var ImageProxy|MockObject $imagineProxyStub */
       
$imagineProxyStub = $this->createMock(ImageProxy::class);
       
$imagineProxyStub->method('getImage')->willReturn($imagineInterfaceStub);
       
$imagineProxyStub->method('getRGB')->willReturn($palette);

       
$this->oneClickCaptchaService = new OneClickCaptchaService($configStub, $postStub, $imagineProxyStub);
    }

   
/**
     * @test
     * @throws Exception
     */
   
public function shouldShowCaptcha(): void
   
{
       
$this->oneClickCaptchaService->showCaptcha();

       
$headers = xdebug_get_headers();

       
$this->assertEquals('Expires: Thu, 19 Nov 1981 08:52:00 GMT', $headers[1]);
       
$this->assertEquals('Pragma: public', $headers[2]);
       
$this->assertEquals('Cache-Control: public', $headers[3]);
    }

   
/**
     * @test
     */
   
public function shouldValidate(): void
   
{
       
$this->assertFalse($this->oneClickCaptchaService->validate(-1, -5));
       
$this->assertFalse($this->oneClickCaptchaService->validate(2, 2));
       
$this->assertTrue($this->oneClickCaptchaService->validate(1, 1));
    }
}