MockSource.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\mocks\data;
  9. use lithium\util\Inflector;
  10. class MockSource extends \lithium\data\Source {
  11. protected $_classes = array(
  12. 'entity' => 'lithium\data\entity\Record',
  13. 'set' => 'lithium\data\collection\RecordSet',
  14. 'relationship' => 'lithium\data\model\Relationship',
  15. 'schema' => 'lithium\data\Schema'
  16. );
  17. protected $_mockPosts = array(
  18. 'id' => array('type' => 'int', 'length' => '10', 'null' => false, 'default' => null),
  19. 'user_id' => array(
  20. 'type' => 'int', 'length' => '10', 'null' => true, 'default' => null
  21. ),
  22. 'title' => array(
  23. 'type' => 'varchar', 'length' => '255', 'null' => true, 'default' => null
  24. ),
  25. 'body' => array(
  26. 'type' => 'text', 'length' => null, 'null' => true, 'default' => null
  27. ),
  28. 'created' => array(
  29. 'type' => 'datetime', 'length' => null, 'null' => true, 'default' => null
  30. ),
  31. 'modified' => array(
  32. 'type' => 'datetime', 'length' => null, 'null' => true, 'default' => null
  33. ),
  34. 'status' => array(
  35. 'type' => 'tinyint', 'length' => '1', 'null' => false, 'default' => '0'
  36. )
  37. );
  38. protected $_mockComments = array(
  39. 'id' => array(
  40. 'type' => 'int', 'length' => '10', 'null' => false, 'default' => null
  41. ),
  42. 'comment_type_id' => array(
  43. 'type' => 'int', 'length' => '10', 'null' => false, 'default' => null
  44. ),
  45. 'article_id' => array(
  46. 'type' => 'int', 'length' => '10', 'null' => false, 'default' => null
  47. ),
  48. 'comment_id' => array(
  49. 'type' => 'int', 'length' => '10', 'null' => false, 'default' => null
  50. ),
  51. 'user_id' => array(
  52. 'type' => 'int', 'length' => '10', 'null' => false, 'default' => null
  53. ),
  54. 'created' => array(
  55. 'type' => 'datetime', 'length' => null, 'null' => false, 'default' => null
  56. ),
  57. 'body' => array(
  58. 'type' => 'text', 'length' => null, 'null' => false, 'default' => null
  59. ),
  60. 'subscribed' => array(
  61. 'type' => 'tinyint', 'length' => '1', 'null' => false, 'default' => null
  62. ),
  63. 'published' => array(
  64. 'type' => 'tinyint', 'length' => '1', 'null' => false, 'default' => null
  65. )
  66. );
  67. protected $_mockTags = array(
  68. 'id' => array(
  69. 'type' => 'int', 'length' => '10', 'null' => false, 'default' => null
  70. ),
  71. 'linked' => array(
  72. 'type' => 'int', 'length' => '10', 'null' => true, 'default' => null
  73. ),
  74. 'name' => array(
  75. 'type' => 'varchar', 'length' => '20', 'null' => true, 'default' => null
  76. ),
  77. 'keyname' => array(
  78. 'type' => 'varchar', 'length' => '20', 'null' => true, 'default' => null
  79. )
  80. );
  81. protected $_postsTags = array(
  82. 'id' => array('type' => 'int'),
  83. 'post_id' => array('type' => 'int'),
  84. 'tag_id' => array('type' => 'int'),
  85. );
  86. protected $_mockCreators = array(
  87. 'id' => array('type' => 'int'),
  88. 'name' => array(
  89. 'default' => 'Moe',
  90. 'type' => 'string',
  91. 'null' => false
  92. ),
  93. 'sign' => array(
  94. 'default' => 'bar',
  95. 'type' => 'string',
  96. 'null' => false
  97. ),
  98. 'age' => array(
  99. 'default' => 0,
  100. 'type' => 'number',
  101. 'null' => false
  102. )
  103. );
  104. public function connect() {
  105. return ($this->_isConnected = true);
  106. }
  107. public function disconnect() {
  108. return !($this->_isConnected = false);
  109. }
  110. public function sources($class = null) {
  111. return array('mock_posts', 'mock_comments', 'mock_tags', 'posts_tags');
  112. }
  113. public function describe($entity, $schema = array(), array $meta = array()) {
  114. $source = '_' . Inflector::camelize($entity, false);
  115. $fields = isset($this->$source) ? $this->$source : array();
  116. return $this->_instance('schema', compact('fields'));
  117. }
  118. public function create($query, array $options = array()) {
  119. return compact('query', 'options');
  120. }
  121. public function read($query, array $options = array()) {
  122. return compact('query', 'options');
  123. }
  124. public function update($query, array $options = array()) {
  125. return compact('query', 'options');
  126. }
  127. public function delete($query, array $options = array()) {
  128. return compact('query', 'options');
  129. }
  130. public function schema($query, $resource = null, $context = null) {
  131. }
  132. public function result($type, $resource, $context) {
  133. }
  134. public function cast($entity, array $data = array(), array $options = array()) {
  135. $defaults = array('first' => false);
  136. $options += $defaults;
  137. return $options['first'] ? reset($data) : $data;
  138. }
  139. public function relationship($class, $type, $name, array $config = array()) {
  140. $field = Inflector::underscore(Inflector::singularize($name));
  141. $key = "{$field}_id";
  142. $primary = $class::meta('key');
  143. if (is_array($primary)) {
  144. $key = array_combine($primary, $primary);
  145. } elseif ($type === 'hasMany' || $type === 'hasOne') {
  146. if ($type === 'hasMany') {
  147. $field = Inflector::pluralize($field);
  148. }
  149. $secondary = Inflector::underscore(Inflector::singularize($class::meta('name')));
  150. $key = array($primary => "{$secondary}_id");
  151. }
  152. $from = $class;
  153. $fieldName = $field;
  154. $config += compact('type', 'name', 'key', 'from', 'fieldName');
  155. return $this->_instance('relationship', $config);
  156. }
  157. public function calculation($type, $query, array $options = array()) {
  158. $query->calculate($type);
  159. return compact('query', 'options');
  160. }
  161. }
  162. ?>