Skip to content

Commit

Permalink
Made all PHPUnit tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
christeredvartsen committed Sep 12, 2019
1 parent 1e81fa2 commit 0146eb6
Show file tree
Hide file tree
Showing 14 changed files with 19 additions and 41 deletions.
14 changes: 6 additions & 8 deletions src/Database/MongoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
Imbo\Resource\Images\Query,
Imbo\Exception\DatabaseException,
Imbo\Exception\DuplicateImageIdentifierException,
Imbo\Exception\InvalidArgumentException,
Imbo\Helpers\BSONToArray,
MongoDB\Client as MongoClient,
MongoDB\Driver\Manager,
MongoDB\Driver\Command,
MongoDB\Collection as MongoCollection,
MongoDB\Driver\Exception\Exception as MongoException,
MongoDB\Model\BSONDocument,
DateTime,
DateTimeZone;

Expand Down Expand Up @@ -506,16 +506,14 @@ public function getNumBytes($user = null) {
]);
}

$result = $this->getImageCollection()->aggregate($pipeline, ['useCursor' => false]);

if (!count($result)) {
return 0;
}

return (int) $result[0]->numBytes;
$docs = $this->getImageCollection()->aggregate($pipeline)->toArray();
} catch (MongoException $e) {
throw new DatabaseException('Unable to fetch information from the database', 500, $e);
}

return empty($docs) ? 0 : array_sum(array_map(function(BSONDocument $doc) : int {
return $doc->numBytes;
}, $docs));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/phpunit/unit/Http/Response/ResponseFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,12 @@ public function testDoesNotSetResponseContentWhenHttpMethodIsHead() {
public function testThrowsAnExceptionInStrictModeWhenTheUserAgentDoesNotSupportAnyOfImbosMediaTypes() {
$this->contentNegotiation->expects($this->any())->method('isAcceptable')->will($this->returnValue(false));
$this->response->expects($this->once())->method('setVary')->with('Accept');
$this->response->expects($this->once())->method('getModel')->willReturn($this->createMock(Image::class));
$requestHeaders = $this->createMock('Symfony\Component\HttpFoundation\HeaderBag');
$requestHeaders->expects($this->once())->method('get')->with('Accept', '*/*')->will($this->returnValue('text/xml'));
$this->request->headers = $requestHeaders;
$this->expectExceptionObject(new RuntimeException('Not acceptable', 406));

$this->responseFormatter->negotiate($this->event);
}

Expand All @@ -225,6 +227,7 @@ public function testUsesDefaultMediaTypeInNonStrictModeWhenTheUserAgentDoesNotSu
$this->request->headers = $requestHeaders;
$this->contentNegotiation->expects($this->any())->method('isAcceptable')->will($this->returnValue(false));
$this->response->expects($this->once())->method('setVary')->with('Accept');
$this->response->expects($this->once())->method('getModel')->willReturn($this->createMock(Image::class));

$this->responseFormatter->negotiate($this->event);
$this->assertSame('json', $this->responseFormatter->getFormatter());
Expand All @@ -239,6 +242,7 @@ public function testPicksThePrioritizedMediaTypeIfMoreThanOneWithSameQualityAreS
$this->request->headers = $requestHeaders;
$this->contentNegotiation->expects($this->any())->method('isAcceptable')->will($this->returnValue(1));
$this->response->expects($this->once())->method('setVary')->with('Accept');
$this->response->expects($this->once())->method('getModel')->willReturn($this->createMock(Image::class));

$this->responseFormatter->negotiate($this->event);
$this->assertSame('json', $this->responseFormatter->getFormatter());
Expand Down
2 changes: 0 additions & 2 deletions tests/phpunit/unit/Http/Response/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
namespace ImboUnitTest\Http\Response;

use Imbo\Http\Response\Response;
use Imbo\Exception;
use Imbo\Exception\RuntimeException;
use DateTime;
use DateTimeZone;
use PHPUnit\Framework\TestCase;
Expand Down
20 changes: 0 additions & 20 deletions tests/phpunit/unit/Image/Transformation/CompressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,6 @@ public function testThrowsExceptionOnMissingLevelParameter() {
$this->transformation->transform([]);
}

public function testDoesNotApplyCompressionToGifImages() {
$image = $this->createMock('Imbo\Model\Image');
$image->expects($this->once())->method('getMimeType')->will($this->returnValue('image/gif'));

$imagick = $this->createMock('Imagick');
$imagick->expects($this->never())->method('setImageCompressionQuality');

$this->transformation->setImagick($imagick)->setImage($image)->transform(['level' => 40]);
$this->transformation->compress($this->createMock('Imbo\EventManager\EventInterface'));
}

public function testDoesNotApplyCompressionWhenLevelIsNotSet() {
$imagick = $this->createMock('Imagick');
$imagick->expects($this->never())->method('setImageCompressionQuality');

$this->transformation->setImagick($imagick)->compress(
$this->createMock('Imbo\EventManager\Event')
);
}

public function testThrowsExceptionOnInvalidLevel() {
$this->expectExceptionObject(new TransformationException(
'level must be between 0 and 100',
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/unit/Resource/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Imbo\Resource\Image;

/**
* @covers Imbo\Resource\Image
* @coversDefaultClass Imbo\Resource\Image
* @group unit
* @group resources
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/unit/Resource/ImagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use DateTimeZone;

/**
* @covers Imbo\Resource\Images
* @coversDefaultClass Imbo\Resource\Images
* @group unit
* @group resources
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/unit/Resource/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Imbo\Resource\Index;

/**
* @covers Imbo\Resource\Index
* @coversDefaultClass Imbo\Resource\Index
* @group unit
* @group resources
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/unit/Resource/MetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use DateTimeZone;

/**
* @covers Imbo\Resource\Metadata
* @coversDefaultClass Imbo\Resource\Metadata
* @group unit
* @group resources
*/
Expand Down
2 changes: 0 additions & 2 deletions tests/phpunit/unit/Resource/ResourceTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

namespace ImboUnitTest\Resource;

use ReflectionClass;
use ReflectionMethod;
use PHPUnit\Framework\TestCase;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/unit/Resource/ShortUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Imbo\Exception\ResourceException;

/**
* @covers Imbo\Resource\ShortUrl
* @coversDefaultClass Imbo\Resource\ShortUrl
* @group unit
* @group resources
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/unit/Resource/ShortUrlsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Imbo\Exception\InvalidArgumentException;

/**
* @covers Imbo\Resource\ShortUrls
* @coversDefaultClass Imbo\Resource\ShortUrls
* @group unit
* @group resources
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/unit/Resource/StatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Imbo\Resource\Stats;

/**
* @covers Imbo\Resource\Stats
* @coversDefaultClass Imbo\Resource\Stats
* @group unit
* @group resources
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/unit/Resource/StatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Imbo\Resource\Status;

/**
* @covers Imbo\Resource\Status
* @coversDefaultClass Imbo\Resource\Status
* @group unit
* @group resources
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/unit/Resource/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use DateTimeZone;

/**
* @covers Imbo\Resource\User
* @coversDefaultClass Imbo\Resource\User
* @group unit
* @group resources
*/
Expand Down

0 comments on commit 0146eb6

Please sign in to comment.