Ivan - Fix mocks and unit testing to use NullDataStore and BeanMock [skip ci]

This commit is contained in:
ivan 2016-07-27 18:27:28 -03:00
parent eccc0b5a29
commit 50974d2ed2
8 changed files with 117 additions and 5 deletions

View File

@ -0,0 +1,41 @@
<?php
class BeanMock implements ArrayAccess {
private $properties;
public function __construct($array = []) {
$this->properties = $array;
}
public function offsetSet($offset, $value) {
$this->__set($offset, $value);
}
public function offsetExists($offset) {
return $this->__isset($offset);
}
public function offsetUnset($offset) {
$this->__unset($offset);
}
public function &offsetGet($offset) {
return $this->__get($offset);
}
public function &__get($property) {
return $this->properties[$property];
}
public function __set($property, $value) {
$this->properties[$property] = $value;
}
public function __isset($property) {
return isset($this->properties[$property]);
}
public function __unset($property) {
unset($this->properties[$property]);
}
}

View File

@ -0,0 +1,7 @@
<?php
class NullDataStore extends \Mock {
public function isNull() {
return true;
}
}

View File

@ -1,5 +1,8 @@
<?php
namespace RedBeanPHP {
include_once 'tests/__mocks__/BeanMock.php';
class Facade extends \Mock {
public static $functionList = array();
@ -7,7 +10,7 @@ namespace RedBeanPHP {
self::setStatics(array(
'trash' => parent::stub(),
'store' => parent::stub(),
'dispense' => parent::stub()->returns(array())
'dispense' => parent::stub()->returns(new \BeanMock())
));
}
}

View File

@ -0,0 +1,26 @@
<?php
include_once 'tests/__mocks__/BeanMock.php';
class Setting extends \Mock {
public static $functionList = array();
public static function initStubs() {
parent::setStatics(array(
'getSetting' => parent::stub()->returns(self::getSettingInstanceMock()),
'setSetting' => parent::stub()
));
}
public function isNull() {
return false;
}
private static function getSettingInstanceMock() {
$mockUserInstance = new BeanMock();
$mockUserInstance->name = 'MOCK_SETTING_NAME';
$mockUserInstance->value = 'MOCK_SETTING_VALUE';
return $mockUserInstance;
}
}

View File

@ -7,9 +7,13 @@ class User extends \Mock {
'authenticate' => parent::stub()->returns(self::getUserInstanceMock()),
));
}
public function isNull() {
return false;
}
private static function getUserInstanceMock() {
$mockUserInstance = new \stdClass();
$mockUserInstance = new User();
$mockUserInstance->id = 'MOCK_ID';
$mockUserInstance->email = 'MOCK_EMAIL';

View File

@ -1,6 +1,7 @@
<?php
// MOCKS
include_once 'tests/__lib__/Mock.php';
include_once 'tests/__mocks__/NullDataStoreMock.php';
include_once 'tests/__mocks__/ResponseMock.php';
include_once 'tests/__mocks__/ControllerMock.php';
include_once 'tests/__mocks__/SessionMock.php';
@ -45,7 +46,7 @@ class LoginControllerTest extends PHPUnit_Framework_TestCase {
public function testShouldRespondErrorIfCredentialsAreInvalid() {
User::setStatics(array(
'authenticate' => \Mock::stub()->returns(null)
'authenticate' => \Mock::stub()->returns(new NullDataStore())
));
$this->loginController->handler();

View File

@ -1,5 +1,6 @@
<?php
include_once 'tests/__lib__/Mock.php';
include_once 'tests/__mocks__/BeanMock.php';
include_once 'tests/__mocks__/SlimMock.php';
include_once 'tests/__mocks__/RedBeanMock.php';
include_once 'models/DataStore.php';
@ -57,7 +58,7 @@ class DataStoreTest extends PHPUnit_Framework_TestCase {
public function testGetDataStore() {
RedBean::setStatics(array(
'findOne' => \Mock::stub()->returns(array('TEST_PROP' => 'TEST_VALUE'))
'findOne' => \Mock::stub()->returns(new BeanMock(['TEST_PROP' => 'TEST_VALUE']))
));
$dataStoreIntance = DataStoreMock::getDataStore('ID_VALUE');

View File

@ -1,12 +1,31 @@
<?php
include_once 'tests/__lib__/Mock.php';
include_once 'tests/__mocks__/BeanMock.php';
include_once 'tests/__mocks__/SettingMock.php';
include_once 'tests/__mocks__/RedBeanMock.php';
include_once 'models/MailTemplate.php';
use RedBeanPHP\Facade as RedBean;
class MailTemplateTest extends PHPUnit_Framework_TestCase {
protected function setUp() {
RedBean::initStubs();
Setting::initStubs();
RedBean::setStatics([
'findOne' => \Mock::stub()->returns($this->getMockTemplateBean())
]);
}
public function testGetTemplateShouldReturnSpecifiedTemplate() {
$mailTemplate = MailTemplate::getTemplate(MailTemplate::USER_SIGNUP);
$this->assertEquals(MailTemplate::USER_SIGNUP, $mailTemplate->type);
$this->assertEquals('TEST_TYPE', $mailTemplate->type);
$this->assertTrue(Redbean::get('findOne')->hasBeenCalledWithArgs('mailtemplate', 'type = :type AND language = :language', array(
':type' => 'USER_SIGNUP',
':language' => 'MOCK_SETTING_VALUE'
)));
}
public function testCompilation() {
@ -24,4 +43,14 @@ class MailTemplateTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($result['subject'], 'Welcoming to cersei@opensupports.com');
$this->assertEquals($result['body'], 'Welcome, Cersei Lannister to our team');
}
private function getMockTemplateBean() {
$mailTemplateBean = new BeanMock();
$mailTemplateBean->type = 'TEST_TYPE';
$mailTemplateBean->body = 'Some body';
$mailTemplateBean->subject = 'Some subject';
$mailTemplateBean->language = 'en';
return $mailTemplateBean;
}
}