Skip to content

Commit

Permalink
tests passing. #94
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Aug 5, 2014
1 parent f923a80 commit 138880c
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/Controllers/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function index($name) {

if ($aggr = Input::get('aggr')) {
// Aggregate 'max'/'min'/'avg'/'sum' methods
if ($aggr['field']) {
if (isset($aggr['field'])) {
return $this->json($query->{$aggr['method']}($aggr['field']));

} else {
Expand Down
14 changes: 10 additions & 4 deletions src/Controllers/KeyValueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ class KeyValueController extends HookController {

public function show($name) {
$key = Model\KeyValue::where('name', $name)->first();
return $this->json(($key) ? $key->value : null);
return $this->json(($key) ? json_encode(unserialize($key->value)) : null);
}

public function store($name) {
return $this->json(Model\KeyValue::upsert(array(
$value = Input::get('value');
Model\KeyValue::upsert(array(
'name' => $name,
'value' => Input::get('value')
))->value);
'value' => serialize($value)
));
return $this->json(json_encode($value));
}

public function delete($name) {
return $this->json(Model\KeyValue::where('name', $name)->delete());
}

}
7 changes: 6 additions & 1 deletion src/Controllers/PushNotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
use Hook\Http\Input;
use Hook\Http\Request;

use Hook\Database\AppContext;
use Hook\Exceptions\ForbiddenException;

use Hook\PushNotification;

class PushNotificationController extends HookController {

public function store() {
Expand All @@ -25,7 +30,7 @@ public function delete() {
throw new \Exception("'device_id' is required to delete push registration.");
}
$registration = Model\PushRegistration::where('device_id', $data['device_id']);
$app->content = array('success' => ($registration->delete() == 1));
return $this->json(array('success' => ($registration->delete() == 1)));
}

public function notify() {
Expand Down
5 changes: 3 additions & 2 deletions src/Database/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ class Relationship

public static function getRelation($model, $relation_name) {
$schema = Cache::get($model->getTable());
$debug = json_encode($schema);

if (isset($schema['relationships'])) {
// TODO: refactoring.
// change the way to store relationships to prevent excessive loops
foreach($schema['relationships'] as $relation => $fields) {
foreach($fields as $field => $collection) {
if ($field == $relation_name || $collection == $relation_name) {
$related_collection = App::collection($collection);
// TODO: '$collection' should be always a string
$related_collection_name = (is_array($collection)) ? $collection[0] : $collection;
$related_collection = App::collection($related_collection_name);
return static::getRelationInstance($model, $related_collection, $relation, $field);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public static function migrate($model, $config)
$config['relationships'] = array();
}

// var_dump($table, $config['relationships']);

if (!isset($config['attributes'])) {
$config['attributes'] = array();
}
Expand Down
12 changes: 8 additions & 4 deletions src/Database/Schema/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
class Cache
{
protected static $store;
protected static $local_cache = array();
protected static $tmp_cache = array();

public static function flush() {
static::$tmp_cache = array();
}

public static function forever($collection, $value) {
return static::getStore()->forever($collection, $value);
}

public static function get($collection) {
if (!isset(static::$local_cache[$collection])) {
static::$local_cache[$collection] = static::getStore()->get($collection) ?: array();
if (!isset(static::$tmp_cache[$collection])) {
static::$tmp_cache[$collection] = static::getStore()->get($collection) ?: array();
}
return static::$local_cache[$collection];
return static::$tmp_cache[$collection];
}

public static function getStore() {
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ public static function mount($app)
// Key/Value
$app->get('/key/:name', 'Hook\\Controllers\\KeyValueController:show');
$app->post('/key/:name', 'Hook\\Controllers\\KeyValueController:store');
$app->delete('/key/:name', 'Hook\\Controllers\\KeyValueController:delete');

// Channels
$app->get('/channels/:name+', 'Hook\\Controllers\\ChannelsController:index');
$app->post('/channels/:name+', 'Hook\\Controllers\\ChannelsController:store');

// Push Notifications
$app->post('/push', 'Hook\\Controllers\\PushNotificationController:store');
$app->post('/push/registration', 'Hook\\Controllers\\PushNotificationController:store');
$app->delete('/push', 'Hook\\Controllers\\PushNotificationController:delete');
$app->get('/push/notify', 'Hook\\Controllers\\PushNotificationController:notify');

Expand Down
9 changes: 9 additions & 0 deletions src/Model/DynamicModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ protected static function registerDefaultEvents($table=null)
}
}

/**
* isModified
* @return bool
*/
public function isModified()
{
return count($this->getDirty()) > 0;
}

public function beforeCreate() { }

public function beforeSave()
Expand Down
24 changes: 14 additions & 10 deletions src/Model/PushMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@ public static function boot()
{
parent::boot();
static::registerDefaultEvents();
static::creating(function ($model) { $model->beforeCreate(); });
}

public function beforeCreate()
public function beforeSave()
{
if (!AppContext::getKey()->isServer()) {
throw new ForbiddenException("Need a 'server' key to perform this action.");
}
if (!$this->getAttribute('_id')) {

if (!AppContext::getKey()->isServer()) {
throw new ForbiddenException("Need a 'server' key to perform this action.");
}

if (!$this->getAttribute('message')) {
throw new InternalException("Can't create PushMessage: 'message' is required.");
}

if (!$this->getAttribute('message')) {
throw new InternalException("Can't create PushMessage: 'message' is required.");
$this->setAttribute('status', self::STATUS_QUEUE);
$this->setAttribute('devices', 0);
$this->setAttribute('failure', 0);
}

$this->setAttribute('status', self::STATUS_QUEUE);
$this->setAttribute('devices', 0);
$this->setAttribute('failure', 0);
parent::beforeSave();
}

}
8 changes: 5 additions & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ public function tearDown()
parent::tearDown();
}

public function useApp($id, $db_driver = 'sqlite')
public function useApp($id)
{
$apps = $this->get('apps/list');
$db_driver = getenv('DB_DRIVER') ?: 'mysql';

$apps = $this->get('apps');
if (!isset($apps[0])) {
$this->post('apps', array(
'app' => array('name' => 'phpunit')
));
return $this->useApp($id, $db_driver);
return $this->useApp($id);
}

// associate keys by type
Expand Down
47 changes: 33 additions & 14 deletions tests/integration/key_value/KeyValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,52 @@

class KeyValue extends HTTP_TestCase
{
public function testSetString()
public function testString()
{
$this->post('key/some_string', array('value' => 5));
$key = $this->get('key/some_string');
$this->assertTrue($key['value'] === '5');
$this->post('key/some_string', array('value' => 'Hello world'));
$value = $this->get('key/some_string');
$this->assertTrue($value === 'Hello world');
}

public function testSetInteger()
public function testInteger()
{
$this->post('key/some_integer', array('value' => 5));
$key = $this->get('key/some_integer');
$this->assertTrue($key['value'] === '5', 'int 5 results as "5"');
$value = $this->get('key/some_integer');
$this->assertTrue($value === 5, 'int 5 results as "5"');
}

public function testSetFloat()
public function testFloat()
{
$this->post('key/some_float', array('value' => 9.9));
$key = $this->get('key/some_float');
$this->assertTrue($key['value'] === '9.9', 'float 9.9 results as "9.9"');
$value = $this->get('key/some_float');
$this->assertTrue($value === 9.9, 'float 9.9 results as "9.9"');
}

public function testSetArray()
public function testArray()
{
$response = $this->post('key/some_array', array('value' => array('one', 'two', 'three')));
$this->assertTrue(isset($response['error']), "array key-values are not supported.");
// $key = $this->get('key/some_array');
// $this->assertTrue($key['value'] === array('one', 'two', 'three'));
$value = $this->get('key/some_array');
$this->assertTrue($value === array('one', 'two', 'three'), "array ['one', 'two', 'three'] ok.");
}

public function testNested()
{
$response = $this->post('key/nested', array('value' => array(
'nested' => array(
'complex' => 999,
'fields' => "are",
'supported' => "too!"
),
)));
$value = $this->get('key/nested');

$this->assertTrue($value === array(
'nested' => array(
'complex' => 999,
'fields' => "are",
'supported' => "too!"
),
), "nested key/value ok.");
}

}
11 changes: 6 additions & 5 deletions tests/integration/push/PushRegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,28 @@ public function testCreateMessageFail()
{
$this->setKeyType(AppKey::TYPE_BROWSER);
$message = $this->post('collection/push_messages', array('message' => "Hello!"));
$this->assertTrue(is_array($message) && is_string($message['error']));
$this->assertTrue(is_array($message) && isset($message['error']) && is_string($message['error']), "Shouldn't be able to create push message as a BROWSER.");
$this->setKeyType(AppKey::TYPE_DEVICE);
$message = $this->post('collection/push_messages', array('message' => "Hello!"));
$this->assertTrue(is_array($message) && is_string($message['error']));
$this->assertTrue(is_array($message) && isset($message['error']) && is_string($message['error']), "Shouldn't be able to create push message as a DEVICE.");
}

public function testCreateMessageSuccess()
{
$this->setKeyType(AppKey::TYPE_DEVICE);
$this->setKeyType(AppKey::TYPE_SERVER);
$message = $this->post('collection/push_messages', array('message' => "Hello!"));
$this->assertTrue(is_array($message) && $message['message'] == "Hello!");
$this->assertTrue(is_array($message) && isset($message['message']) && $message['message'] == "Hello!", "Should be able to create a push message as a SERVER");
}

public function testNotify()
{
$this->setKeyType(AppKey::TYPE_SERVER);
$notify = $this->get('push/notify', array('X-Scheduled-Task' => 'yes'));

$this->assertTrue($notify['push_messages'] === 1);
$this->assertTrue($notify['devices'] >= 2);
$this->assertTrue($notify['success'] === 0);
$this->assertTrue($notify['failure'] === 0);

}

}
4 changes: 3 additions & 1 deletion tests/unit/collection/CollectionRelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Hook\Model\App as App;
use Hook\Database\Schema as Schema;
use Hook\Cache\Cache as Cache;
use Hook\Database\Schema\Cache as SchemaCache;

class CollectionRelationshipTest extends TestCase
{
Expand All @@ -23,7 +24,7 @@ public function setUp()
Schema\Builder::migrate(App::collection('books')->getModel(), array(
'relationships' => array('belongs_to' => array('author'))
));

SchemaCache::flush();

// clear tables before running tests
App::collection('authors')->truncate();
Expand All @@ -47,6 +48,7 @@ public function setUp()
Schema\Builder::migrate(App::collection('teams')->getModel(), array(
'relationships' => array('has_many' => 'matches')
));
SchemaCache::flush();

App::collection('teams')->truncate();
App::collection('matches')->truncate();
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/schema/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Hook\Model\App as App;
use Hook\Database\Schema as Schema;
use Hook\Cache\Cache as Cache;
use Hook\Database\Schema\Cache as SchemaCache;

class SchemaBuilderTest extends TestCase
{

public function setUp()
{
parent::setUp();
}

public function testMigrate()
{
Cache::flush();

// books / authors / contacts
Schema\Builder::migrate(App::collection('contacts')->getModel(), array(
'relationships' => array('belongs_to' => 'author')
));
Schema\Builder::migrate(App::collection('authors')->getModel(), array(
'relationships' => array('has_many' => array('contacts'))
));

SchemaCache::flush();

$author = App::collection('authors')->create(array('name' => "Rasmus Lerdorf"));
$this->assertTrue(get_class($author->contacts()) == "Illuminate\\Database\\Eloquent\\Relations\\HasMany");
}

}


0 comments on commit 138880c

Please sign in to comment.