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

Commit

Permalink
Merge pull request #44 from archdevil666pl/master
Browse files Browse the repository at this point in the history
Added repository pattern samples
  • Loading branch information
arius86 committed Apr 23, 2015
2 parents b4c4fcb + d79675e commit c9da3e5
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ before_install:

before_script:
- phpenv config-add travis/php.ini
- git clone -q --depth=1 https://github.com/phalcon/cphalcon.git -b master
- git clone -q --depth=1 https://github.com/phalcon/cphalcon.git -b phalcon-v1.3.4
- (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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"require-dev": {
"phpunit/phpunit": "4.0.*",
"satooshi/php-coveralls": "dev-master",
"ext-mongo": ">=1.5.0"
"ext-mongo": ">=1.5"
},
"autoload": {
"psr-4": {
Expand Down
48 changes: 48 additions & 0 deletions src/Db/Decorator/Helper/RepositoryTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* This file is part of Vegas package
*
* @author Radosław Fąfara <radek@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 Vegas\Db\Decorator\Helper;

/**
* Trait RepositoryTrait
* Sample trait to be used by Model/Collection repositories.
* @package Vegas\Db\Decorator\Helper
*/
trait RepositoryTrait
{
/**
* {@inheritdoc}
*/
public static function find($parameters=null)
{
$classname = get_parent_class(__CLASS__);
return $classname::find($parameters);
}

/**
* {@inheritdoc}
*/
public static function findFirst($parameters=null)
{
$classname = get_parent_class(__CLASS__);
return $classname::findFirst($parameters);
}

/**
* {@inheritdoc}
*/
public static function findById($id)
{
$classname = get_parent_class(__CLASS__);
return $classname::findById($id);
}
}
17 changes: 13 additions & 4 deletions tests/Db/Decorator/CollectionAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Phalcon\Utils\Slug;
use Vegas\Db\Decorator\CollectionAbstract;
use Vegas\Db\Decorator\Helper\RepositoryTrait;

class Fake extends CollectionAbstract
{
Expand All @@ -23,16 +24,21 @@ public function getSource()
}
}

abstract class FakeRepository extends Fake
{
use RepositoryTrait;
}

class CollectionAbstractTest extends \PHPUnit_Framework_TestCase
{

public function testCreateDocument()
{
$data = array(
$data = [
'title' => 'Title test',
'content' => 'Content test',
'category_id' => new \MongoId()
);
];

$fake = new Fake();
$this->assertInstanceOf('\Vegas\Db\Decorator\CollectionAbstract', $fake);
Expand All @@ -52,15 +58,18 @@ public function testCreateDocument()
$this->assertTrue($fake->save());
}

/**
* @depends testCreateDocument
*/
public function testUpdateDocument()
{
$fake = Fake::findFirst();
$fake = FakeRepository::findFirst();
$fake->title = 'New title';

$this->assertTrue($fake->save());
$this->assertInstanceOf('MongoInt32', $fake->updated_at);

$fake = Fake::findFirst(array(array('_id' => $fake->getId())));
$fake = FakeRepository::findById($fake->getId());
$this->assertEquals('New title', $fake->title);
}
}
24 changes: 18 additions & 6 deletions tests/Db/Decorator/ModelAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Phalcon\DI;
use Phalcon\Utils\Slug;
use Vegas\Db\Decorator\Helper\RepositoryTrait;
use Vegas\Db\Decorator\ModelAbstract;

class FakeModel extends ModelAbstract
Expand All @@ -36,6 +37,11 @@ public function getSource()
}
}

abstract class FakeModelRepository extends FakeModel
{
use RepositoryTrait;
}

class ModelAbstractTest extends \PHPUnit_Framework_TestCase
{

Expand All @@ -62,11 +68,11 @@ public static function tearDownAfterClass()

public function testShouldCreateRecord()
{
$data = array(
$data = [
'title' => 'Title test',
'content' => 'Content test',
'category_id' => rand(1000, 9999)
);
];

$fake = new FakeModel();
$this->assertInstanceOf('\Vegas\Db\Decorator\ModelAbstract', $fake);
Expand All @@ -86,25 +92,31 @@ public function testShouldCreateRecord()
$this->assertTrue($fake->save());
}

/**
* @depends testShouldCreateRecord
*/
public function testShouldFindRecordByItsId()
{
$fake = FakeModel::findFirst();
$fake = FakeModelRepository::findFirst();

$this->assertSame(
$fake->toArray(),
FakeModel::findById($fake->getId())->toArray()
FakeModelRepository::findById($fake->getId())->toArray()
);
}

/**
* @depends testShouldCreateRecord
*/
public function testShouldUpdateRecord()
{
$fake = FakeModel::findFirst();
$fake = FakeModelRepository::findFirst();
$fake->title = 'New title';

$this->assertTrue($fake->save());
$this->assertInternalType('int', $fake->updated_at);

$fake = FakeModel::findFirst();
$fake = FakeModelRepository::findFirst();
$this->assertEquals('New title', $fake->title);
}
}

0 comments on commit c9da3e5

Please sign in to comment.