Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Commit

Permalink
Merge 1310ef7 into 14d2037
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-kabala committed Feb 10, 2017
2 parents 14d2037 + 1310ef7 commit f80e764
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ before_script:
- (cd cphalcon/ext; export CFLAGS="-g3 -O1 -fno-delete-null-pointer-checks -Wall"; phpize && ./configure --enable-phalcon && make -j4 && sudo make install && phpenv config-add ../unit-tests/ci/phalcon.ini)
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev

script:
- cp tests/config.sample.php tests/config.php
- mkdir -p tests/fixtures/cache
Expand All @@ -29,4 +29,4 @@ script:
- php vendor/bin/phpunit -c travis/phpunit.xml.dist

after_script:
- php vendor/bin/coveralls -v
- php vendor/bin/coveralls -v
16 changes: 16 additions & 0 deletions src/DI/Scaffolding.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ class Scaffolding implements ScaffoldingInterface
*/
protected $formName;

/**
* Query
*
* @var array
*/
protected $query = [];

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -135,6 +142,15 @@ public function setModelName($name)
return $this;
}

/**
* {@inheritdoc}
*/
public function setQuery($query)
{
$this->adapter->setQuery($query);
return $this;
}

/**
* {@inheritdoc}
*/
Expand Down
18 changes: 18 additions & 0 deletions src/DI/Scaffolding/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class Mongo implements AdapterInterface, ScaffoldingAdapterInterface
*/
protected $scaffolding;

/**
* Query for paginator
*
* @var array
*/
protected $query = [];

/**
* Constructor
* Verifies services required by Mongo
Expand All @@ -49,6 +56,16 @@ public function __construct()
$this->setupExtraServices($di);
}

/**
* {@inheritdoc}
*/
public function setQuery($query)
{
$this->query = $query;

return $this;
}

/**
* {@inheritdoc}
*/
Expand All @@ -69,6 +86,7 @@ public function retrieveOne($id)
public function getPaginator($page = 1, $limit = 10)
{
return new PaginatorAdapterMongo(array(
'query' => $this->query,
'model' => $this->scaffolding->getRecord(),
'limit' => $limit,
'page' => $page
Expand Down
19 changes: 18 additions & 1 deletion src/DI/Scaffolding/Adapter/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class Mysql implements AdapterInterface, ScaffoldingAdapterInterface
*/
protected $scaffolding;

/**
* Query for paginator
*
* @var array
*/
protected $query = [];

/**
* Constructor
*/
Expand All @@ -47,6 +54,16 @@ public function __construct()
$this->verifyRequiredServices($di);
}

/**
* {@inheritdoc}
*/
public function setQuery($query)
{
$this->query = $query;

return $this;
}

/**
* {@inheritdoc}
*/
Expand All @@ -71,7 +88,7 @@ public function getPaginator($page = 1, $limit = 10)
$this->ensureScaffolding();

return new PaginatorAdapterModel(array(
'data' => (object) call_user_func(array($this->scaffolding->getRecord(), 'find')),
'data' => call_user_func(array($this->scaffolding->getRecord(), 'find'), $this->query),
'limit' => $limit,
'page' => $page
));
Expand Down
8 changes: 8 additions & 0 deletions src/DI/Scaffolding/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ interface AdapterInterface
*/
public function retrieveOne($id);

/**
* Sets query for the paginator
*
* @param $id
* @return mixed
*/
public function setQuery($query);

/**
* Retrieve list of records as paginator object.
*
Expand Down
8 changes: 8 additions & 0 deletions src/DI/ScaffoldingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public function getForm($entity = null);
*/
public function setFormName($name);

/**
* Sets query for paginator
*
* @param $query
* @return mixed
*/
public function setQuery($query);

/**
* Sets model name
*
Expand Down
8 changes: 8 additions & 0 deletions src/Mvc/Controller/CrudAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public function initialize()
*/
protected $modelName;

/**
* Query that will be used in index action
*
* @var array
*/
protected $query = [];

/**
* Array of fields names that will be used in index action
*
Expand Down Expand Up @@ -104,6 +111,7 @@ protected function initializeScaffolding()

$this->scaffolding->setModelName($this->modelName);
$this->scaffolding->setFormName($this->formName);
$this->scaffolding->setQuery($this->query);
}

/**
Expand Down
34 changes: 32 additions & 2 deletions tests/DI/Scaffolding/Adapter/MysqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,47 @@ public function testShouldReturnValidPagination()
$mysql = new \Vegas\DI\Scaffolding\Adapter\Mysql();
$scaffolding = new Scaffolding($mysql);

$scaffolding->setModelName('\Test\Models\Fake');
$scaffolding->setModelName('\Test\Models\FakeSql');
$scaffolding->setFormName('\Test\Forms\Fake');
$scaffolding->doCreate([
'fake_field' => 'fake'
]);
$scaffolding->doCreate([
'fake_field' => 'fake2'
]);

$pagination = $mysql->getPaginator();
$paginate = $pagination->getPaginate();

$this->assertInstanceOf('\Phalcon\Paginator\Adapter\Model', $pagination);
$this->assertInstanceOf('\stdClass', $paginate);
$this->assertNotEquals(0, $paginate->total_items);
}

public function testShouldReturnFilteredPagination()
{
$mysql = new \Vegas\DI\Scaffolding\Adapter\Mysql();
$scaffolding = new Scaffolding($mysql);

$scaffolding->setModelName('\Test\Models\FakeSql');
$scaffolding->setFormName('\Test\Forms\Fake');
$scaffolding->doCreate([
'fake_field' => 'fake'
]);
$scaffolding->doCreate([
'fake_field' => 'fake2'
]);
$scaffolding->doCreate([
'fake_field' => 'fake'
]);
$scaffolding->setQuery('fake_field = "fake2"');

$pagination = $mysql->getPaginator();
$paginate = $pagination->getPaginate();

$this->assertInstanceOf('\Phalcon\Paginator\Adapter\Model', $pagination);
$this->assertInstanceOf('\stdClass', $pagination->getPaginate());
$this->assertInstanceOf('\stdClass', $paginate);
$this->assertNotEquals(0, $paginate->total_items);
$this->assertEquals('fake2', $paginate->items[0]->fake_field);
}
}
29 changes: 29 additions & 0 deletions tests/DI/ScaffoldingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ protected function setUp()
$record = new \Vegas\Tests\Stub\Models\FakeModel();
$record->fake_field = 'test';
$record->save();

$record = new \Vegas\Tests\Stub\Models\FakeModel();
$record->fake_field = 'test_query';
$record->save();

$this->record = $record;
}
Expand Down Expand Up @@ -133,4 +137,29 @@ public function testDoDelete()
$this->assertInstanceOf('\Vegas\DI\Scaffolding\Exception\RecordNotFoundException', $ex);
}
}

public function testShouldReturnValidPagination()
{
$pagination = $this->scaffolding->doPaginate();

$this->assertInstanceOf('\Vegas\Paginator\Adapter\Mongo', $pagination);

$results = $pagination->getResults();

$this->assertNotCount(0, $results);
$this->assertInstanceOf('\Vegas\Tests\Stub\Models\FakeModel', $results[0]);
}

public function testShouldReturnFilteredPagination()
{
$this->scaffolding->setQuery([
'fake_field' => 'test_query'
]);

$pagination = $this->scaffolding->doPaginate();
$results = $pagination->getResults();

$this->assertNotCount(0, $results);
$this->assertEquals('test_query', $results[0]->fake_field);
}
}
27 changes: 27 additions & 0 deletions tests/fixtures/app/modules/Test/models/FakeSql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* This file is part of Vegas package
*
* @author Slawomir Zytko <slawek@amsterdam-standard.pl>
* @copyright Amsterdam Standard Sp. Z o.o.
* @homepage http://vegas-cmf.github.io
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Test\Models;

use Vegas\Db\Decorator\ModelAbstract;

class FakeSql extends ModelAbstract
{
public function getSource()
{
return 'fake';
}

protected $mappings = array(
'arraydata' => 'json'
);
}
3 changes: 3 additions & 0 deletions tests/fixtures/public/assets/css/vendor.another.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
p {
color: #111;
}
3 changes: 3 additions & 0 deletions tests/fixtures/public/assets/css/vendor.test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: #FDFDFD;
}
3 changes: 3 additions & 0 deletions tests/fixtures/public/assets/js/vendor.example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* Created by arius on 30.05.14.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
p {
color: #111;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: #FDFDFD;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* Created by arius on 30.05.14.
*/

0 comments on commit f80e764

Please sign in to comment.