Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonom committed Jan 17, 2018
1 parent 0e44ef2 commit ec81774
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ checks:
duplication: true

filter:
paths: [code/*, tests/*]
paths: [src/*, tests/*]
35 changes: 24 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
# See https://github.com/silverstripe-labs/silverstripe-travis-support for setup details

sudo: false

language: php

php:
- 5.6
sudo: false
dist: trusty

env:
- DB=MYSQL CORE_RELEASE=3.6
global:
- COMPOSER_ROOT_VERSION=3.0.x-dev
- SS_ENVIRONMENT_TYPE="dev"

matrix:
include:
- php: 5.6
env:
- DB=MYSQL
- php: 7.1
env:
- DB=PGSQL

before_script:
# Init PHP
- composer self-update || true
- git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support
- php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss
- cd ~/builds/ss
- composer install
- phpenv rehash
- phpenv config-rm xdebug.ini || true

# Install composer dependencies
- export PATH=~/.composer/vendor/bin:$PATH
- composer validate
- composer install --prefer-dist
- if [[ $DB == PGSQL ]]; then composer require silverstripe/postgresql:2.0.x-dev --prefer-dist; fi

script:
- vendor/bin/phpunit focuspoint/tests
- vendor/bin/phpunit
36 changes: 24 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
{
"name": "jonom/focuspoint",
"name": "jonom/focuspoint",
"type": "silverstripe-vendormodule",
"description": "Smarter automatic image cropping for SilverStripe",
"keywords": ["silverstripe", "crop", "cropping", "focus", "focuspoint", "image"],
"license": "MIT",
"authors": [{
"name": "Jonathon Menz",
"homepage": "http://jonathonmenz.com"
}],
"require": {
"description": "Smarter automatic image cropping for SilverStripe",
"keywords": [
"silverstripe",
"crop",
"cropping",
"focus",
"focuspoint",
"image"
],
"license": "MIT",
"authors": [
{
"name": "Jonathon Menz",
"homepage": "http://jonathonmenz.com"
}
],
"require": {
"silverstripe/vendor-plugin": "^1.0",
"silverstripe/framework": "^4.0"
},
"silverstripe/asset-admin": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
},
"extra": {
"expose": [
"client"
Expand All @@ -26,4 +38,4 @@
"JonoM\\FocusPoint\\Tests\\": "tests/"
}
}
}
}
14 changes: 14 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests/</directory>
</testsuite>

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
160 changes: 56 additions & 104 deletions tests/FocusPointTest.php
Original file line number Diff line number Diff line change
@@ -1,89 +1,45 @@
<?php

namespace JonoM\FocusPoint;
namespace Jonom\FocusPoint\Tests;

use SilverStripe\Assets\Image;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Folder;
use SilverStripe\ORM\DataObject;
use SilverStripe\Assets\Filesystem;
use SilverStripe\Assets\Image;
use SilverStripe\Assets\InterventionBackend;
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
use SilverStripe\Dev\SapphireTest;


class FocusPointTest extends SapphireTest
{
protected static $fixture_file = 'FocusPointTest.yml';

/**
* Set up images used for tests (Copied from framework/tests/mode/ImageTest.php).
*/
public function setUp()
{
parent::setUp();

$this->origBackend = Image::get_backend();

if ($this->skipTest) {
return;
}

if (!file_exists(ASSETS_PATH)) {
mkdir(ASSETS_PATH);
}

// Create a test folders for each of the fixture references
$folderIDs = $this->allFixtureIDs(Folder::class);

foreach ($folderIDs as $folderID) {
$folder = DataObject::get_by_id(Folder::class, $folderID);

if (!file_exists(BASE_PATH."/$folder->Filename")) {
mkdir(BASE_PATH."/$folder->Filename");
}
}
// Set backend root to /images
TestAssetStore::activate('images');

// Copy test images for each of the fixture references
$imageIDs = $this->allFixtureIDs(Image::class);
foreach ($imageIDs as $imageID) {
$image = DataObject::get_by_id(Image::class, $imageID);
$filePath = BASE_PATH."/$image->Filename";
$sourcePath = str_replace('assets/ImageTest/', FOCUSPOINT_DIR.'/tests/', $filePath);
if (!file_exists($filePath)) {
if (!copy($sourcePath, $filePath)) {
user_error('Failed to copy test images', E_USER_ERROR);
}
}
/** @var File $image */
$files = File::get()->exclude('ClassName', Folder::class);
foreach ($files as $image) {
$sourcePath = __DIR__ . '/images/' . $image->Name;
$image->setFromLocalFile($sourcePath, $image->Filename);
}

// Set default config
InterventionBackend::config()->set('error_cache_ttl', [
InterventionBackend::FAILED_INVALID => 0,
InterventionBackend::FAILED_MISSING => '5,10',
InterventionBackend::FAILED_UNKNOWN => 300,
]);
}

/**
* Remove images used for tests (Copied from framework/tests/mode/ImageTest.php).
*/
public function tearDown()
{
if ($this->origBackend) {
Image::set_backend($this->origBackend);
}

// Remove the test files that we've created
$fileIDs = $this->allFixtureIDs(Image::class);
foreach ($fileIDs as $fileID) {
$file = DataObject::get_by_id(Image::class, $fileID);
if ($file && file_exists(BASE_PATH."/$file->Filename")) {
unlink(BASE_PATH."/$file->Filename");
}
}

// Remove the test folders that we've created
$folderIDs = $this->allFixtureIDs(Folder::class);
foreach ($folderIDs as $folderID) {
$folder = DataObject::get_by_id(Folder::class, $folderID);
if ($folder && file_exists(BASE_PATH."/$folder->Filename")) {
Filesystem::removeFolder(BASE_PATH."/$folder->Filename");
}
if ($folder && file_exists(BASE_PATH.'/'.$folder->Filename.'_resampled')) {
Filesystem::removeFolder(BASE_PATH.'/'.$folder->Filename.'_resampled');
}
}

TestAssetStore::reset();
parent::tearDown();
}

Expand All @@ -93,28 +49,28 @@ public function tearDown()
public function Images()
{
$pngLeftTop = $this->objFromFixture(Image::class, 'pngLeftTop');
$pngLeftTop->VerticalSliceTopLeftColor = 'ff0000';
$pngLeftTop->VerticalSliceBottomRightColor = '00ff00';
$pngLeftTop->HorizontalSliceTopLeftColor = 'ff0000';
$pngLeftTop->HorizontalSliceBottomRightColor = 'ffff00';
$pngLeftTop->VerticalSliceTopLeftColor = '#ff0000';
$pngLeftTop->VerticalSliceBottomRightColor = '#00ff00';
$pngLeftTop->HorizontalSliceTopLeftColor = '#ff0000';
$pngLeftTop->HorizontalSliceBottomRightColor = '#ffff00';

$pngRightTop = $this->objFromFixture(Image::class, 'pngRightTop');
$pngRightTop->VerticalSliceTopLeftColor = 'ffff00';
$pngRightTop->VerticalSliceBottomRightColor = '0000ff';
$pngRightTop->HorizontalSliceTopLeftColor = 'ff0000';
$pngRightTop->HorizontalSliceBottomRightColor = 'ffff00';
$pngRightTop->VerticalSliceTopLeftColor = '#ffff00';
$pngRightTop->VerticalSliceBottomRightColor = '#0000ff';
$pngRightTop->HorizontalSliceTopLeftColor = '#ff0000';
$pngRightTop->HorizontalSliceBottomRightColor = '#ffff00';

$pngRightBottom = $this->objFromFixture(Image::class, 'pngRightBottom');
$pngRightBottom->VerticalSliceTopLeftColor = 'ffff00';
$pngRightBottom->VerticalSliceBottomRightColor = '0000ff';
$pngRightBottom->HorizontalSliceTopLeftColor = '00ff00';
$pngRightBottom->HorizontalSliceBottomRightColor = '0000ff';
$pngRightBottom->VerticalSliceTopLeftColor = '#ffff00';
$pngRightBottom->VerticalSliceBottomRightColor = '#0000ff';
$pngRightBottom->HorizontalSliceTopLeftColor = '#00ff00';
$pngRightBottom->HorizontalSliceBottomRightColor = '#0000ff';

$pngLeftBottom = $this->objFromFixture(Image::class, 'pngLeftBottom');
$pngLeftBottom->VerticalSliceTopLeftColor = 'ff0000';
$pngLeftBottom->VerticalSliceBottomRightColor = '00ff00';
$pngLeftBottom->HorizontalSliceTopLeftColor = '00ff00';
$pngLeftBottom->HorizontalSliceBottomRightColor = '0000ff';
$pngLeftBottom->VerticalSliceTopLeftColor = '#ff0000';
$pngLeftBottom->VerticalSliceBottomRightColor = '#00ff00';
$pngLeftBottom->HorizontalSliceTopLeftColor = '#00ff00';
$pngLeftBottom->HorizontalSliceBottomRightColor = '#0000ff';

return array($pngLeftTop, $pngRightTop, $pngRightBottom, $pngLeftBottom);
}
Expand All @@ -128,23 +84,21 @@ public function testFocusFill()
// Crop a vertical slice
$croppedVert = $img->FocusFill(1, 50);
$this->assertTrue($croppedVert->isSize(1, 50));
$im = imagecreatefrompng($croppedVert->getFullPath());
$leftTopColor = str_pad(dechex(imagecolorat($im, 0, 0)), 6, '0', STR_PAD_LEFT);
$bottomRightColor = str_pad(dechex(imagecolorat($im, 0, 49)), 6, '0', STR_PAD_LEFT);
$im = $croppedVert->getImageBackend()->getImageResource();
$leftTopColor = $im->pickColor(0, 0, 'hex');
$bottomRightColor = $im->pickColor(0, 49, 'hex');
$this->assertEquals($img->VerticalSliceTopLeftColor, $leftTopColor);
$this->assertEquals($img->VerticalSliceBottomRightColor, $bottomRightColor);

// Crop a horizontal slice
$croppedHorz = $img->FocusFill(50, 1);
$this->assertTrue($croppedHorz->isSize(50, 1));
$im = imagecreatefrompng($croppedHorz->getFullPath());
$leftTopColor = str_pad(dechex(imagecolorat($im, 0, 0)), 6, '0', STR_PAD_LEFT);
$bottomRightColor = str_pad(dechex(imagecolorat($im, 49, 0)), 6, '0', STR_PAD_LEFT);
$im = $croppedHorz->getImageBackend()->getImageResource();
$leftTopColor = $im->pickColor(0, 0, 'hex');
$bottomRightColor = $im->pickColor(49, 0, 'hex');
$this->assertEquals($img->HorizontalSliceTopLeftColor, $leftTopColor);
$this->assertEquals($img->HorizontalSliceBottomRightColor, $bottomRightColor);

// Clear cache since we're changing the focus point
$img->deleteFormattedImages();
}
}

Expand All @@ -158,18 +112,18 @@ public function testFocusFillMax()
// Crop a vertical slice
$croppedVert = $img->FocusFillMax(1, 50);
$this->assertTrue($croppedVert->isSize(1, 50));
$im = imagecreatefrompng($croppedVert->getFullPath());
$leftTopColor = str_pad(dechex(imagecolorat($im, 0, 0)), 6, '0', STR_PAD_LEFT);
$bottomRightColor = str_pad(dechex(imagecolorat($im, 0, 49)), 6, '0', STR_PAD_LEFT);
$im = $croppedVert->getImageBackend()->getImageResource();
$leftTopColor = $im->pickColor(0, 0, 'hex');
$bottomRightColor = $im->pickColor(0, 49, 'hex');
$this->assertEquals($img->VerticalSliceTopLeftColor, $leftTopColor);
$this->assertEquals($img->VerticalSliceBottomRightColor, $bottomRightColor);

// Crop a horizontal slice
$croppedHorz = $img->FocusFillMax(50, 1);
$this->assertTrue($croppedHorz->isSize(50, 1));
$im = imagecreatefrompng($croppedHorz->getFullPath());
$leftTopColor = str_pad(dechex(imagecolorat($im, 0, 0)), 6, '0', STR_PAD_LEFT);
$bottomRightColor = str_pad(dechex(imagecolorat($im, 49, 0)), 6, '0', STR_PAD_LEFT);
$im = $croppedHorz->getImageBackend()->getImageResource();
$leftTopColor = $im->pickColor(0, 0, 'hex');
$bottomRightColor = $im->pickColor(49, 0, 'hex');
$this->assertEquals($img->HorizontalSliceTopLeftColor, $leftTopColor);
$this->assertEquals($img->HorizontalSliceBottomRightColor, $bottomRightColor);

Expand All @@ -178,23 +132,21 @@ public function testFocusFillMax()
// Crop a vertical slice
$croppedVert = $img->FocusFillMax(1, 200);
$this->assertTrue($croppedVert->isSize(1, 100));
$im = imagecreatefrompng($croppedVert->getFullPath());
$leftTopColor = str_pad(dechex(imagecolorat($im, 0, 0)), 6, '0', STR_PAD_LEFT);
$bottomRightColor = str_pad(dechex(imagecolorat($im, 0, 99)), 6, '0', STR_PAD_LEFT);
$im = $croppedVert->getImageBackend()->getImageResource();
$leftTopColor = $im->pickColor(0, 0, 'hex');
$bottomRightColor = $im->pickColor(0, 99, 'hex');
$this->assertEquals($img->VerticalSliceTopLeftColor, $leftTopColor);
$this->assertEquals($img->VerticalSliceBottomRightColor, $bottomRightColor);

// Crop a horizontal slice
$croppedHorz = $img->FocusFillMax(200, 1);
$this->assertTrue($croppedHorz->isSize(100, 1));
$im = imagecreatefrompng($croppedHorz->getFullPath());
$leftTopColor = str_pad(dechex(imagecolorat($im, 0, 0)), 6, '0', STR_PAD_LEFT);
$bottomRightColor = str_pad(dechex(imagecolorat($im, 99, 0)), 6, '0', STR_PAD_LEFT);
$im = $croppedHorz->getImageBackend()->getImageResource();
$leftTopColor = $im->pickColor(0, 0, 'hex');
$bottomRightColor = $im->pickColor(99, 0, 'hex');
$this->assertEquals($img->HorizontalSliceTopLeftColor, $leftTopColor);
$this->assertEquals($img->HorizontalSliceBottomRightColor, $bottomRightColor);

// Clear cache since we're changing the focus point
$img->deleteFormattedImages();
}
}
}
}
Loading

0 comments on commit ec81774

Please sign in to comment.