Skip to content

Commit

Permalink
added fixtures
Browse files Browse the repository at this point in the history
added relational queries
fixed get's method parameters
  • Loading branch information
simialbi committed Apr 25, 2019
1 parent 1cffee4 commit ccd0a9e
Show file tree
Hide file tree
Showing 15 changed files with 448 additions and 53 deletions.
22 changes: 11 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ install:
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- travis_retry composer install --prefer-dist --no-interaction

before_script:
- |
if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
PHPUNIT_FLAGS="--coverage-clover=coverage.clover"
fi
#before_script:
# - |
# if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
# PHPUNIT_FLAGS="--coverage-clover=coverage.clover"
# fi
script:
- ./vendor/bin/phpunit --verbose $PHPUNIT_FLAGS

after_script:
- |
if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
travis_retry wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover coverage.clover
fi
#after_script:
# - |
# if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
# travis_retry wget https://scrutinizer-ci.com/ocular.phar
# php ocular.phar code-coverage:upload --format=php-clover coverage.clover
# fi
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ It is based on [ApexWire's](https://github.com/ApexWire) [yii2-restclient](https
[![Latest Stable Version](https://poser.pugx.org/simialbi/yii2-rest-client/v/stable?format=flat-square)](https://packagist.org/packages/simialbi/yii2-rest-client)
[![Total Downloads](https://poser.pugx.org/simialbi/yii2-rest-client/downloads?format=flat-square)](https://packagist.org/packages/simialbi/yii2-rest-client)
[![License](https://poser.pugx.org/simialbi/yii2-rest-client/license?format=flat-square)](https://packagist.org/packages/simialbi/yii2-rest-client)
[![Build Status](https://travis-ci.com/simialbi/yii2-rest-client.svg?branch=master)](https://travis-ci.com/simialbi/yii2-rest-client)

## Resources
* [yii2-restclient](https://github.com/ApexWire/yii2-restclient)
Expand Down Expand Up @@ -135,4 +136,4 @@ The usage how to define the active record (rules, behaviors etc.) is the same li
## Acknowledgments
* [ApexWire's](https://github.com/ApexWire) [yii2-restclient](https://github.com/ApexWire/yii2-restclient)
* [Yii2 HiArt](https://github.com/hiqdev/yii2-hiart).
* [mikolajzieba](https://github.com/mikolajzieba)
* [mikolajzieba](https://github.com/mikolajzieba)
118 changes: 118 additions & 0 deletions src/ActiveFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* @package yii2-rest-client
* @author Simon Karlen <[email protected]>
* @copyright Copyright © 2019 Simon Karlen
*/

namespace simialbi\yii2\rest;

use yii\base\InvalidConfigException;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
use yii\test\BaseActiveFixture;

class ActiveFixture extends BaseActiveFixture
{
/**
* @var Connection|array|string the DB connection object or the application component ID of the DB connection.
* After the DbFixture object is created, if you want to change this property, you should only assign it
* with a DB connection object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/
public $db = 'rest';
/**
* @var string the name of the model that this fixture is about. If this property is not set,
* the model name will be determined via [[modelClass]].
* @see modelClass
*/
public $modelName;

/**
* @var \yii\db\ActiveRecord[] the loaded AR models
*/
private $_models = [];


/**
* {@inheritDoc}
*
* @throws InvalidConfigException
*/
public function init()
{
parent::init();
if ($this->modelClass === null && $this->modelName === null) {
throw new InvalidConfigException('Either "modelClass" or "modelName" must be set.');
}
if ($this->modelName === null) {
$this->modelName = Inflector::camel2id(StringHelper::basename($this->modelClass), '-');
}
}

/**
* {@inheritDoc}
*
* @throws InvalidConfigException
* @throws \ReflectionException
*/
public function load()
{
$this->data = [];
foreach ($this->getData() as $alias => $row) {
$this->data[$alias] = $row;
}
}

/**
* {@inheritDoc}
*
* @throws InvalidConfigException
* @throws \ReflectionException
*/
protected function getData()
{
if ($this->dataFile === null) {
if ($this->dataDirectory !== null) {
$dataFile = $this->modelName . '.php';
} else {
$class = new \ReflectionClass($this);
$dataFile = dirname($class->getFileName()) . '/data/' . $this->modelName . '.php';
}

return $this->loadData($dataFile, false);
}
return parent::getData();
}

/**
* {@inheritDoc}
*/
public function getModel($name)
{
if (!isset($this->data[$name])) {
return null;
}
if (array_key_exists($name, $this->_models)) {
return $this->_models[$name];
}

if ($this->modelClass === null) {
throw new InvalidConfigException('The "modelClass" property must be set.');
}
$row = $this->data[$name];
/* @var $modelClass ActiveRecord */
$modelClass = $this->modelClass;
$keys = [];
foreach ($modelClass::primaryKey() as $key) {
$keys[$key] = isset($row[$key]) ? $row[$key] : null;
}

/* @var $model ActiveRecord */
$model = new $modelClass();
$model->setOldAttributes($row);
$model->setAttributes($row, false);

return $this->_models[$name] = $model;
}
}
66 changes: 43 additions & 23 deletions src/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,6 @@ class ActiveQuery extends Query implements ActiveQueryInterface
*/
public $joinWith = [];

/**
* @var array options for search
*/
public $options = [];

/**
* Constructor.
*
* @param string $modelClass the model class associated with this query
* @param array $config configurations to be applied to the newly created query object
*/
public function __construct($modelClass, $config = [])
{
$this->modelClass = $modelClass;
parent::__construct($config);
}


/**
* Creates a DB command that can be used to execute this query.
*
Expand All @@ -71,10 +53,6 @@ public function createCommand($db = null)
$this->from($modelClass::modelName());
}

// if ($this->searchModel === null) {
// $this->searchModel = mb_substr(mb_strrchr($this->modelClass, '\\'), 1) . 'Search';
// }

return parent::createCommand($db);
}

Expand Down Expand Up @@ -104,14 +82,56 @@ public function one($db = null)

/**
* {@inheritdoc}
* @throws InvalidConfigException
*/
public function prepare($builder)
{
if (!empty($this->joinWith)) {
$this->buildJoinWith();
$this->joinWith = null;
}
return $this;

if ($this->primaryModel === null) {
// eager loading
$query = Query::create($this);
} else {
// lazy loading of a relation
$where = $this->where;

if ($this->via instanceof self) {
// via junction table
$viaModels = $this->via->findJunctionRows([$this->primaryModel]);
$this->filterByModels($viaModels);
} elseif (is_array($this->via)) {
// via relation
/* @var $viaQuery ActiveQuery */
list($viaName, $viaQuery) = $this->via;
if ($viaQuery->multiple) {
if ($this->primaryModel->isRelationPopulated($viaName)) {
$viaModels = $this->primaryModel->$viaName;
} else {
$viaModels = $viaQuery->all();
$this->primaryModel->populateRelation($viaName, $viaModels);
}
} else {
if ($this->primaryModel->isRelationPopulated($viaName)) {
$model = $this->primaryModel->$viaName;
} else {
$model = $viaQuery->one();
$this->primaryModel->populateRelation($viaName, $model);
}
$viaModels = $model === null ? [] : [$model];
}
$this->filterByModels($viaModels);
} else {
$this->filterByModels([$this->primaryModel]);
}

$query = Query::create($this);
$this->andWhere($where);
}

return $query;
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,10 @@ public static function primaryKey()
* @return ActiveQuery
* @throws InvalidConfigException
*/
public static function find($options = [])
public static function find()
{
$config = [
'class' => 'simialbi\yii2\rest\ActiveQuery',
'options' => $options
];

/* @var $query ActiveQuery */
$query = Yii::createObject($config, [get_called_class()]);
$query = Yii::createObject(ActiveQuery::class, [get_called_class()]);

return $query;
}
Expand Down
15 changes: 11 additions & 4 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,24 @@ public function getRawUrl()
}

/**
* @return mixed
* Executes the SQL statement and returns ALL rows at once.
* @param int $fetchMode for compatibility with [[\yii\db\Command]]
* @return array all rows of the query result. Each array element is an array representing a row of data.
* An empty array is returned if the query results in nothing.
*/
public function queryAll()
public function queryAll($fetchMode = null)
{
return $this->queryInternal();
}

/**
* @return mixed
* Executes the SQL statement and returns the first row of the result.
* This method is best used when only the first row of result is needed for a query.
* @param int $fetchMode for compatibility with [[\yii\db\Command]]
* @return array|false the first row (in terms of an array) of the query result. False is returned if the query
* results in nothing.
*/
public function queryOne()
public function queryOne($fetchMode = null)
{
/* @var $class ActiveRecord */
$class = $this->modelClass;
Expand Down
19 changes: 15 additions & 4 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\helpers\Url;
use yii\httpclient\Client;
use yii\httpclient\Response;
use yii\web\HeaderCollection;
Expand Down Expand Up @@ -177,7 +176,8 @@ public function getQueryBuilder()
*/
public function get($url, $data = [])
{
return $this->request('get', $url, $data);
array_unshift($data, $url);
return $this->request('get', $data);
}

/**
Expand Down Expand Up @@ -275,9 +275,20 @@ public function getHandler()
*/
protected function request($method, $url, $data = [])
{
if (is_array($url)) {
$path = array_shift($url);
$query = http_build_query($url);

array_unshift($url, $path);

$path .= '?' . $query;
} else {
$path = $url;
}

$headers = [];
$method = strtoupper($method);
$profile = $method . ' ' . Url::to($url) . '#' . (is_array($data) ? http_build_query($data) : $data);
$profile = $method . ' ' . $this->handler->baseUrl . '/' . $path . '#' . (is_array($data) ? http_build_query($data) : $data);

if ($auth = $this->getAuth()) {
$headers['Authorization'] = $auth;
Expand All @@ -287,7 +298,7 @@ protected function request($method, $url, $data = [])
/* @var $request \yii\httpclient\Request */

Yii::debug($method, __METHOD__ . '-method');
Yii::debug($this->handler->baseUrl . '/' . $url, __METHOD__ . '-url');
Yii::debug($this->handler->baseUrl . '/' . $path, __METHOD__ . '-url');
Yii::debug($data, __METHOD__ . '-data');
Yii::debug($headers, __METHOD__ . '-headers');

Expand Down
Loading

0 comments on commit ccd0a9e

Please sign in to comment.