Skip to content

Commit

Permalink
Merge 4890af5 into cb84176
Browse files Browse the repository at this point in the history
  • Loading branch information
kilip committed Jan 25, 2018
2 parents cb84176 + 4890af5 commit 5057a21
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 48 deletions.
4 changes: 3 additions & 1 deletion module/Core/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ public function onBootstrap(MvcEvent $e)
(new ErrorHandlerListener())->attach($eventManager);

/* @var \Core\Options\ModuleOptions $options */
$languageRouteListener = new LanguageRouteListener($sm->get('Core/Locale'));
$languageRouteListener = new LanguageRouteListener(
$sm->get('Core/Locale'),$sm->get('Core/Options')
);
$languageRouteListener->attach($eventManager);

$ajaxRenderListener = new AjaxRenderListener();
Expand Down
31 changes: 3 additions & 28 deletions module/Core/src/Core/Entity/AbstractIdentifiableEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,12 @@
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
* Base class for an Entity that should have id on it's class
*
* @author Anthonius Munthi <me@itstoni.com>
* @ODM\MappedSuperclass
*/
abstract class AbstractIdentifiableEntity extends AbstractEntity implements IdentifiableEntityInterface
{

/**
* Entity id
*
* @var mixed
* @ODM\Id
*/
protected $id;

/**
* {@inheritdoc}
* @see \Core\Entity\EntityInterface::getId()
*/
public function getId()
{
return $this->id;
}

/**
* {@inheritdoc}
* @see \Core\Entity\EntityInterface::setId()
* @return $this
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
use IdentifiableEntityTrait;
}
8 changes: 3 additions & 5 deletions module/Core/src/Core/Entity/Timeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* Any type of timeline
*
* @author Anthonius Munthi <me@itstoni.com>
* @ODM\EmbeddedDocument
*/
class Timeline extends AbstractEntity
Expand All @@ -25,17 +26,14 @@ class Timeline extends AbstractEntity

public function __construct()
{
$this->getDate();
$this->date = new \DateTime();
}

/**
* @return $date
* @return \DateTime
*/
public function getDate()
{
if (!$this->date) {
$this->setDate(new \DateTime());
}
return $this->date;
}

Expand Down
29 changes: 26 additions & 3 deletions module/Core/src/Core/Listener/LanguageRouteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace Core\Listener;

use Core\Options\ModuleOptions;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\Mvc\MvcEvent;
Expand All @@ -16,6 +17,15 @@
use Locale;
use Core\I18n\Locale as LocaleService;

/**
* Class LanguageRouteListener
*
* @package Core\Listener
* @authro Carsten Bleek <cbleek@yawik.org>
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
* @author Miroslav Fedeleš <miroslav.fedeles@gmail.com>
* @author Anthonius Munthi <me@itstoni.com>
*/
class LanguageRouteListener implements ListenerAggregateInterface
{

Expand All @@ -28,13 +38,23 @@ class LanguageRouteListener implements ListenerAggregateInterface
* @var LocaleService
*/
protected $localeService;

/**
* @var ModuleOptions
*/
protected $moduleOptions;


/**
* LanguageRouteListener constructor.
*
* @param LocaleService $localeService
* @param ModuleOptions $moduleOptions
*/
public function __construct(LocaleService $localeService)
public function __construct(LocaleService $localeService, ModuleOptions $moduleOptions)
{
$this->localeService = $localeService;
$this->moduleOptions = $moduleOptions;
}

/**
Expand Down Expand Up @@ -138,7 +158,10 @@ public function onDispatchError(MvcEvent $e)
*/
$request = clone $e->getRequest(); // clone the request, because maybe we
$origUri = str_replace($basePath, '', $request->getRequestUri());
$lang = $this->detectLanguage($e);

$options = $this->moduleOptions;
// using default language if detect language is disabled
$lang = $options->isDetectLanguage() ? $this->detectLanguage($e):$options->getDefaultLanguage();
$langUri = rtrim("$basePath/$lang$origUri", '/');
if ($router->match($request->setUri($langUri)) instanceof RouteMatch) {
$e->stopPropagation(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @group Core
* @group Core.Entity
* @covers \Core\Entity\AbstractIdentifiableEntity
* @covers \Core\Entity\IdentifiableEntityTrait
*/
class AbstractIdentifiableEntityTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -43,4 +44,4 @@ public function testSetGetIdByMethod(){
}

class ConcreteIdentifiableEntity extends AbstractIdentifiableEntity {
}
}
48 changes: 48 additions & 0 deletions module/Core/test/CoreTest/Entity/TimelineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* YAWIK
*
* @filesource
* @license MIT
* @copyright 2013 - 2017 Cross Solution <http://cross-solution.de>
*/

namespace CoreTest\Entity;

use Core\Entity\Timeline;
use CoreTestUtils\TestCase\SetupTargetTrait;
use CoreTestUtils\TestCase\TestSetterGetterTrait;

/**
* Class TimelineTest
*
* @author Anthonius Munthi <me@itstoni.com>
* @package CoreTest\Entity
* @since 0.30.1
* @covers \Core\Entity\Timeline
*/
class TimelineTest extends \PHPUnit_Framework_TestCase
{
use TestSetterGetterTrait, SetupTargetTrait;

protected $target = [
'class' => Timeline::class
];

public function propertiesProvider()
{
return [
['date',[
'value' => new \DateTime(),
'default' => new \DateTime(),
'default_assert' => function($v,$return){
$date = new \DateTime();
$this->assertEquals(
$date->getTimestamp(),
$return->getTimestamp()
);
},
]]
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/** */
namespace CoreTest\Listener\LanguageRouteListener;

use Core\Options\ModuleOptions;
use Zend\EventManager\EventManager;
use Core\Listener\LanguageRouteListener;
use Core\I18n\Locale as LocaleService;
Expand Down Expand Up @@ -38,7 +39,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$this->target = new LanguageRouteListener(new LocaleService(['xx' => 'xx_XX']));
$this->target = new LanguageRouteListener(new LocaleService(['xx' => 'xx_XX']),new ModuleOptions());
}

public function testAttachsToExpectedEvents()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Auth\AuthenticationService;
use Auth\Entity\UserInterface;
use Core\Listener\LanguageRouteListener;
use Core\Options\ModuleOptions;
use CoreTestUtils\TestCase\ServiceManagerMockTrait;
use Zend\Http\Request;
use Zend\Http\Response;
Expand Down Expand Up @@ -180,6 +181,14 @@ public function testDetectLanguageWithoutAcceptedLanguage()

class LrlMock extends LanguageRouteListener
{
public function __construct(LocaleService $localeService, ModuleOptions $moduleOptions = null)
{
if(is_null($moduleOptions)){
$moduleOptions = new ModuleOptions();
}
parent::__construct($localeService, $moduleOptions);
}

public function setLocale(MvcEvent $e, $lang)
{
$origLocale = setlocale(LC_ALL, "0");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/** */
namespace CoreTest\Listener\LanguageRouteListener;

use Core\Options\ModuleOptions;
use CoreTestUtils\TestCase\SetupTargetTrait;
use Core\Listener\LanguageRouteListener;
use Zend\Http\PhpEnvironment\Response;
Expand All @@ -36,10 +37,15 @@ class OnDispatchErrorCallbackTest extends \PHPUnit_Framework_TestCase
private $target = [
LanguageRouteListener::class,
'mock' => [ 'detectLanguage' => ['return' => 'xx'], 'setLocale', 'isSupportedLanguage', 'redirect'],
'args' => 'getLocaleService'
'args' => 'getConstructorArgs'
];

public function testHandleConsoleRequests()
/**
* @var ModuleOptions
*/
private $moduleOptions;

public function testHandleConsoleRequests()
{
$event = $this
->getMockBuilder(MvcEvent::class)
Expand Down Expand Up @@ -183,11 +189,51 @@ function($v) {
$this->target->onDispatchError($event);

}
public function getLocaleService()

public function testHandleWhenDetectLanguageDisabled()
{
return [new LocaleService([])];
$event = $this->getEventMock('/base/uri', '/see/no/lang');

$event->expects($this->once())
->method('stopPropagation')
->with(true)
;
$response = new Response();
$event->setResponse($response);

$routeMatch = new RouteMatch([]);
$this->router
->expects($this->once())
->method('match')
->with($this->callback(
function($v) {
\PHPUnit_Framework_Assert::assertEquals('/base/uri/default/see/no/lang', (string) $v->getUri());
return true;
}
))
->willReturn($routeMatch)
;


$target = $this->target;
$this->moduleOptions->setDefaultLanguage('default');
$this->moduleOptions->setDetectLanguage(false);

$target->expects($this->once())
->method('redirect')
->with($response,'/base/uri/default/see/no/lang')
;

$target->onDispatchError($event);
}

public function getConstructorArgs()
{
$this->moduleOptions = new ModuleOptions();
return [new LocaleService([]),$this->moduleOptions];
}


}


Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/** */
namespace CoreTest\Listener\LanguageRouteListener;

use Core\Options\ModuleOptions;
use CoreTestUtils\TestCase\SetupTargetTrait;
use Zend\EventManager\EventManager;
use Core\Listener\LanguageRouteListener;
Expand All @@ -36,7 +37,7 @@ class OnRouteCallbackTest extends \PHPUnit_Framework_TestCase
private $target = [
LanguageRouteListener::class,
'mock' => [ 'detectLanguage' => ['return' => 'xx'], 'setLocale' ],
'args' => 'getLocaleService'
'args' => 'getConstructorArgs'
];

protected $localeService;
Expand Down Expand Up @@ -121,14 +122,14 @@ public function testRouteWithoutSupportedLanguage()
$this->assertEquals('response', $actual);
}

public function getLocaleService()
public function getConstructorArgs()
{
$this->localeService = $this->getMockBuilder(LocaleService::class)
->setConstructorArgs([[]])
->setMethods(['isLanguageSupported'])
->getMock();
return [$this->localeService];

return [$this->localeService, new ModuleOptions()];
}
}

Expand Down

0 comments on commit 5057a21

Please sign in to comment.