Skip to content

Commit 069b2a8

Browse files
committed
Merge branch 'jira-3393' into 'master'
[DEV-3393] Add new methods to fetch metadata. See merge request floss/libliza-php!3
2 parents e683418 + 19412d7 commit 069b2a8

3 files changed

Lines changed: 202 additions & 25 deletions

File tree

src/Document/Document.php

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Document
3737
*
3838
* @var string
3939
*/
40-
private $_id = '';
40+
private $_id = "";
4141

4242
/**
4343
* Document key/value store
@@ -47,11 +47,11 @@ class Document
4747
private $_bucket = null;
4848

4949
/**
50-
* Document metadata
50+
* Document fields
5151
*
5252
* @var array
5353
*/
54-
private $_meta = [];
54+
private $_fields = [];
5555

5656

5757
/**
@@ -61,13 +61,13 @@ class Document
6161
*
6262
* @param string $doc_id document identifier
6363
* @param Bucket $bucket document key/value store
64-
* @param array $meta document metadata
64+
* @param array $fields document fields
6565
*/
66-
public function __construct( $doc_id, Bucket $bucket, array $meta = [] )
66+
public function __construct( $doc_id, Bucket $bucket, array $fields = [] )
6767
{
6868
$this->_id = (string)$doc_id;
6969
$this->_bucket = $bucket;
70-
$this->_meta = $meta;
70+
$this->_fields = $fields;
7171
}
7272

7373

@@ -89,9 +89,71 @@ public function getId()
8989
*/
9090
public function getProgramId()
9191
{
92-
$program_id = &$this->_meta[ 'programId' ];
92+
// This existed before MissingDocumentFieldException and
93+
// was expected to not throw an exception when it does not exist.
94+
try
95+
{
96+
return (string)$this->_getFieldByName( "programId" );
97+
}
98+
catch ( MissingDocumentFieldException $e )
99+
{
100+
return "";
101+
}
102+
}
103+
104+
105+
/**
106+
* Get agent identifier
107+
*
108+
* @return string agent id or empty string if unknown
109+
*/
110+
public function getAgentId()
111+
{
112+
return $this->_getFieldByName( "agentId" );
113+
}
114+
115+
116+
/**
117+
* Get agent entity identifier (i.e. the user id)
118+
*
119+
* @return string agent entity id or empty string if unknown
120+
*/
121+
public function getAgentEntityId()
122+
{
123+
return $this->_getFieldByName( "agentEntityId" );
124+
}
125+
126+
127+
/**
128+
* Get the initial rated date.
129+
*
130+
* @return int A unix timestamp for the initial rating date
131+
*/
132+
public function getInitialRatedDate()
133+
{
134+
return $this->_getFieldByName( "initialRatedDate" );
135+
}
136+
93137

94-
return (string)$program_id;
138+
/**
139+
* Get the start date.
140+
*
141+
* @return int A unix timestamp for the start date
142+
*/
143+
public function getStartDate()
144+
{
145+
return $this->_getFieldByName( "startDate" );
146+
}
147+
148+
149+
/**
150+
* Get agent name
151+
*
152+
* @return string agent name or empty string if unknown
153+
*/
154+
public function getAgentName()
155+
{
156+
return $this->_getFieldByName( "agentName" );
95157
}
96158

97159

@@ -104,4 +166,22 @@ public function getBucket()
104166
{
105167
return $this->_bucket;
106168
}
169+
170+
171+
/**
172+
* Get fields by key name
173+
*
174+
* @param String The name of the key holding the data
175+
* @return string field or empty string if unknown
176+
* @throws MissingDocumentFieldException when the field does not exist
177+
*/
178+
private function _getFieldByName( $name )
179+
{
180+
if ( array_key_exists( $name, $this->_fields ) )
181+
{
182+
return $this->_fields[ $name ];
183+
}
184+
185+
throw new MissingDocumentFieldException( "Missing field data" );
186+
}
107187
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Missin document field data exception
4+
*
5+
* Copyright (C) 2016 LoVullo Associates, Inc.
6+
*
7+
* This file is part of libliza-php.
8+
*
9+
* libliza-php is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
23+
24+
namespace Lovullo\Liza\Document;
25+
26+
27+
/**
28+
* Invalid or incomplete document field data were provided
29+
*
30+
* N.B: This exception should not provide the bad data, as that would leak
31+
* implementation details (and potentially very sensitive data); this is a
32+
* public-facing exception!
33+
*/
34+
class MissingDocumentFieldException
35+
extends \Exception
36+
{
37+
}

test/Document/DocumentTest.php

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@
2424

2525
use \Lovullo\Liza\Document\Document as Sut;
2626

27-
2827
class DocumentTest
2928
extends \PHPUnit_Framework_TestCase
3029
{
3130
protected function getDummyBucket()
3231
{
3332
return $this->getMockBuilder(
34-
'Lovullo\Liza\Bucket\Bucket'
33+
"Lovullo\Liza\Bucket\Bucket"
3534
)
36-
->setMethods( array( 'getDataByName', 'hasData' ) )
35+
->setMethods( array( "getDataByName", "hasData" ) )
3736
->getMock();
3837
}
3938

@@ -46,7 +45,7 @@ protected function createSut( $id, $bucket, $meta = [] )
4645

4746
public function testReturnsDocumentId()
4847
{
49-
$id = '123456';
48+
$id = "123456";
5049

5150
$this->assertEquals(
5251
$id,
@@ -58,45 +57,106 @@ public function testReturnsDocumentId()
5857
public function testIdReturnedAsAString()
5958
{
6059
$this->assertSame(
61-
'1234',
60+
"1234",
6261
$this->createSut( 1234, $this->getDummyBucket() )->getId()
6362
);
6463
}
6564

6665

67-
public function programIdDataProvider()
66+
public function testReturnsBucket()
67+
{
68+
$bucket = $this->getDummyBucket();
69+
70+
$this->assertSame(
71+
$bucket,
72+
$this->createSut( "foo", $bucket )->getBucket()
73+
);
74+
}
75+
76+
77+
public function gettersDataProvider()
6878
{
6979
return [
70-
[ [ 'programId' => 'fooprog' ], 'fooprog' ],
71-
[ [ 'programId' => '' ], '' ],
72-
[ [], '' ],
80+
[ [ "agentId" => "fooagentid" ], "fooagentid" ],
81+
[ [ "agentId" => "" ], "" ],
82+
[ [ "agentName" => "fooagentname" ], "fooagentname" ],
83+
[ [ "agentName" => "" ], "" ],
84+
[ [ "programId" => "fooprog" ], "fooprog" ],
85+
[ [ "programId" => "" ], "" ],
86+
[ [ "agentEntityId" => "fooaei" ], "fooaei" ],
87+
[ [ "agentEntityId" => "" ], "" ],
88+
[ [ "initialRatedDate" => "fooird" ], "fooird" ],
89+
[ [ "initialRatedDate" => "" ], "" ],
90+
[ [ "startDate" => "foosd" ], "foosd" ],
91+
[ [ "startDate" => "" ], "" ],
7392
];
7493
}
7594

7695

7796
/**
78-
* @dataProvider programIdDataProvider
97+
* @dataProvider gettersDataProvider
7998
*/
80-
public function testProgramIdReturnedAsString( array $meta, $expected )
99+
public function testGetters( array $meta, $expected )
81100
{
101+
$methodName = "get" . ucfirst( array_keys( $meta )[0] );
102+
82103
$this->assertEquals(
83104
$expected,
84105
$this->createSut(
85106
1234,
86107
$this->getDummyBucket(),
87108
$meta
88-
)->getProgramId()
109+
)->$methodName()
89110
);
90111
}
91112

92113

93-
public function testReturnsBucket()
114+
public function emptyGettersDataProvider()
94115
{
95-
$bucket = $this->getDummyBucket();
116+
return [
117+
[ "getProgramId" ],
118+
];
119+
}
96120

97-
$this->assertSame(
98-
$bucket,
99-
$this->createSut( 'foo', $bucket )->getBucket()
121+
122+
/**
123+
* @dataProvider emptyGettersDataProvider
124+
*/
125+
public function testEmptyGetters( $methodName )
126+
{
127+
$this->assertEquals(
128+
"",
129+
$this->createSut(
130+
1234,
131+
$this->getDummyBucket(),
132+
[]
133+
)->$methodName()
100134
);
101135
}
136+
137+
138+
public function exceptionGettersDataProvider()
139+
{
140+
return [
141+
[ "getAgentId" ],
142+
[ "getAgentName" ],
143+
[ "getAgentEntityId" ],
144+
[ "getInitialRatedDate" ],
145+
[ "getStartDate" ],
146+
];
147+
}
148+
149+
150+
/**
151+
* @dataProvider exceptionGettersDataProvider
152+
* @expectedException \Lovullo\Liza\Document\MissingDocumentFieldException
153+
*/
154+
public function testExceptionGetters( $methodName )
155+
{
156+
$this->createSut(
157+
1234,
158+
$this->getDummyBucket(),
159+
[]
160+
)->$methodName();
161+
}
102162
}

0 commit comments

Comments
 (0)