Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions spec/ConversationSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ function it_converse_automatically_and_return_the_last_context($api, $actionMapp
{
$api->converse('session_id', 'my text', Argument::any())->willReturn($this->stepData[Step::TYPE_MERGE]);

$expectedContext = new Context();
$expectedContext->add('custom', 'value');
$expectedContext = new Context(['custom' => 'value']);

$actionMapping
->merge('session_id', Argument::type(Context::class), $this->stepData[Step::TYPE_MERGE]['entities'])
Expand Down Expand Up @@ -149,8 +148,7 @@ function it_delegate_action_step_execution($api, $actionMapping)
{
$api->converse('session_id', 'my text', Argument::any())->willReturn($this->stepData[Step::TYPE_ACTION]);

$expectedContext = new Context();
$expectedContext->add('custom', 'value');
$expectedContext = new Context(['custom' => 'value']);

$actionMapping
->action('session_id', $this->stepData[Step::TYPE_ACTION]['action'], Argument::type(Context::class), Argument::type('array'))
Expand Down Expand Up @@ -197,8 +195,7 @@ function it_delegate_merge_execution($api, $actionMapping)
{
$api->converse('session_id', 'my text', Argument::any())->willReturn($this->stepData[Step::TYPE_MERGE]);

$expectedContext = new Context();
$expectedContext->add('custom', 'value');
$expectedContext = new Context(['custom' => 'value']);

$actionMapping
->merge('session_id', Argument::type(Context::class), $this->stepData[Step::TYPE_MERGE]['entities'])
Expand Down
12 changes: 9 additions & 3 deletions spec/EntityApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function it_should_get_entities($client, ResponseInterface $response)
$this->get()->shouldReturn($expected);
}

function it_should_create_entities($client, ResponseInterface $response, EntityValue $entityValue)
function it_should_create_entities($client, ResponseInterface $response)
{
$body = '
{
Expand All @@ -54,6 +54,8 @@ function it_should_create_entities($client, ResponseInterface $response, EntityV
}
';

$entityValue = new EntityValue('value');

$expected = json_decode($body, true);
$response->getBody()->willReturn($body);
$client->post('/entities', [
Expand Down Expand Up @@ -100,7 +102,7 @@ function it_should_get_an_entity($client, ResponseInterface $response)
$this->get('first_name')->shouldReturn($expected);
}

function it_should_update_an_entity($client, ResponseInterface $response, EntityValue $entityValue)
function it_should_update_an_entity($client, ResponseInterface $response)
{
$body = '
{
Expand All @@ -113,6 +115,8 @@ function it_should_update_an_entity($client, ResponseInterface $response, Entity
}
';

$entityValue = new EntityValue('value');

$expected = json_decode($body, true);
$response->getBody()->willReturn($body);
$client->put('/entities/favorite_city', [
Expand All @@ -139,7 +143,7 @@ function it_should_delete_an_entity($client, ResponseInterface $response)
$this->delete('favorite_city')->shouldReturn($expected);
}

function it_should_add_a_value_to_an_entity($client, ResponseInterface $response, EntityValue $entityValue)
function it_should_add_a_value_to_an_entity($client, ResponseInterface $response)
{
$body = '
{
Expand All @@ -153,6 +157,8 @@ function it_should_add_a_value_to_an_entity($client, ResponseInterface $response
}
';

$entityValue = new EntityValue('value');

$expected = json_decode($body, true);
$response->getBody()->willReturn($body);
$client->send('POST', '/entities/favorite_city/values', $entityValue)->willReturn($response);
Expand Down
4 changes: 3 additions & 1 deletion spec/Exception/ConversationExceptionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ function it_has_no_context_by_default()
$this->getContext()->shouldReturn(null);
}

function it_can_have_a_context(Context $context)
function it_can_have_a_context()
{
$context = new Context();

$this->beConstructedWith('error message', null, $context);
$this->getContext()->shouldReturn($context);
}
Expand Down
44 changes: 32 additions & 12 deletions spec/Model/ContextSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Tgallice\Wit\Model\Context;
use Tgallice\Wit\Model\Entity;
use Tgallice\Wit\Model\Location;

class ContextSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith(['field' => 'value']);
}

function it_is_initializable()
{
$this->shouldHaveType('Tgallice\Wit\Model\Context');
}

function it_has_a_reference_date()
{
$date = new \DateTime();
$date = new \DateTimeImmutable();

$this->beConstructedWith([
'reference_date' => $date,
Expand All @@ -34,8 +40,10 @@ function it_has_no_default_location()
$this->getLocation()->shouldReturn([]);
}

function it_can_define_a_location(Location $location)
function it_can_define_a_location()
{
$location = new Location(1.1, 1.2);

$this->beConstructedWith([
'location' => $location,
]);
Expand All @@ -60,8 +68,10 @@ function it_has_no_default_entities()
$this->getEntities()->shouldReturn([]);
}

function it_can_define_entities(Entity $entity)
function it_can_define_entities()
{
$entity = new Entity('id');

$this->beConstructedWith([
'entities' => [$entity],
]);
Expand All @@ -81,27 +91,37 @@ function it_can_define_timezone()
$this->getTimezone()->shouldReturn('timezone');
}

function it_can_add_custom_context_field()
function it_can_set_custom_context_field()
{
$this->add('context', 'value');
$this->get('context')->shouldReturn('value');
$context = $this->set('custom', 'value');
$context->get('custom')->shouldReturn('value');
}

function it_can_remove_a_context_field()
{
$this->add('context', 'value');
$this->get('context')->shouldReturn('value');
$this->remove('context');
$this->get('context')->shouldReturn(null);
$this->get('field')->shouldReturn('value');

$context = $this->remove('field');
$context->get('field')->shouldReturn(null);
}

function it_can_check_presence_of_a_context_field()
{
$this->add('context', 'value');
$this->has('context')->shouldReturn(true);
$this->has('field')->shouldReturn(true);
$this->has('wrong_context')->shouldReturn(false);
}

function it_is_immutable()
{
$newContext = $this->set('field', 'value');
$newContext2 = $newContext->remove('field');

$this->shouldBeLike($newContext);
$this->shouldNotBeEqualTo($newContext);
$newContext->get('field')->shouldReturn('value');
$newContext2->has('field')->shouldReturn(false);
}

function it_must_be_json_serializable()
{
$this->shouldHaveType(\JsonSerializable::class);
Expand Down
4 changes: 3 additions & 1 deletion spec/Model/EntitySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ function it_has_a_description()
$this->getDescription()->shouldReturn('description');
}

function it_has_values(EntityValue $entityValue)
function it_has_values()
{
$entityValue = new EntityValue('value');

$this->beConstructedWith('id', [$entityValue]);
$this->getValues()->shouldEqual([$entityValue]);
}
Expand Down
25 changes: 19 additions & 6 deletions src/Model/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Tgallice\Wit\Model;

class Context implements \JsonSerializable
/**
* Context Value Object
*/
final class Context implements \JsonSerializable
{
/**
* @var array
Expand All @@ -16,10 +19,10 @@ public function __construct($data = [])
{
// Ensure the refenre_date field
if (empty($data['reference_date'])) {
$data['reference_date'] = new \DateTime();
$data['reference_date'] = new \DateTimeImmutable();
}

if ($data['reference_date'] instanceof \DateTime) {
if ($data['reference_date'] instanceof \DateTimeInterface) {
$data['reference_date'] = $data['reference_date']->format(DATE_ISO8601);
}

Expand Down Expand Up @@ -71,10 +74,15 @@ public function getTimezone()
/**
* @param string $name
* @param mixed $value
*
* @return Context
*/
public function add($name, $value)
public function set($name, $value)
{
$this->data[$name] = $value;
$newData = $this->data;
$newData[$name] = $value;

return new self($newData);
}

/**
Expand All @@ -89,10 +97,15 @@ public function get($name)

/**
* @param string $name
*
* @return Context
*/
public function remove($name)
{
unset($this->data[$name]);
$newData = $this->data;
unset($newData[$name]);

return new self($newData);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tgallice\Wit\Model;

class Entity implements \JsonSerializable
final class Entity implements \JsonSerializable
{
const LOOKUP_TRAIT = 'trait';

Expand Down
2 changes: 1 addition & 1 deletion src/Model/EntityValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tgallice\Wit\Model;

class EntityValue implements \JsonSerializable
final class EntityValue implements \JsonSerializable
{
/**
* @var string
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tgallice\Wit\Model;

class Location implements \JsonSerializable
final class Location implements \JsonSerializable
{
/**
* @var float
Expand Down