123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- use Behat\Gherkin\Node\TableNode;
- use PHPUnit\Framework\Assert;
- require __DIR__ . '/../../vendor/autoload.php';
- trait Avatar {
-
- private $lastAvatar;
-
- public function cleanupLastAvatar() {
- $this->lastAvatar = null;
- }
- private function getLastAvatar() {
- $this->lastAvatar = '';
- $body = $this->response->getBody();
- while (!$body->eof()) {
- $this->lastAvatar .= $body->read(8192);
- }
- $body->close();
- }
-
- public function userGetsAvatarForUser(string $user, string $userAvatar) {
- $this->userGetsAvatarForUserWithSize($user, $userAvatar, '128');
- }
-
- public function userGetsAvatarForUserWithSize(string $user, string $userAvatar, string $size) {
- $this->asAn($user);
- $this->sendingToDirectUrl('GET', '/index.php/avatar/' . $userAvatar . '/' . $size);
- $this->theHTTPStatusCodeShouldBe('200');
- $this->getLastAvatar();
- }
-
- public function userGetsAvatarForGuest(string $user, string $guestAvatar) {
- $this->asAn($user);
- $this->sendingToDirectUrl('GET', '/index.php/avatar/guest/' . $guestAvatar . '/128');
- $this->theHTTPStatusCodeShouldBe('201');
- $this->getLastAvatar();
- }
-
- public function loggedInUserGetsTemporaryAvatar() {
- $this->loggedInUserGetsTemporaryAvatarWith('200');
- }
-
- public function loggedInUserGetsTemporaryAvatarWith(string $statusCode) {
- $this->sendingAToWithRequesttoken('GET', '/index.php/avatar/tmp');
- $this->theHTTPStatusCodeShouldBe($statusCode);
- $this->getLastAvatar();
- }
-
- public function loggedInUserPostsTemporaryAvatarFromFile(string $source) {
- $file = \GuzzleHttp\Psr7\Utils::streamFor(fopen($source, 'r'));
- $this->sendingAToWithRequesttoken('POST', '/index.php/avatar',
- [
- 'multipart' => [
- [
- 'name' => 'files[]',
- 'contents' => $file
- ]
- ]
- ]);
- $this->theHTTPStatusCodeShouldBe('200');
- }
-
- public function loggedInUserPostsTemporaryAvatarFromInternalPath(string $path) {
- $this->sendingAToWithRequesttoken('POST', '/index.php/avatar?path=' . $path);
- $this->theHTTPStatusCodeShouldBe('200');
- }
-
- public function loggedInUserCropsTemporaryAvatar(TableNode $crop) {
- $this->loggedInUserCropsTemporaryAvatarWith('200', $crop);
- }
-
- public function loggedInUserCropsTemporaryAvatarWith(string $statusCode, TableNode $crop) {
- $parameters = [];
- foreach ($crop->getRowsHash() as $key => $value) {
- $parameters[] = 'crop[' . $key . ']=' . $value;
- }
- $this->sendingAToWithRequesttoken('POST', '/index.php/avatar/cropped?' . implode('&', $parameters));
- $this->theHTTPStatusCodeShouldBe($statusCode);
- }
-
- public function loggedInUserDeletesTheUserAvatar() {
- $this->sendingAToWithRequesttoken('DELETE', '/index.php/avatar');
- $this->theHTTPStatusCodeShouldBe('200');
- }
-
- public function lastAvatarIsASquareOfSize(string $size) {
- [$width, $height] = getimagesizefromstring($this->lastAvatar);
- Assert::assertEquals($width, $height, 'Expected avatar to be a square');
- Assert::assertEquals($size, $width);
- }
-
- public function lastAvatarIsNotASquare() {
- [$width, $height] = getimagesizefromstring($this->lastAvatar);
- Assert::assertNotEquals($width, $height, 'Expected avatar to not be a square');
- }
-
- public function lastAvatarIsNotASingleColor() {
- Assert::assertEquals(null, $this->getColorFromLastAvatar());
- }
-
- public function lastAvatarIsASingleColor(string $color) {
- $expectedColor = $this->hexStringToRgbColor($color);
- $colorFromLastAvatar = $this->getColorFromLastAvatar();
- Assert::assertTrue($this->isSameColor($expectedColor, $colorFromLastAvatar),
- $this->rgbColorToHexString($colorFromLastAvatar) . ' does not match expected ' . $color);
- }
- private function hexStringToRgbColor($hexString) {
-
- $hexString = substr($hexString, 1);
- $rgbColorInt = hexdec($hexString);
-
-
- return [
- 'red' => ($rgbColorInt >> 16) & 0xFF,
- 'green' => ($rgbColorInt >> 8) & 0xFF,
- 'blue' => $rgbColorInt & 0xFF,
- 'alpha' => 0
- ];
- }
- private function rgbColorToHexString($rgbColor) {
- $rgbColorInt = ($rgbColor['red'] << 16) + ($rgbColor['green'] << 8) + ($rgbColor['blue']);
- return '#' . str_pad(strtoupper(dechex($rgbColorInt)), 6, '0', STR_PAD_LEFT);
- }
- private function getColorFromLastAvatar() {
- $image = imagecreatefromstring($this->lastAvatar);
- $firstPixelColorIndex = imagecolorat($image, 0, 0);
- $firstPixelColor = imagecolorsforindex($image, $firstPixelColorIndex);
- for ($i = 0; $i < imagesx($image); $i++) {
- for ($j = 0; $j < imagesx($image); $j++) {
- $currentPixelColorIndex = imagecolorat($image, $i, $j);
- $currentPixelColor = imagecolorsforindex($image, $currentPixelColorIndex);
-
-
-
- if (!$this->isSameColor($firstPixelColor, $currentPixelColor)) {
- imagedestroy($image);
- return null;
- }
- }
- }
- imagedestroy($image);
- return $firstPixelColor;
- }
- private function isSameColor(array $firstColor, array $secondColor, int $allowedDelta = 1) {
- if ($this->isSameColorComponent($firstColor['red'], $secondColor['red'], $allowedDelta) &&
- $this->isSameColorComponent($firstColor['green'], $secondColor['green'], $allowedDelta) &&
- $this->isSameColorComponent($firstColor['blue'], $secondColor['blue'], $allowedDelta) &&
- $this->isSameColorComponent($firstColor['alpha'], $secondColor['alpha'], $allowedDelta)) {
- return true;
- }
- return false;
- }
- private function isSameColorComponent(int $firstColorComponent, int $secondColorComponent, int $allowedDelta) {
- if ($firstColorComponent >= ($secondColorComponent - $allowedDelta) &&
- $firstColorComponent <= ($secondColorComponent + $allowedDelta)) {
- return true;
- }
- return false;
- }
- }
|