diff --git a/.atoum.php b/.atoum.php new file mode 100644 index 0000000..3d84643 --- /dev/null +++ b/.atoum.php @@ -0,0 +1,30 @@ +addTestsFromDirectory(__DIR__ . '/tests/units'); + +$runner->getScore()->getCoverage()->excludeDirectory(__DIR__ . '/tests/units/mock'); + +$travis = getenv('TRAVIS'); +if ($travis) { + $script->addDefaultReport(); + $coverallsToken = getenv('COVERALLS_REPO_TOKEN'); + if ($coverallsToken) { + $coverallsReport = new reports\asynchronous\coveralls('classes', $coverallsToken); + $defaultFinder = $coverallsReport->getBranchFinder(); + $coverallsReport + ->setBranchFinder(function () use ($defaultFinder) { + if (($branch = getenv('TRAVIS_BRANCH')) === false) { + $branch = $defaultFinder(); + } + return $branch; + } + ) + ->setServiceName('travis-ci') + ->setServiceJobId(getenv('TRAVIS_JOB_ID')) + ->addDefaultWriter() + ; + $runner->addReport($coverallsReport); + } +} diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..47f4af2 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: php +php: + - 7.1 + - 7.2 + - 7.3 + - 7.4 +install: + - composer install +script: composer test diff --git a/composer.json b/composer.json index 58ccc06..891466d 100644 --- a/composer.json +++ b/composer.json @@ -30,5 +30,8 @@ "psr-4": { "ByJoby\\ImageTransform\\tests\\": "tests/" } + }, + "scripts": { + "test": "./vendor/bin/atoum -d tests" } } diff --git a/tests/units/100x200.jpg b/tests/units/100x200.jpg new file mode 100644 index 0000000..aa3bcc5 Binary files /dev/null and b/tests/units/100x200.jpg differ diff --git a/tests/units/200x100.jpg b/tests/units/200x100.jpg new file mode 100644 index 0000000..cb9088b Binary files /dev/null and b/tests/units/200x100.jpg differ diff --git a/tests/units/200x200.jpg b/tests/units/200x200.jpg new file mode 100644 index 0000000..6a4cf3e Binary files /dev/null and b/tests/units/200x200.jpg differ diff --git a/tests/units/Sizers/Cover.php b/tests/units/Sizers/Cover.php index ba9f46e..cb54cf2 100644 --- a/tests/units/Sizers/Cover.php +++ b/tests/units/Sizers/Cover.php @@ -3,8 +3,171 @@ namespace ByJoby\ImageTransform\tests\units\Sizers; use atoum; +use ByJoby\ImageTransform\Sizers\Cover as SizerUnderTest; +use ByJoby\ImageTransform\tests\units\mock\MockDriver; class Cover extends atoum { - + public function testPortraitWhenImageIsTaller() + { + $this + ->given($image = $this->image(__DIR__.'/../100x200.jpg',100,150)) + ->integer($image->width())->isEqualTo(100) + ->integer($image->height())->isEqualTo(200) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(100) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(200) + ->variable($image->sizer()->cropToWidth())->isEqualTo(100) + ->variable($image->sizer()->cropToHeight())->isEqualTo(150) + ->given($image->rotate()) + ->integer($image->width())->isEqualTo(300) + ->integer($image->height())->isEqualTo(150) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(300) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(150) + ->variable($image->sizer()->cropToWidth())->isEqualTo(100) + ->variable($image->sizer()->cropToHeight())->isEqualTo(150) + ; + } + + // public function testPortraitWhenImageIsShorter() + // { + // $this + // ->given($image = $this->image(__DIR__.'/../100x200.jpg',50,300)) + // ->integer($image->width())->isEqualTo(50) + // ->integer($image->height())->isEqualTo(100) + // ->variable($image->sizer()->resizeToWidth())->isEqualTo(50) + // ->variable($image->sizer()->resizeToHeight())->isEqualTo(100) + // ->variable($image->sizer()->cropToWidth())->isNull() + // ->variable($image->sizer()->cropToHeight())->isNull() + // ->given($image->rotate()) + // ->integer($image->width())->isEqualTo(50) + // ->integer($image->height())->isEqualTo(25) + // ->variable($image->sizer()->resizeToWidth())->isEqualTo(50) + // ->variable($image->sizer()->resizeToHeight())->isEqualTo(25) + // ->variable($image->sizer()->cropToWidth())->isNull() + // ->variable($image->sizer()->cropToHeight())->isNull() + // ; + // } + + // public function testPortraitWithSquareImage() + // { + // $this + // ->given($image = $this->image(__DIR__.'/../200x200.jpg',75,300)) + // ->integer($image->width())->isEqualTo(75) + // ->integer($image->height())->isEqualTo(75) + // ->variable($image->sizer()->resizeToWidth())->isEqualTo(75) + // ->variable($image->sizer()->resizeToHeight())->isEqualTo(75) + // ->variable($image->sizer()->cropToWidth())->isNull() + // ->variable($image->sizer()->cropToHeight())->isNull() + // ; + // } + + // public function testPortraitWithLandscapeImage() + // { + // $this + // ->given($image = $this->image(__DIR__.'/../200x100.jpg',100,200)) + // ->integer($image->width())->isEqualTo(100) + // ->integer($image->height())->isEqualTo(50) + // ->variable($image->sizer()->resizeToWidth())->isEqualTo(100) + // ->variable($image->sizer()->resizeToHeight())->isEqualTo(50) + // ->variable($image->sizer()->cropToWidth())->isNull() + // ->variable($image->sizer()->cropToHeight())->isNull() + // ; + // } + + // public function testLandscapeWhenImageIsTaller() + // { + // $this + // ->given($image = $this->image(__DIR__.'/../200x100.jpg',150,100)) + // ->integer($image->width())->isEqualTo(150) + // ->integer($image->height())->isEqualTo(75) + // ->variable($image->sizer()->resizeToWidth())->isEqualTo(150) + // ->variable($image->sizer()->resizeToHeight())->isEqualTo(75) + // ->variable($image->sizer()->cropToWidth())->isNull() + // ->variable($image->sizer()->cropToHeight())->isNull() + // ; + // } + + // public function testLandscapeWhenImageIsShorter() + // { + // $this + // ->given($image = $this->image(__DIR__.'/../200x100.jpg',300,75)) + // ->integer($image->width())->isEqualTo(150) + // ->integer($image->height())->isEqualTo(75) + // ->variable($image->sizer()->resizeToWidth())->isEqualTo(150) + // ->variable($image->sizer()->resizeToHeight())->isEqualTo(75) + // ->variable($image->sizer()->cropToWidth())->isNull() + // ->variable($image->sizer()->cropToHeight())->isNull() + // ; + // } + + // public function testLandscapeWithSquareImage() + // { + // $this + // ->given($image = $this->image(__DIR__.'/../200x200.jpg',300,75)) + // ->integer($image->width())->isEqualTo(75) + // ->integer($image->height())->isEqualTo(75) + // ->variable($image->sizer()->resizeToWidth())->isEqualTo(75) + // ->variable($image->sizer()->resizeToHeight())->isEqualTo(75) + // ->variable($image->sizer()->cropToWidth())->isNull() + // ->variable($image->sizer()->cropToHeight())->isNull() + // ; + // } + + // public function testLandscapeWithPortraitImage() + // { + // $this + // ->given($image = $this->image(__DIR__.'/../100x200.jpg',200,100)) + // ->integer($image->width())->isEqualTo(50) + // ->integer($image->height())->isEqualTo(100) + // ->variable($image->sizer()->resizeToWidth())->isEqualTo(50) + // ->variable($image->sizer()->resizeToHeight())->isEqualTo(100) + // ->variable($image->sizer()->cropToWidth())->isNull() + // ->variable($image->sizer()->cropToHeight())->isNull() + // ; + // } + + // public function testWithSmallSquareImage() + // { + // $this + // ->given($image = $this->image(__DIR__.'/../200x200.jpg',300,300)) + // ->integer($image->width())->isEqualTo(300) + // ->integer($image->height())->isEqualTo(300) + // ->variable($image->sizer()->resizeToWidth())->isEqualTo(300) + // ->variable($image->sizer()->resizeToHeight())->isEqualTo(300) + // ->variable($image->sizer()->cropToWidth())->isNull() + // ->variable($image->sizer()->cropToHeight())->isNull() + // ; + // } + + // public function testWithSmallLandscapeImage() + // { + // $this + // ->given($image = $this->image(__DIR__.'/../200x100.jpg',300,300)) + // ->integer($image->width())->isEqualTo(300) + // ->integer($image->height())->isEqualTo(150) + // ->variable($image->sizer()->resizeToWidth())->isEqualTo(300) + // ->variable($image->sizer()->resizeToHeight())->isEqualTo(150) + // ->variable($image->sizer()->cropToWidth())->isNull() + // ->variable($image->sizer()->cropToHeight())->isNull() + // ; + // } + + // public function testWithSmallPortraitImage() + // { + // $this + // ->given($image = $this->image(__DIR__.'/../100x200.jpg',300,300)) + // ->integer($image->width())->isEqualTo(150) + // ->integer($image->height())->isEqualTo(300) + // ->variable($image->sizer()->resizeToWidth())->isEqualTo(150) + // ->variable($image->sizer()->resizeToHeight())->isEqualTo(300) + // ->variable($image->sizer()->cropToWidth())->isNull() + // ->variable($image->sizer()->cropToHeight())->isNull() + // ; + // } + + protected function image(string $path, int $width, int $height) + { + $driver = new MockDriver(); + return $driver->image($path, new SizerUnderTest($width, $height)); + } } diff --git a/tests/units/Sizers/Fit.php b/tests/units/Sizers/Fit.php new file mode 100644 index 0000000..d8f9865 --- /dev/null +++ b/tests/units/Sizers/Fit.php @@ -0,0 +1,173 @@ +given($image = $this->image(__DIR__.'/../100x200.jpg',100,150)) + ->integer($image->width())->isEqualTo(75) + ->integer($image->height())->isEqualTo(150) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(75) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(150) + ->variable($image->sizer()->cropToWidth())->isNull() + ->variable($image->sizer()->cropToHeight())->isNull() + ->given($image->rotate()) + ->integer($image->width())->isEqualTo(100) + ->integer($image->height())->isEqualTo(50) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(100) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(50) + ->variable($image->sizer()->cropToWidth())->isNull() + ->variable($image->sizer()->cropToHeight())->isNull() + ; + } + + public function testPortraitWhenImageIsShorter() + { + $this + ->given($image = $this->image(__DIR__.'/../100x200.jpg',50,300)) + ->integer($image->width())->isEqualTo(50) + ->integer($image->height())->isEqualTo(100) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(50) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(100) + ->variable($image->sizer()->cropToWidth())->isNull() + ->variable($image->sizer()->cropToHeight())->isNull() + ->given($image->rotate()) + ->integer($image->width())->isEqualTo(50) + ->integer($image->height())->isEqualTo(25) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(50) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(25) + ->variable($image->sizer()->cropToWidth())->isNull() + ->variable($image->sizer()->cropToHeight())->isNull() + ; + } + + public function testPortraitWithSquareImage() + { + $this + ->given($image = $this->image(__DIR__.'/../200x200.jpg',75,300)) + ->integer($image->width())->isEqualTo(75) + ->integer($image->height())->isEqualTo(75) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(75) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(75) + ->variable($image->sizer()->cropToWidth())->isNull() + ->variable($image->sizer()->cropToHeight())->isNull() + ; + } + + public function testPortraitWithLandscapeImage() + { + $this + ->given($image = $this->image(__DIR__.'/../200x100.jpg',100,200)) + ->integer($image->width())->isEqualTo(100) + ->integer($image->height())->isEqualTo(50) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(100) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(50) + ->variable($image->sizer()->cropToWidth())->isNull() + ->variable($image->sizer()->cropToHeight())->isNull() + ; + } + + public function testLandscapeWhenImageIsTaller() + { + $this + ->given($image = $this->image(__DIR__.'/../200x100.jpg',150,100)) + ->integer($image->width())->isEqualTo(150) + ->integer($image->height())->isEqualTo(75) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(150) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(75) + ->variable($image->sizer()->cropToWidth())->isNull() + ->variable($image->sizer()->cropToHeight())->isNull() + ; + } + + public function testLandscapeWhenImageIsShorter() + { + $this + ->given($image = $this->image(__DIR__.'/../200x100.jpg',300,75)) + ->integer($image->width())->isEqualTo(150) + ->integer($image->height())->isEqualTo(75) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(150) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(75) + ->variable($image->sizer()->cropToWidth())->isNull() + ->variable($image->sizer()->cropToHeight())->isNull() + ; + } + + public function testLandscapeWithSquareImage() + { + $this + ->given($image = $this->image(__DIR__.'/../200x200.jpg',300,75)) + ->integer($image->width())->isEqualTo(75) + ->integer($image->height())->isEqualTo(75) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(75) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(75) + ->variable($image->sizer()->cropToWidth())->isNull() + ->variable($image->sizer()->cropToHeight())->isNull() + ; + } + + public function testLandscapeWithPortraitImage() + { + $this + ->given($image = $this->image(__DIR__.'/../100x200.jpg',200,100)) + ->integer($image->width())->isEqualTo(50) + ->integer($image->height())->isEqualTo(100) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(50) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(100) + ->variable($image->sizer()->cropToWidth())->isNull() + ->variable($image->sizer()->cropToHeight())->isNull() + ; + } + + public function testWithSmallSquareImage() + { + $this + ->given($image = $this->image(__DIR__.'/../200x200.jpg',300,300)) + ->integer($image->width())->isEqualTo(300) + ->integer($image->height())->isEqualTo(300) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(300) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(300) + ->variable($image->sizer()->cropToWidth())->isNull() + ->variable($image->sizer()->cropToHeight())->isNull() + ; + } + + public function testWithSmallLandscapeImage() + { + $this + ->given($image = $this->image(__DIR__.'/../200x100.jpg',300,300)) + ->integer($image->width())->isEqualTo(300) + ->integer($image->height())->isEqualTo(150) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(300) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(150) + ->variable($image->sizer()->cropToWidth())->isNull() + ->variable($image->sizer()->cropToHeight())->isNull() + ; + } + + public function testWithSmallPortraitImage() + { + $this + ->given($image = $this->image(__DIR__.'/../100x200.jpg',300,300)) + ->integer($image->width())->isEqualTo(150) + ->integer($image->height())->isEqualTo(300) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(150) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(300) + ->variable($image->sizer()->cropToWidth())->isNull() + ->variable($image->sizer()->cropToHeight())->isNull() + ; + } + + protected function image(string $path, int $width, int $height) + { + $driver = new MockDriver(); + return $driver->image($path, new SizerUnderTest($width, $height)); + } +} diff --git a/tests/units/mock/MockDriver.php b/tests/units/mock/MockDriver.php new file mode 100644 index 0000000..7883a42 --- /dev/null +++ b/tests/units/mock/MockDriver.php @@ -0,0 +1,9 @@ +