Skip to content

Commit

Permalink
Add Journey and Language entity
Browse files Browse the repository at this point in the history
Signed-off-by: Lasse Mammen <[email protected]>
  • Loading branch information
lkm committed Jan 9, 2020
1 parent d79d056 commit e9ce991
Show file tree
Hide file tree
Showing 6 changed files with 349 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Entity/Frontpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Frontpage extends Entity
*/
public static function get(Bookboon $bookboon, array $bookTypes = ['pdf']) : BookboonResponse
{
$bResponse = $bookboon->rawRequest("/frontpage", ['bookType' => join(',', $bookTypes)]);
$bResponse = $bookboon->rawRequest('/frontpage', ['bookType' => implode(',', $bookTypes)]);

$bResponse->setEntityStore(
new EntityStore(Frontpage::getEntitiesFromArray($bResponse->getReturnArray()))
Expand Down Expand Up @@ -107,4 +107,4 @@ public function getBooks() : array
{
return Book::getEntitiesFromArray($this->safeGet('books', []));
}
}
}
147 changes: 147 additions & 0 deletions src/Entity/Journey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace Bookboon\Api\Entity;

use Bookboon\Api\Bookboon;
use Bookboon\Api\Client\BookboonResponse;
use Bookboon\Api\Exception\UsageException;

class Journey extends Entity
{
private $books;

/**
* @param Bookboon $bookboon
* @param string $journeyId
* @return BookboonResponse
* @throws \Bookboon\Api\Exception\UsageException
*/
public static function get(Bookboon $bookboon, string $journeyId) : BookboonResponse
{
$bResponse = $bookboon->rawRequest("/journeys/$journeyId");

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

if (count($journeyEntity->getBookIds()) > 0) {
$books = Book::getMultiple($bookboon, $journeyEntity->getBookIds(), false);
}

$journeyEntity->setBooks($books->getEntityStore()->get());

$bResponse->setEntityStore(
new EntityStore(
[
$journeyEntity
]
)
);

return $bResponse;
}

/**
* Get all journeys
*
* @param Bookboon $bookboon
* @param array $bookTypes
* @return BookboonResponse
* @throws UsageException
* @throws \Bookboon\Api\Exception\ApiDecodeException
*/
public static function getAll(Bookboon $bookboon, array $bookTypes = ['pdf']) : BookboonResponse
{
$bResponse = $bookboon->rawRequest('/journeys');

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

return $bResponse;
}

/**
* @return string id
*/
public function getId() : string
{
return $this->safeGet('_id');
}

/**
* @return string title
*/
public function getTitle() : string
{
return $this->safeGet('title');
}

/**
* @return string abstract
*/
public function getAbstract() : string
{
return $this->safeGet('abstract');
}

/**
* @return string description
*/
public function getDescription() : string
{
return $this->safeGet('description');
}

/**
* Return publish date.
*
* @return string date of publishing
*/
public function getPublished() : string
{
return $this->safeGet('published');
}

/**
* @return bool is featured
*/
public function isFeatured() : bool
{
return $this->safeGet('isFeatured');
}

/**
* @return array book ids
*/
public function getBookIds() : array
{
return $this->safeGet('books', []);
}

/**
* @return array books
*/
public function getBooks() : array
{
return $this->books;
}

/**
* @param array $books
* @return void
*/
public function setBooks(array $books) : void
{
$this->books = $books;
}

/**
* Determines whether api response is valid
*
* @param array $array
* @return bool
*/
protected function isValid(array $array): bool
{
return isset($array['_id'], $array['title'], $array['abstract']);
}
}
81 changes: 81 additions & 0 deletions src/Entity/Language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Bookboon\Api\Entity;

use Bookboon\Api\Bookboon;
use Bookboon\Api\Client\BookboonResponse;
use Bookboon\Api\Exception\UsageException;

class Language extends Entity
{
/**
* Get all languages
*
* @param Bookboon $bookboon
* @param array $bookTypes
* @return BookboonResponse
* @throws UsageException
* @throws \Bookboon\Api\Exception\ApiDecodeException
*/
public static function get(Bookboon $bookboon, array $bookTypes = ['pdf']) : BookboonResponse
{
$bResponse = $bookboon->rawRequest('/languages');

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

return $bResponse;
}

/**
* @return string id
*/
public function getId() : string
{
return $this->safeGet('id');
}

/**
* @return string code
*/
public function getCode() : string
{
return $this->safeGet('code');
}

/**
* @return string slug
*/
public function getSlug() : string
{
return $this->safeGet('_slug');
}

/**
* @return string name
*/
public function getName() : string
{
return $this->safeGet('name');
}

/**
* @return string localized name
*/
public function getLocalizedName() : string
{
return $this->safeGet('localizedName');
}

/**
* Determines whether api response is valid
*
* @param array $array
* @return bool
*/
protected function isValid(array $array): bool
{
return isset($array['code'], $array['id'], $array['name']);
}
}
2 changes: 1 addition & 1 deletion src/Entity/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ public static function remove(Bookboon $bookboon, string $email, ?string $alias)
}

}

66 changes: 66 additions & 0 deletions tests/Entity/JourneyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Bookboon\Api\Entity;

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

/**
* Class JourneyTest
* @package Bookboon\Api\Entity
* @group entity
*/
class JourneyTest extends TestCase
{
private $methodsToTest = ['getTitle', 'getId', 'getAbstract', 'getDescription', 'getPublished'];
private static $bookboon;

public static function setUpBeforeClass()
{
include_once(__DIR__ . '/../Helpers.php');
self::$bookboon = \Helpers::getBookboon();
}

public function providerTestGetters()
{
return [
'getTitle' => ['getTitle'],
'getId' => ['getId'],
'getAbstract' => ['getAbstract'],
'getDescription' => ['getDescription'],
'getPublished' => ['getPublished'],
];
}

public function testFirstAllJourneysData()
{
$data = Journey::getAll(self::$bookboon)
->getEntityStore()
->get()[0];

foreach ($this->methodsToTest as $method) {
$this->assertNotFalse($data->$method());
}
}

public function testGetJourney()
{
$data = Journey::get(self::$bookboon, '010e0268-0eec-4859-a67a-ce41ee2315c4')
->getEntityStore()
->get()[0];

foreach ($this->methodsToTest as $method) {
$this->assertNotFalse($data->$method());
}

$this->assertInstanceOf(Book::class, $data->getBooks()[0]);
}

/**
* @expectedException \Bookboon\Api\Exception\EntityDataException
*/
public function testInvalidJourney()
{
$language = new Language(['blah']);
}
}
52 changes: 52 additions & 0 deletions tests/Entity/LanguageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Bookboon\Api\Entity;

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

/**
* Class LanguageTest
* @package Bookboon\Api\Entity
* @group entity
*/
class LanguageTest extends TestCase
{
/** @var []Language */
private static $data = null;

public static function setUpBeforeClass()
{
include_once(__DIR__ . '/../Helpers.php');
$bookboon = \Helpers::getBookboon();
self::$data = Language::get($bookboon)
->getEntityStore()
->get()[0];
}

public function providerTestGetters()
{
return [
'getName' => ['getName'],
'getId' => ['getId'],
'getLocalizedName' => ['getLocalizedName'],
'getCode' => ['getCode'],
];
}

/**
* @dataProvider providerTestGetters
*/
public function testNotFalse($method)
{
$this->assertNotFalse(self::$data->$method());
}

/**
* @expectedException \Bookboon\Api\Exception\EntityDataException
*/
public function testInvalidLanguage()
{
$language = new Language(['blah']);
}
}

0 comments on commit e9ce991

Please sign in to comment.