Skip to content

Commit

Permalink
moar
Browse files Browse the repository at this point in the history
  • Loading branch information
cmb69 committed Mar 22, 2023
1 parent f3febc7 commit f51dfd8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 45 deletions.
33 changes: 28 additions & 5 deletions classes/VisualCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,73 +403,96 @@ public function createErrorImage(string $text)
return $img;
}

/** @codeCoverageIgnore */
protected function randomFont(int $count): int
{
return mt_rand(0, $count - 1);
}

/** @codeCoverageIgnore */
protected function randomAngle(int $max): int
{
return mt_rand(1, 2) == 1
? mt_rand(0, $max)
: mt_rand(360 - $max, 360);
}

/** @codeCoverageIgnore */
protected function randomCharSize(int $min, int $max): int
{
return mt_rand($min, $max);
}

/** @codeCoverageIgnore */
protected function randomDisplacement(int $min, int $max): int
{
return mt_rand($min, $max);
}

/** @codeCoverageIgnore */
protected function randomBackgroundImage(int $count): int
{
return mt_rand(0, $count - 1);
}

/** @return array{int,int,int} */
/**
* @return array{int,int,int}
* @codeCoverageIgnore
*/
protected function randomNoiseColor(): array
{
return [mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)];
}

/** @return array{int,int,int} */
/**
* @return array{int,int,int}
* @codeCoverageIgnore
*/
protected function randomCharColor(): array
{
return [mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)];
}

/** @codeCoverageIgnore */
protected function randomPointCount(int $min, int $max): int
{
return mt_rand($min, $max);
}

/** @codeCoverageIgnore */
protected function randomLineCount(int $min, int $max): int
{
return mt_rand($min, $max);
}

/** @codeCoverageIgnore */
protected function randomCircleCount(int $min, int $max): int
{
return mt_rand($min, $max);
}

/** @return array{int,int} */
/**
* @return array{int,int}
* @codeCoverageIgnore
*/
protected function randomPoint(int $width, int $height): array
{
return [mt_rand(0, $width - 1), mt_rand(0, $height - 1)];
}

/** @return array{int,int,int,int} */
/**
* @return array{int,int,int,int}
* @codeCoverageIgnore
*/
protected function randomLine(int $width, int $height): array
{
return [mt_rand(0, $width - 1), mt_rand(0, $height - 1),mt_rand(0, $width - 1), mt_rand(0, $height - 1)];
}

/** @return array{int,int,int} */
/**
* @return array{int,int,int}
* @codeCoverageIgnore
*/
protected function randomCircle(int $width, int $height): array
{
$diameter = mt_rand(5, $width / 3);
Expand Down
69 changes: 29 additions & 40 deletions tests/VisualCaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

namespace Cryptographp;

use ApprovalTests\Approvals;
use PHPUnit\Framework\TestCase;
use org\bovigo\vfs\vfsStream;

Expand Down Expand Up @@ -69,37 +68,37 @@ public function setUp(): void

public function testCreateImage()
{
$sut = new FakeVisualCaptcha(vfsStream::url('test/images/'), '../cryptographp/fonts', self::CONFIG);
$sut = new FakeVisualCaptcha(vfsStream::url('test/images/'), '../cryptographp/fonts', $this->conf());
$actual = $sut->createImage('ABCD');
$this->assertImageEquals('image', $actual);
}

public function testCreateErrorImage()
{
$sut = new FakeVisualCaptcha(vfsStream::url('test/images/'), '../cryptographp/fonts', self::CONFIG);
$actual = $sut->createErrorImage('Cookies must be enabled!');
$this->assertImageEquals('error_image', $actual);
}
// public function testCreateErrorImage()
// {
// $sut = new FakeVisualCaptcha(vfsStream::url('test/images/'), '../cryptographp/fonts', $this->conf());
// $actual = $sut->createErrorImage('Cookies must be enabled!');
// $this->assertImageEquals('error_image', $actual);
// }

public function testNoiseAbove()
{
$config = array_merge(self::CONFIG, ['noise_above' => 'true']);
$config = ['noise_above' => 'true'] + $this->conf();
$sut = new FakeVisualCaptcha(vfsStream::url('test/images/'), '../cryptographp/fonts', $config);
$actual = $sut->createImage('ABCD');
$this->assertImageEquals('noise_above', $actual);
}

public function testCryptGrayScale()
{
$config = array_merge(self::CONFIG, ['crypt_gray_scale' => 'true']);
$config = ['crypt_gray_scale' => 'true'] + $this->conf();
$sut = new FakeVisualCaptcha(vfsStream::url('test/images/'), '../cryptographp/fonts', $config);
$actual = $sut->createImage('ABCD');
$this->assertImageEquals('gray_scale', $actual);
}

public function testCryptGaussianBlur()
{
$config = array_merge(self::CONFIG, ['crypt_gaussian_blur' => 'true']);
$config = ['crypt_gaussian_blur' => 'true'] + $this->conf();
$sut = new FakeVisualCaptcha(vfsStream::url('test/images/'), '../cryptographp/fonts', $config);
$actual = $sut->createImage('ABCD');
$this->assertImageEquals('gaussian_blur', $actual);
Expand All @@ -108,11 +107,11 @@ public function testCryptGaussianBlur()
/** @dataProvider bgImages */
public function testBgImage(string $type)
{
$config = array_merge(self::CONFIG, ['bg_image' => "bg.png"]);
$config = ['bg_image' => "bg.$type"] + $this->conf();
$sut = new FakeVisualCaptcha(vfsStream::url('test/images/'), '../cryptographp/fonts', $config);
$this->createYellowBackgroundImage("png");
$this->createYellowBackgroundImage($type);
$actual = $sut->createImage('ABCD');
$this->assertImageEquals('bg_image', $actual);
$this->assertImageEquals("bg_image", $actual);
}

public function bgImages(): array
Expand All @@ -126,18 +125,15 @@ public function bgImages(): array

public function testBgImages()
{
$config = array_merge(self::CONFIG, ['bg_image' => "."]);
$config = ['bg_image' => "."] + $this->conf();
$sut = new FakeVisualCaptcha(vfsStream::url('test/images/'), '../cryptographp/fonts', $config);
$this->createYellowBackgroundImage('png');
$this->createYellowBackgroundImage('gif');
$actual = $sut->createImage('ABCD');
$this->assertImageEquals('bg_images', $actual);
}

/**
* @param string $type
*/
private function createYellowBackgroundImage($type)
private function createYellowBackgroundImage(string $type)
{
$backgroundImage = imagecreate(130, 40);
imagecolorallocate($backgroundImage, 255, 255, 0);
Expand All @@ -148,7 +144,7 @@ private function createYellowBackgroundImage($type)
/** @dataProvider noiseColors */
public function testNoiseColor(string $kind, string $expected)
{
$config = array_merge(self::CONFIG, ['noise_color' => $kind]);
$config = ['noise_color' => $kind] + $this->conf();
$sut = new FakeVisualCaptcha(vfsStream::url('test/images/'), '../cryptographp/fonts', $config);
$actual = $sut->createImage('ABCD');
$this->assertImageEquals($expected, $actual);
Expand All @@ -162,22 +158,26 @@ public function noiseColors(): array
];
}

/**
* @link https://github.com/cmb69/cryptographp_xh/issues/5
*/
public function testWordwrapInErrorImage()
// /**
// * @link https://github.com/cmb69/cryptographp_xh/issues/5
// */
// public function testWordwrapInErrorImage()
// {
// $sut = new FakeVisualCaptcha(vfsStream::url('test/images/'), '../cryptographp/fonts', self::CONFIG);
// $actual = $sut->createErrorImage('Перезагрузка слишком быстро!');
// $this->assertImageEquals('word_wrap_in_error', $actual);
// }

private function conf(): array
{
$sut = new FakeVisualCaptcha(vfsStream::url('test/images/'), '../cryptographp/fonts', self::CONFIG);
$actual = $sut->createErrorImage('Перезагрузка слишком быстро!');
$this->assertImageEquals('word_wrap_in_error', $actual);
return XH_includeVar("./config/config.php", "plugin_cf")["cryptographp"];
}

/**
* @param string $expected
* @param resource $actual
* @return void
*/
private function assertImageEquals($expected, $actual)
private function assertImageEquals(string $expected, $actual)
{
$im1 = imagecreatefrompng(__DIR__ . "/images/$expected.png");
$im2 = $actual;
Expand Down Expand Up @@ -216,23 +216,12 @@ private function assertImageEquals($expected, $actual)

$d = sqrt(($r1 - $r2)**2 + ($g1 - $g2)**2 + ($b1 - $b2)**2) / sqrt(3 * 255**2);
$difference += $d;

$a3 = 127 - (int) (127 * $d);
$r3 = (int) (sqrt($r1**2 + $r2**2) / sqrt(2 * 255**2) * 255);
$g3 = (int) (sqrt($g1**2 + $g2**2) / sqrt(2 * 255**2) * 255);
$b3 = (int) (sqrt($b1**2 + $b2**2) / sqrt(2 * 255**2) * 255);

$c3 = ($a3 << 24) | ($r3 << 16) | ($g3 << 8) | $b3;

imagesetpixel($im3, $i, $j, $c3);
}
}

if ($difference > 0.0) {
imagesavealpha($im2, true);
imagepng($im2, __DIR__ . "/images/$expected.out.png");
imagesavealpha($im3, true);
imagepng($im3, __DIR__ . "/images/$expected.diff.png");
}

return $this->assertEqualsWithDelta(0.0, $difference, 100);
Expand Down

0 comments on commit f51dfd8

Please sign in to comment.