forked from CakePHP-Copula/Copula
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add HABTM association handle, write tests, see #18
- Loading branch information
imsamurai
committed
Oct 29, 2013
1 parent
67e251c
commit 4ae1a29
Showing
12 changed files
with
474 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/** | ||
* Author: imsamurai <[email protected]> | ||
* Date: Oct 29, 2013 | ||
* Time: 5:54:32 PM | ||
* | ||
*/ | ||
$config['TestHttpSource']['config_version'] = 2; | ||
|
||
$CF = HttpSourceConfigFactory::instance(); | ||
$Config = $CF->config(); | ||
|
||
$Config | ||
->add( | ||
$CF->endpoint() | ||
->id(1) | ||
->methodRead() | ||
->table('default') | ||
->path('/imsamurai/cakephp-httpsource-datasource/master/Test/Data/default.json') | ||
->result($CF->result()->map(function($result) { | ||
return array($result); | ||
})) | ||
) | ||
->add( | ||
$CF->endpoint() | ||
->id(2) | ||
->methodRead() | ||
->table('documents') | ||
->path('/imsamurai/cakephp-httpsource-datasource/master/Test/Data/documents:id.json') | ||
->addCondition($CF->condition()->name('id')->map(function($v) { | ||
return implode('.', $v); | ||
})) | ||
->result($CF->result()->map(function($result) { | ||
return $result; | ||
})) | ||
) | ||
|
||
; | ||
|
||
$config['TestHttpSource']['config'] = $Config; | ||
|
||
|
||
$config['TestHttpSource']['cache_name'] = false; |
31 changes: 31 additions & 0 deletions
31
Plugin/TestHttpSource/Model/Datasource/Http/TestHttpSource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/** | ||
* Author: imsamurai <[email protected]> | ||
* Date: Oct 29, 2013 | ||
* Time: 5:44:49 PM | ||
* | ||
*/ | ||
|
||
App::uses('HttpSource', 'HttpSource.Model/Datasource'); | ||
App::uses('TestHttpSourceConnection', 'TestHttpSource.Model/Datasource'); | ||
|
||
class TestHttpSource extends HttpSource { | ||
|
||
/** | ||
* The description of this data source | ||
* | ||
* @var string | ||
*/ | ||
public $description = 'Test DataSource'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @param array $config | ||
* @param HttpSourceConnection $Connection | ||
*/ | ||
public function __construct($config = array(), HttpSourceConnection $Connection = null) { | ||
parent::__construct($config, new TestHttpSourceConnection($config)); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
Plugin/TestHttpSource/Model/Datasource/TestHttpSourceConnection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/** | ||
* Author: imsamurai <[email protected]> | ||
* Date: Oct 29, 2013 | ||
* Time: 5:45:06 PM | ||
* | ||
*/ | ||
App::uses('HttpSourceConnection', 'HttpSource.Model/Datasource'); | ||
|
||
class TestHttpSourceConnection extends HttpSourceConnection { | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param array $config | ||
* @param HttpSocket $Transport | ||
*/ | ||
public function __construct(array $config = array(), HttpSocket $Transport = null) { | ||
parent::__construct($config, $Transport); | ||
|
||
$this->setDecoder(array('text/plain'), function(HttpSocketResponse $Response) { | ||
return json_decode((string) $Response, true); | ||
}, false); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/** | ||
* Author: imsamurai <[email protected]> | ||
* Date: Oct 29, 2013 | ||
* Time: 6:02:17 PM | ||
* Format: http://book.cakephp.org/2.0/en/models.html | ||
*/ | ||
|
||
App::uses('HttpSourceModel', 'HttpSource.Model'); | ||
|
||
/** | ||
* TestHttpSourceModel Model | ||
*/ | ||
class TestHttpSourceModel extends HttpSourceModel { | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @var string | ||
*/ | ||
public $name = 'TestHttpSourceModel'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @var string | ||
*/ | ||
public $useTable = 'default'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @var string | ||
*/ | ||
public $useDbConfig = 'http'; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/** | ||
* Author: imsamurai <[email protected]> | ||
* Date: 29.10.2013 | ||
* Time: 21:50:00 | ||
*/ | ||
class AllHttpSourceTest extends PHPUnit_Framework_TestSuite { | ||
|
||
/** | ||
* Suite define the tests for this suite | ||
* | ||
* @return void | ||
*/ | ||
public static function suite() { | ||
$suite = new CakeTestSuite('All Http Source Tests'); | ||
|
||
$path = App::pluginPath('HttpSource') . 'Test' . DS . 'Case' . DS; | ||
$suite->addTestDirectoryRecursive($path); | ||
return $suite; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/** | ||
* Author: imsamurai <[email protected]> | ||
* Date: Oct 29, 2013 | ||
* Time: 6:06:31 PM | ||
* Format: http://book.cakephp.org/2.0/en/development/testing.html | ||
*/ | ||
App::uses('HttpSourceTest', 'HttpSource.Test'); | ||
|
||
/** | ||
* BasicsTest | ||
*/ | ||
class BasicsTest extends HttpSourceTest { | ||
|
||
public function testRead() { | ||
$this->HttpModel->setSource('default'); | ||
$result = $this->HttpModel->field('name'); | ||
|
||
$this->assertSame('imsamurai', $result); | ||
} | ||
|
||
public function testRead2() { | ||
$this->HttpModel->setSource('documents'); | ||
$result = $this->HttpModel->find('all', array( | ||
'conditions' => array( | ||
'id' => array(1,2) | ||
) | ||
)); | ||
|
||
$this->assertCount(2, $result); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/** | ||
* Author: imsamurai <[email protected]> | ||
* Date: Oct 29, 2013 | ||
* Time: 5:28:22 PM | ||
* Format: http://book.cakephp.org/2.0/en/development/testing.html | ||
*/ | ||
|
||
App::uses('HttpSourceTest', 'HttpSource.Test'); | ||
|
||
/** | ||
* HttpSourceToDboAssociationTest | ||
*/ | ||
class ToDboAssociationTest extends HttpSourceTest { | ||
|
||
/** | ||
* User model | ||
* | ||
* @var User | ||
*/ | ||
public $User = null; | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUp(){ | ||
parent::setUp(); | ||
$this->User = new User(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @var array | ||
*/ | ||
public $fixtures = array( | ||
'plugin.HttpSource.User', | ||
'plugin.HttpSource.UsersDocument' | ||
); | ||
|
||
public function testHABTM() { | ||
$this->User->Documents->useDbConfig = 'testHttpSource'; | ||
$results = $this->User->find('all', array('recursive' => 3)); | ||
debug($results); | ||
foreach ($results as $result) { | ||
$this->assertNotEmpty($result['Documents']); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/** | ||
* Author: imsamurai <[email protected]> | ||
* Date: Oct 29, 2013 | ||
* Time: 9:18:11 PM | ||
* Format: http://book.cakephp.org/2.0/en/views.html | ||
* | ||
*/ | ||
App::uses('HttpSourceModel', 'HttpSource.Model'); | ||
|
||
class Document extends HttpSourceModel { | ||
|
||
public $name = 'Document'; | ||
public $useTable = 'documents'; | ||
|
||
} | ||
|
||
class User extends AppModel { | ||
|
||
public $name = 'User'; | ||
public $useTable = 'users'; | ||
public $useDbConfig = 'test'; | ||
public $hasAndBelongsToMany = array( | ||
'Documents' => array( | ||
'joinTable' => 'users_documents', | ||
'dependent' => false | ||
) | ||
); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/** | ||
* User Fixture | ||
*/ | ||
class UserFixture extends CakeTestFixture { | ||
|
||
public $useDbConfig = 'test'; | ||
public $table = 'users'; | ||
|
||
/** | ||
* Fields | ||
* | ||
* @var array | ||
*/ | ||
public $fields = array( | ||
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'length' => 20, 'key' => 'primary'), | ||
'name' => array('type' => 'string', 'null' => false, 'length' => 100), | ||
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'MyISAM') | ||
); | ||
|
||
/** | ||
* Records | ||
* | ||
* @var array | ||
*/ | ||
public $records = array( | ||
array('id' => 1, 'name' => 'dan'), | ||
array('id' => 2, 'name' => 'robert') | ||
); | ||
|
||
} |
Oops, something went wrong.