Skip to content

Commit

Permalink
Implements date service provider decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed May 7, 2018
1 parent 8f188ff commit 10fe206
Show file tree
Hide file tree
Showing 2 changed files with 300 additions and 0 deletions.
130 changes: 130 additions & 0 deletions lib/mshoplib/src/MShop/Service/Provider/Decorator/Date.php
@@ -0,0 +1,130 @@
<?php

/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2018
* @package MShop
* @subpackage Service
*/


namespace Aimeos\MShop\Service\Provider\Decorator;


/**
* 'Date decorator for service providers
*
* @package MShop
* @subpackage Service
*/
class Date
extends \Aimeos\MShop\Service\Provider\Decorator\Base
implements \Aimeos\MShop\Service\Provider\Decorator\Iface
{
private $beConfig = array(
'date.minimumdays' => array(
'code' => 'date.minimumdays',
'internalcode' => 'date.minimumdays',
'label' => 'Miniumn number of days to wait when selecting dates',
'type' => 'integer',
'internaltype' => 'integer',
'default' => '0',
'required' => false,
),
);

private $feConfig = array(
'date.value' => array(
'code' => 'date.value',
'internalcode' => 'value',
'label' => 'Delivery date',
'type' => 'date',
'internaltype' => 'date',
'default' => '',
'required' => true
),
);


/**
* Checks the backend configuration attributes for validity.
*
* @param array $attributes Attributes added by the shop owner in the administraton interface
* @return array An array with the attribute keys as key and an error message as values for all attributes that are
* known by the provider but aren't valid
*/
public function checkConfigBE( array $attributes )
{
$error = $this->getProvider()->checkConfigBE( $attributes );
$error += $this->checkConfig( $this->beConfig, $attributes );

return $error;
}


/**
* Returns the configuration attribute definitions of the provider to generate a list of available fields and
* rules for the value of each field in the administration interface.
*
* @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface
*/
public function getConfigBE()
{
return array_merge( $this->getProvider()->getConfigBE(), $this->getConfigItems( $this->beConfig ) );
}


/**
* Returns the configuration attribute definitions of the provider to generate a list of available fields and
* rules for the value of each field in the frontend.
*
* @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
* @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface
*/
public function getConfigFE( \Aimeos\MShop\Order\Item\Base\Iface $basket )
{
$feconfig = $this->feConfig;

try
{
$days = $this->getConfigValue( 'date.minimumdays', 0 );
$type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_DELIVERY;
$service = $basket->getService( $type, $this->getServiceItem()->getCode() );

if( ( $value = $service->getAttribute( 'date.value', 'delivery' ) ) == '' ) {
$feconfig['date.value']['default'] = date( 'Y-m-d', time() + 86400 * $days );
} else {
$feconfig['date.value']['default'] = $value;
}
}
catch( \Aimeos\MShop\Order\Exception $e ) {} // If service isn't available

return array_merge( $this->getProvider()->getConfigFE( $basket ), $this->getConfigItems( $feconfig ) );
}


/**
* Checks the frontend configuration attributes for validity.
*
* @param array $attributes Attributes entered by the customer during the checkout process
* @return array An array with the attribute keys as key and an error message as values for all attributes that are
* known by the provider but aren't valid resp. null for attributes whose values are OK
*/
public function checkConfigFE( array $attributes )
{
$result = $this->getProvider()->checkConfigFE( $attributes );
$result = array_merge( $result, $this->checkConfig( $this->feConfig, $attributes ) );

if( $result['date.value'] !== null ) {
return $result;
}

$minimum = date( 'Y-m-d', time() + 86400 * $this->getConfigValue( 'date.minimumdays', 0 ) );

if( $attributes['date.value'] < $minimum ) {
$result['date.value'] = sprintf( 'Date value before "%1$s"', $minimum );
}

return $result;
}
}
170 changes: 170 additions & 0 deletions lib/mshoplib/tests/MShop/Service/Provider/Decorator/DateTest.php
@@ -0,0 +1,170 @@
<?php

/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2018
*/


namespace Aimeos\MShop\Service\Provider\Decorator;


class DateTest extends \PHPUnit_Framework_TestCase
{
private $object;
private $basket;
private $context;
private $servItem;
private $mockProvider;


protected function setUp()
{
$this->context = \TestHelperMShop::getContext();

$servManager = \Aimeos\MShop\Factory::createManager( $this->context, 'service' );
$this->servItem = $servManager->createItem()->setCode( '73' );

$this->mockProvider = $this->getMockBuilder( '\\Aimeos\\MShop\\Service\\Provider\\Decorator\\Date' )
->disableOriginalConstructor()->getMock();

$this->basket = \Aimeos\MShop\Order\Manager\Factory::createManager( $this->context )
->getSubManager( 'base' )->createItem();

$this->object = new \Aimeos\MShop\Service\Provider\Decorator\Date( $this->mockProvider, $this->context, $this->servItem );
}


protected function tearDown()
{
unset( $this->object, $this->basket, $this->mockProvider, $this->servItem, $this->context );
}


public function testGetConfigBE()
{
$this->mockProvider->expects( $this->once() )
->method( 'getConfigBE' )
->will( $this->returnValue( [] ) );

$result = $this->object->getConfigBE();

$this->assertArrayHasKey( 'date.minimumdays', $result );
}


public function testCheckConfigBE()
{
$this->mockProvider->expects( $this->once() )
->method( 'checkConfigBE' )
->will( $this->returnValue( [] ) );

$attributes = array(
'date.minimumdays' => '0',
);
$result = $this->object->checkConfigBE( $attributes );

$this->assertEquals( 1, count( $result ) );
$this->assertInternalType( 'null', $result['date.minimumdays'] );
}


public function testCheckConfigBENoConfig()
{
$this->mockProvider->expects( $this->once() )
->method( 'checkConfigBE' )
->will( $this->returnValue( [] ) );

$result = $this->object->checkConfigBE( [] );

$this->assertEquals( 1, count( $result ) );
$this->assertInternalType( 'null', $result['date.minimumdays'] );
}


public function testCheckConfigBEFailure()
{
$this->mockProvider->expects( $this->once() )
->method( 'checkConfigBE' )
->will( $this->returnValue( [] ) );

$attributes = array(
'date.minimumdays' => [],
);
$result = $this->object->checkConfigBE( $attributes );

$this->assertEquals( 1, count( $result ) );
$this->assertInternalType( 'string', $result['date.minimumdays'] );
}


public function testGetConfigFE()
{
$orderManager = \Aimeos\MShop\Order\Manager\Factory::createManager( \TestHelperMShop::getContext() );
$orderBaseManager = $orderManager->getSubManager( 'base' );
$search = $orderManager->createSearch();
$expr = array(
$search->compare( '==', 'order.type', \Aimeos\MShop\Order\Item\Base::TYPE_WEB ),
$search->compare( '==', 'order.statuspayment', \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED )
);
$search->setConditions( $search->combine( '&&', $expr ) );
$orderItems = $orderManager->searchItems( $search );

if( ( $order = reset( $orderItems ) ) === false ) {
throw new \RuntimeException( sprintf( 'No Order found with statuspayment "%1$s" and type "%2$s"', \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED, \Aimeos\MShop\Order\Item\Base::TYPE_WEB ) );
}


$this->mockProvider->expects( $this->once() )->method( 'getConfigFE' )->will( $this->returnValue( [] ) );

$basket = $orderBaseManager->load( $order->getBaseId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_SERVICE );
$config = $this->object->getConfigFE( $basket );

$this->assertRegExp( '/[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]/', $config['date.value']->getDefault() );
}


public function testCheckConfigFE()
{
$this->mockProvider->expects( $this->once() )
->method( 'checkConfigFE' )
->will( $this->returnValue( [] ) );

$config = array( 'date.value' => date( 'Y-m-d' ) );
$expected = array( 'date.value' => null );

$result = $this->object->checkConfigFE( $config );

$this->assertEquals( $expected, $result );
}


public function testCheckConfigFEwrongType()
{
$this->mockProvider->expects( $this->once() )
->method( 'checkConfigFE' )
->will( $this->returnValue( [] ) );

$config = array( 'date.value' => 0 );
$result = $this->object->checkConfigFE( $config );

$this->assertArrayHasKey( 'date.value', $result );
$this->assertFalse( $result['date.value'] === null );
}


public function testCheckConfigFEbefore()
{
$this->mockProvider->expects( $this->once() )
->method( 'checkConfigFE' )
->will( $this->returnValue( [] ) );

$this->servItem->setConfig( ['date.minimumdays' => '1'] );

$config = array( 'date.value' => date( 'Y-m-d' ) );
$result = $this->object->checkConfigFE( $config );

$this->assertArrayHasKey( 'date.value', $result );
$this->assertFalse( $result['date.value'] === null );
}
}

0 comments on commit 10fe206

Please sign in to comment.