Skip to content

Commit

Permalink
Fix tests and phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
lkm committed Apr 28, 2020
1 parent 99a3cf5 commit 22bbba5
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- stage: "Tests"
name: "Unit Tests"
script: vendor/bin/phpunit -c . --coverage-clover build/logs/clover.xml
- script: vendor/bin/phpstan analyse src/ --level 6 --no-progress
- script: vendor/bin/phpstan analyse src/ --level 5 --no-progress
name: "Static Analyser"

before_script:
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
ignoreErrors:
- '#Unsafe usage of new static().#'
5 changes: 3 additions & 2 deletions src/Client/BasicAuthClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BasicAuthClient implements ClientInterface
* @param Headers $headers
* @param CacheInterface|null $cache
* @param string|null $apiUri
* @param LoggerInterface|null $logger
* @throws UsageException
*/
public function __construct(
Expand All @@ -48,8 +49,8 @@ public function __construct(
Headers $headers,
CacheInterface $cache = null,
$apiUri = null,
LoggerInterface $logger = null)
{
LoggerInterface $logger = null
) {
if (empty($apiId) || empty($apiSecret)) {
throw new UsageException("Key and secret are required");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Client/BookboonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function serialize()
*/
public function unserialize($serialized)
{
$data = unserialize($serialized, ['']);
$data = unserialize($serialized, ['allowed_classes' => [BookboonResponse::class]]);
$this->body = $data['body'];
$this->status = $data['status'];
$this->headers = $data['headers'];
Expand Down
5 changes: 2 additions & 3 deletions src/Client/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function handleErrorResponse(string $body, array $headers, int $status
case 400:
case 405:
$returnArray = json_decode($body, true);
throw new ApiSyntaxException($returnArray['message']);
throw new ApiSyntaxException($returnArray['message'] ?? '');
case 401:
case 403:
$returnArray = json_decode($body, true);
Expand All @@ -42,7 +42,6 @@ protected function handleErrorResponse(string $body, array $headers, int $status
case 410:
case 404:
throw new ApiNotFoundException($url);
break;
default:
$returnArray = json_decode($body, true);
throw new ApiGeneralException($this->generalExceptionMessage($returnArray ?? [], $headers));
Expand Down Expand Up @@ -86,4 +85,4 @@ protected function getResponseHeader(array $headers, string $name) : string
{
return $headers[$name] ?? '';
}
}
}
2 changes: 1 addition & 1 deletion src/Entity/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function get(Bookboon $bookboon, string $authorId) : BookboonRespo
$bResponse->setEntityStore(
new EntityStore(
[
new static($bResponse->getReturnArray())
new self($bResponse->getReturnArray())
]
)
);
Expand Down
7 changes: 2 additions & 5 deletions src/Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

class Category extends Entity
{
const TEXTBOOKS = 'd1fabb36-4eff-4760-a80d-a15700efa9ae';
const BUSINESS = '82403e77-ccbf-4e10-875c-a15700ef8a56';

/**
* Get Category.
*
Expand All @@ -29,7 +26,7 @@ public static function get(Bookboon $bookboon, string $categoryId, array $bookTy
$bResponse->setEntityStore(
new EntityStore(
[
new static($bResponse->getReturnArray())
new self($bResponse->getReturnArray())
]
)
);
Expand Down Expand Up @@ -60,7 +57,7 @@ public static function getTree(
}

$bResponse->setEntityStore(
new EntityStore(Category::getEntitiesFromArray($categories))
new EntityStore(self::getEntitiesFromArray($categories))
);

return $bResponse;
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Exam.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function get(Bookboon $bookboon, string $examId) : BookboonRespons
$bResponse->setEntityStore(
new EntityStore(
[
new static($bResponse->getReturnArray())
new self($bResponse->getReturnArray())
]
)
);
Expand Down
6 changes: 2 additions & 4 deletions src/Entity/Frontpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function get(Bookboon $bookboon, array $bookTypes = ['pdf']) : Boo
$bResponse = $bookboon->rawRequest('/v1/frontpage', ['bookType' => implode(',', $bookTypes)]);

$bResponse->setEntityStore(
new EntityStore(Frontpage::getEntitiesFromArray($bResponse->getReturnArray()))
new EntityStore(self::getEntitiesFromArray($bResponse->getReturnArray()))
);

return $bResponse;
Expand All @@ -46,14 +46,12 @@ public static function get(Bookboon $bookboon, array $bookTypes = ['pdf']) : Boo
*/
public static function getBySlug(Bookboon $bookboon, string $slug, array $bookTypes = ['pdf']) : BookboonResponse
{

/** @var Frontpage[] $frontpageArray */
$bResponse = $bookboon->rawRequest("/v1/frontpage/$slug", ['bookType' => join(',', $bookTypes)]);

$bResponse->setEntityStore(
new EntityStore(
[
new static($bResponse->getReturnArray())
new self($bResponse->getReturnArray())
]
)
);
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Journey.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function get(Bookboon $bookboon, string $journeyId) : BookboonResp
{
$bResponse = $bookboon->rawRequest("/v1/journeys/$journeyId");

$journeyEntity = new static($bResponse->getReturnArray());
$journeyEntity = new self($bResponse->getReturnArray());

if (count($journeyEntity->getBookIds()) > 0) {
$books = Book::getMultiple($bookboon, $journeyEntity->getBookIds(), false);
Expand Down
6 changes: 3 additions & 3 deletions src/Entity/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ public static function getByBookId(Bookboon $bookboon, string $bookId) : Bookboo
$bResponse = $bookboon->rawRequest("/v1/books/$bookId/reviews");

$bResponse->setEntityStore(
new EntityStore(Review::getEntitiesFromArray($bResponse->getReturnArray()))
new EntityStore(self::getEntitiesFromArray($bResponse->getReturnArray()))
);

return $bResponse;
}

/**
* @param array $review
* @return static
* @return Review
* @throws \Bookboon\Api\Exception\EntityDataException
*/
public static function create(array $review = []) : Review
{
return new static($review);
return new self($review);
}


Expand Down
5 changes: 3 additions & 2 deletions tests/Entity/AuthorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Bookboon\Api\Bookboon;
use PHPUnit\Framework\TestCase;
use Bookboon\Api\Entity\Book;

/**
* Class AuthorTest
Expand All @@ -19,7 +20,7 @@ public static function setUpBeforeClass()
{
include_once(__DIR__ . '/../Helpers.php');
$bookboon = \Helpers::getBookboon();
self::$data = Author::get($bookboon, '0908031c-ce02-9b86-11e6-6dd9aa4699d1')
self::$data = Author::get($bookboon, '0908031c-ce02-9b86-11e6-6dd9268599d1')
->getEntityStore()
->getSingle();
}
Expand All @@ -44,7 +45,7 @@ public function testNotFalse($method)
public function testHasBooks()
{
$books = self::$data->getBooks();
$this->assertInstanceOf('\Bookboon\Api\Entity\Book', $books[0]);
$this->assertInstanceOf(Book::class, $books[0]);
}

/**
Expand Down
14 changes: 11 additions & 3 deletions tests/Entity/CategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,22 @@ public function testInvalidCategory()
public function testGetCategoryTree()
{
$categories = Category::getTree(self::$bookboon)->getEntityStore()->get();
$this->assertEquals(2, count($categories));
$this->assertGreaterThan(10, count($categories));
}

public function testGetCategoryTreeBlacklist()
{
$categories = Category::getTree(self::$bookboon, ['82403e77-ccbf-4e10-875c-a15700ef8a56', '07651831-1c44-4815-87a2-a2b500f5934a']);
$hiddenId = 'a382f37c-dc28-400a-9838-a17700ad5472';
$categories = Category::getTree(self::$bookboon, [$hiddenId]);

$this->assertEquals(1, count($categories->getEntityStore()->get()));
$categoryIds = array_map(
static function (Category $item) {
return $item->getId();
},
$categories->getEntityStore()->get()
);

$this->assertNotContains($hiddenId, $categoryIds);
}

public function testCategoryDownload()
Expand Down
4 changes: 2 additions & 2 deletions tests/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function getApiSecret()

public static function getBookboon()
{
$bookboon = Bookboon::create(self::getApiId(), self::getApiSecret(), ['basic', 'download_book.pdf', 'download_category']);
$bookboon = Bookboon::create(self::getApiId(), self::getApiSecret(), ['basic', 'api.book.academic', 'api.book.professional', 'api.download_category', 'api.journeys']);
$bookboon->getClient()->requestAccessToken([], OauthGrants::CLIENT_CREDENTIALS);

return $bookboon;
Expand All @@ -41,4 +41,4 @@ public static function invokeMethod(&$object, $methodName, array $parameters = [

return $method->invokeArgs($object, $parameters);
}
}
}

0 comments on commit 22bbba5

Please sign in to comment.