Skip to content

Commit 02c433e

Browse files
committed
Add option to configure collection on construction
1 parent e7dabbf commit 02c433e

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

src/Collection.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ abstract class Collection implements CollectionInterface
1313
{
1414
use EtagInterfaceImplementation;
1515

16+
/**
17+
* Construct the collection instance
18+
*/
19+
public function __construct()
20+
{
21+
$this->configure();
22+
}
23+
24+
/**
25+
* Pre-configure the collection when it is created
26+
*/
27+
protected function configure()
28+
{
29+
}
30+
1631
/**
1732
* @var string
1833
*/

src/Collection/Type.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function __construct(ConnectionInterface &$connection, PoolInterface &$po
3333
{
3434
$this->connection = $connection;
3535
$this->pool = $pool;
36+
37+
parent::__construct();
3638
}
3739

3840
/**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace ActiveCollab\DatabaseObject\Test\Fixtures\Writers;
4+
5+
use ActiveCollab\DatabaseObject\Collection\Type;
6+
7+
/**
8+
* @package ActiveCollab\DatabaseObject\Test\Fixtures\Writers
9+
*/
10+
class PreconfiguredCollection extends Type
11+
{
12+
/**
13+
* Return type that this collection manages
14+
*
15+
* @return string
16+
*/
17+
public function getType()
18+
{
19+
return Writer::class;
20+
}
21+
22+
/**
23+
* Configure the collection when it is created
24+
*/
25+
protected function configure()
26+
{
27+
$this->where('`name` LIKE ?', 'A%')->orderBy('`name`');
28+
}
29+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace ActiveCollab\DatabaseObject\Test;
4+
5+
use ActiveCollab\DatabaseObject\Test\Base\WritersTypeTestCase;
6+
use ActiveCollab\DatabaseObject\Test\Fixtures\Writers\PreconfiguredCollection;
7+
use ActiveCollab\DatabaseConnection\Result\ResultInterface;
8+
9+
/**
10+
* @package ActiveCollab\DatabaseObject\Test
11+
*/
12+
class TypePreconfiguredCollectionTest extends WritersTypeTestCase
13+
{
14+
/**
15+
* Test if configure() method is called when collection is constructed
16+
*/
17+
public function testCollectionIsPreconfigured()
18+
{
19+
$collection = new PreconfiguredCollection($this->connection, $this->pool);
20+
21+
$this->assertEquals("`name` LIKE 'A%'", $collection->getConditions());
22+
$this->assertEquals('`name`', $collection->getOrderBy());
23+
}
24+
25+
/**
26+
* Test if pre-configured collection works as expected
27+
*/
28+
public function testExecutePreconfiguredCollection()
29+
{
30+
$writers = (new PreconfiguredCollection($this->connection, $this->pool))->execute();
31+
32+
$this->assertInstanceOf(ResultInterface::class, $writers);
33+
$this->assertCount(1, $writers);
34+
$this->assertEquals('Alexander Pushkin', $writers[0]->getName());
35+
}
36+
}

0 commit comments

Comments
 (0)