diff --git a/server/tests/__mocks__/BeanMock.php b/server/tests/__mocks__/BeanMock.php new file mode 100644 index 00000000..56d7568a --- /dev/null +++ b/server/tests/__mocks__/BeanMock.php @@ -0,0 +1,41 @@ +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]); + } +} \ No newline at end of file diff --git a/server/tests/__mocks__/NullDataStoreMock.php b/server/tests/__mocks__/NullDataStoreMock.php new file mode 100644 index 00000000..336afb06 --- /dev/null +++ b/server/tests/__mocks__/NullDataStoreMock.php @@ -0,0 +1,7 @@ + parent::stub(), 'store' => parent::stub(), - 'dispense' => parent::stub()->returns(array()) + 'dispense' => parent::stub()->returns(new \BeanMock()) )); } } diff --git a/server/tests/__mocks__/SettingMock.php b/server/tests/__mocks__/SettingMock.php new file mode 100644 index 00000000..8ea84900 --- /dev/null +++ b/server/tests/__mocks__/SettingMock.php @@ -0,0 +1,26 @@ + 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; + } +} \ No newline at end of file diff --git a/server/tests/__mocks__/UserMock.php b/server/tests/__mocks__/UserMock.php index 112f7c6e..c8edb25a 100644 --- a/server/tests/__mocks__/UserMock.php +++ b/server/tests/__mocks__/UserMock.php @@ -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'; diff --git a/server/tests/controllers/user/loginTest.php b/server/tests/controllers/user/loginTest.php index 13d29800..0f78bc03 100644 --- a/server/tests/controllers/user/loginTest.php +++ b/server/tests/controllers/user/loginTest.php @@ -1,6 +1,7 @@ \Mock::stub()->returns(null) + 'authenticate' => \Mock::stub()->returns(new NullDataStore()) )); $this->loginController->handler(); diff --git a/server/tests/models/DataStoreTest.php b/server/tests/models/DataStoreTest.php index ebccd886..608ea9a3 100644 --- a/server/tests/models/DataStoreTest.php +++ b/server/tests/models/DataStoreTest.php @@ -1,5 +1,6 @@ \Mock::stub()->returns(array('TEST_PROP' => 'TEST_VALUE')) + 'findOne' => \Mock::stub()->returns(new BeanMock(['TEST_PROP' => 'TEST_VALUE'])) )); $dataStoreIntance = DataStoreMock::getDataStore('ID_VALUE'); diff --git a/server/tests/models/MailTemplateTest.php b/server/tests/models/MailTemplateTest.php index 95cdd476..db4ffbfe 100644 --- a/server/tests/models/MailTemplateTest.php +++ b/server/tests/models/MailTemplateTest.php @@ -1,12 +1,31 @@ \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; + } }