Skip to content

Commit e53ca7c

Browse files
committed
Use factory to create record on collection
1 parent c88d509 commit e53ca7c

File tree

4 files changed

+63
-37
lines changed

4 files changed

+63
-37
lines changed

src/Collection.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ public function __construct(Factory $factory)
1919

2020
public function record($record = null)
2121
{
22-
if (! $record instanceof Record) {
23-
$data = (array) $record;
24-
$record = new Record($data);
25-
}
26-
27-
return $record;
22+
return $this->factory->record($record);
2823
}
2924

3025
public function criteria($criteria = null)

src/Factory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,14 @@ public function collection($collection = null)
1212

1313
return $collection;
1414
}
15+
16+
public function record($record = null)
17+
{
18+
if (! $record instanceof Record) {
19+
$data = (array) $record;
20+
$record = new Record($data);
21+
}
22+
23+
return $record;
24+
}
1525
}

tests/CollectionTest.php

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,33 @@ public function testShouldAcceptAnInstanceOfFactoryOnConstructor()
1515
$this->assertAttributeSame($factory, 'factory', $collection);
1616
}
1717

18-
public function testShouldCreateNewRecord()
18+
public function testShouldUseFactoryToCreateRecords()
1919
{
20-
$collection = new Collection(new Factory());
21-
$record = $collection->record();
22-
23-
$this->assertInstanceOf(__NAMESPACE__ . '\\Record', $record);
24-
}
20+
$data = array('foo' => true);
21+
$record = new Record($data);
2522

26-
public function testShouldCreateNewCriteria()
27-
{
28-
$collection = new Collection(new Factory());
29-
$criteria = $collection->criteria();
30-
31-
$this->assertInstanceOf(__NAMESPACE__ . '\\Criteria', $criteria);
32-
}
33-
34-
public function testShouldCreateNewRecordFromArray()
35-
{
36-
$collection = new Collection(new Factory());
37-
$data = array();
38-
$record = $collection->record($data);
23+
$factory = $this
24+
->getMockBuilder('PHPFluent\\ArrayStorage\\Factory')
25+
->disableOriginalConstructor()
26+
->getMock();
3927

40-
$this->assertInstanceOf(__NAMESPACE__ . '\\Record', $record);
41-
}
28+
$factory
29+
->expects($this->once())
30+
->method('record')
31+
->with($data)
32+
->will($this->returnValue($record));
4233

43-
public function testShouldCreateNewRecordFromStdClass()
44-
{
45-
$collection = new Collection(new Factory());
46-
$data = new \stdClass();
47-
$record = $collection->record($data);
34+
$collection = new Collection($factory);
4835

49-
$this->assertInstanceOf(__NAMESPACE__ . '\\Record', $record);
36+
$this->assertSame($record, $collection->record($data));
5037
}
5138

52-
public function testShouldReturnTheSameInstanceWhenDataIsAlreadyARecordInstance()
39+
public function testShouldCreateNewCriteria()
5340
{
5441
$collection = new Collection(new Factory());
55-
$data = new Record();
56-
$record = $collection->record($data);
42+
$criteria = $collection->criteria();
5743

58-
$this->assertSame($data, $record);
44+
$this->assertInstanceOf(__NAMESPACE__ . '\\Criteria', $criteria);
5945
}
6046

6147
public function testShouldCountRecordsInCollection()

tests/FactoryTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,39 @@ public function testShouldReturnInputIfInputIsAlreadyACollection()
2525

2626
$this->assertSame($collection, $factory->collection($collection));
2727
}
28+
29+
public function testShouldCreateNewRecord()
30+
{
31+
$factory = new Factory();
32+
$record = $factory->record();
33+
34+
$this->assertInstanceOf(__NAMESPACE__ . '\\Record', $record);
35+
}
36+
37+
public function testShouldCreateNewRecordFromArray()
38+
{
39+
$factory = new Factory();
40+
$data = array();
41+
$record = $factory->record($data);
42+
43+
$this->assertInstanceOf(__NAMESPACE__ . '\\Record', $record);
44+
}
45+
46+
public function testShouldCreateNewRecordFromStdClass()
47+
{
48+
$factory = new Factory();
49+
$data = new \stdClass();
50+
$record = $factory->record($data);
51+
52+
$this->assertInstanceOf(__NAMESPACE__ . '\\Record', $record);
53+
}
54+
55+
public function testShouldReturnTheSameInstanceWhenDataIsAlreadyARecordInstance()
56+
{
57+
$factory = new Factory();
58+
$data = new Record();
59+
$record = $factory->record($data);
60+
61+
$this->assertSame($data, $record);
62+
}
2863
}

0 commit comments

Comments
 (0)