Skip to content

Commit

Permalink
feat(options): enhance the options for better testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sudkumar committed Sep 29, 2020
1 parent 6eba506 commit 4e272e1
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 9 deletions.
8 changes: 2 additions & 6 deletions src/OTP/OTPService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ public function __construct(Client $client, $payload = null)
{
$this->client = $client;
$this->options = (new Options())
->resolveConfig($this->client->getConfig());
if (is_int($payload)) {
$this->options->otp($payload);
} else {
$this->options->mergeWith($payload);
}
->resolveConfig($this->client->getConfig())
->mergeWith($payload);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/OTP/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@

class Options extends Msg91Options
{
/**
* Construct a new options
* @param mixed $options - initial payload of the message
* @return void
*/
public function __construct($options = null)
{
if (is_int($options)) {
$this->otp($options);
} else {
$this->mergeWith($options);
}
}
/**
* Set the otp for the message
* @param int|null $otp
Expand Down Expand Up @@ -74,6 +87,16 @@ public function method($via = null)
return $this;
}

public function mergeWith($options = null)
{
if (is_int($options)) {
$this->otp($options);
} else {
parent::mergeWith($options);
}
return $this;
}

/**
* Resolve the configuration options
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class Options implements ContractsOptions, JsonSerializable
protected $payload = [];

/**
* Construct a new Msg91Message
* Construct a new Options
* @param mixed $options - initial payload of the message
* @return void
*/
Expand Down Expand Up @@ -181,8 +181,8 @@ public function mergeWith($options = null)
// do which ever results in true
$current_payload = $this->getPayload();
switch (true) {
case $options instanceof self:
$this->payload = array_merge($current_payload, $options->getPayload());
case $options instanceof ContractsOptions:
$this->payload = array_merge($current_payload, $options->toArray());
break;
case is_array($options):
// if it is an array
Expand Down
21 changes: 21 additions & 0 deletions tests/OTP/OptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Craftsys\Tests\Msg91\OTP;

use Craftsys\Msg91\OTP\Options;
use Craftsys\Tests\Msg91\TestCase;

class OptionsTest extends TestCase
{
public function test_init_with_int_will_set_otp()
{
$options = (new Options(1234));
$this->assertEquals(1234, $options->getPayloadForKey('otp'));
}

public function test_can_merge_with_another_options()
{
$options = (new Options())->mergeWith(new Options(1234));
$this->assertEquals(1234, $options->getPayloadForKey('otp'));
}
}
38 changes: 38 additions & 0 deletions tests/OptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Craftsys\Tests\Msg91;

use Craftsys\Msg91\Options as AbstractOptions;

class OptionsTest extends TestCase
{
public function test_can_merge_with_a_string()
{
$options = (new Options)->mergeWith('string');
$this->assertEquals('string', $options->getPayloadForKey('message'));
}

public function test_can_merge_with_another_options()
{
$options = (new Options)->mergeWith((new Options())->from('this'));
$this->assertEquals('this', $options->getPayloadForKey('sender'));
}

public function test_be_merged_with_a_closure()
{
$options = (new Options)->mergeWith(function ($options) {
$options->from('this');
});
$this->assertEquals('this', $options->getPayloadForKey('sender'));
}

public function test_can_merge_an_array()
{
$options = (new Options)->mergeWith(['template_id' => 'this']);
$this->assertEquals('this', $options->getPayloadForKey('template_id'));
}
}

class Options extends AbstractOptions
{
}

0 comments on commit 4e272e1

Please sign in to comment.