1414use ScriptFUSION \Porter \Collection \MappedRecords ;
1515use ScriptFUSION \Porter \Collection \PorterRecords ;
1616use ScriptFUSION \Porter \Collection \ProviderRecords ;
17+ use ScriptFUSION \Porter \ImportException ;
1718use ScriptFUSION \Porter \Porter ;
1819use ScriptFUSION \Porter \Provider \Provider ;
20+ use ScriptFUSION \Porter \Provider \Resource \ProviderResource ;
1921use ScriptFUSION \Porter \Provider \StaticDataProvider ;
2022use ScriptFUSION \Porter \ProviderAlreadyRegisteredException ;
2123use ScriptFUSION \Porter \ProviderNotFoundException ;
@@ -33,9 +35,12 @@ final class PorterTest extends \PHPUnit_Framework_TestCase
3335 /** @var Provider|MockInterface */
3436 private $ provider ;
3537
36- /** @var Resource */
38+ /** @var ProviderResource */
3739 private $ resource ;
3840
41+ /** @var ImportSpecification */
42+ private $ specification ;
43+
3944 protected function setUp ()
4045 {
4146 $ this ->porter = (new Porter )->registerProvider (
@@ -48,6 +53,7 @@ protected function setUp()
4853 );
4954
5055 $ this ->resource = MockFactory::mockResource ($ this ->provider );
56+ $ this ->specification = new ImportSpecification ($ this ->resource );
5157 }
5258
5359 public function testGetProvider ()
@@ -85,14 +91,14 @@ public function testGetInvalidProvider()
8591 {
8692 $ this ->setExpectedException (ProviderNotFoundException::class);
8793
88- ( new Porter ) ->getProvider ('foo ' );
94+ $ this -> porter ->getProvider ('foo ' );
8995 }
9096
9197 public function testGetInvalidTag ()
9298 {
9399 $ this ->setExpectedException (ProviderNotFoundException::class);
94100
95- ( new Porter ) ->getProvider (get_class ($ this ->provider ), 'foo ' );
101+ $ this -> porter ->getProvider (get_class ($ this ->provider ), 'foo ' );
96102 }
97103
98104 public function testGetStaticProviderTag ()
@@ -102,26 +108,6 @@ public function testGetStaticProviderTag()
102108 $ this ->porter ->getProvider (StaticDataProvider::class, 'foo ' );
103109 }
104110
105- public function testImportTaggedResource ()
106- {
107- $ this ->porter ->registerProvider (
108- $ provider = \Mockery::mock (Provider::class)
109- ->shouldReceive ('fetch ' )
110- ->andReturn (new \ArrayIterator ([$ output = 'bar ' ]))
111- ->getMock (),
112- $ tag = 'foo '
113- );
114-
115- $ records = $ this ->porter ->import (MockFactory::mockImportSpecification (
116- MockFactory::mockResource ($ provider )
117- ->shouldReceive ('getProviderTag ' )
118- ->andReturn ($ tag )
119- ->getMock ()
120- ));
121-
122- self ::assertSame ($ output , $ records ->current ());
123- }
124-
125111 public function testHasProvider ()
126112 {
127113 self ::assertTrue ($ this ->porter ->hasProvider (get_class ($ this ->provider )));
@@ -131,10 +117,10 @@ public function testHasProvider()
131117
132118 public function testImport ()
133119 {
134- $ records = $ this ->porter ->import ($ specification = new ImportSpecification ( $ this ->resource ) );
120+ $ records = $ this ->porter ->import ($ this ->specification );
135121
136122 self ::assertInstanceOf (PorterRecords::class, $ records );
137- self ::assertNotSame ($ specification , $ records ->getSpecification ());
123+ self ::assertNotSame ($ this -> specification , $ records ->getSpecification ());
138124 self ::assertInstanceOf (ProviderRecords::class, $ records ->getPreviousCollection ());
139125 self ::assertSame ('foo ' , $ records ->current ());
140126 }
@@ -194,12 +180,57 @@ public function testImportAndFilterCountableRecords()
194180 self ::assertNotInstanceOf (\Countable::class, $ records );
195181 }
196182
183+ public function testImportOne ()
184+ {
185+ $ result = $ this ->porter ->importOne ($ this ->specification );
186+
187+ self ::assertSame ('foo ' , $ result );
188+ }
189+
190+ public function testImportOneOfNone ()
191+ {
192+ $ this ->provider ->shouldReceive ('fetch ' )->andReturn (new \EmptyIterator );
193+
194+ $ result = $ this ->porter ->importOne ($ this ->specification );
195+
196+ self ::assertNull ($ result );
197+ }
198+
199+ public function testImportOneOfMany ()
200+ {
201+ $ this ->setExpectedException (ImportException::class);
202+
203+ $ this ->provider ->shouldReceive ('fetch ' )->andReturn (new \ArrayIterator (['foo ' , 'bar ' ]));
204+
205+ $ this ->porter ->importOne ($ this ->specification );
206+ }
207+
208+ public function testImportTaggedResource ()
209+ {
210+ $ this ->porter ->registerProvider (
211+ $ provider = \Mockery::mock (Provider::class)
212+ ->shouldReceive ('fetch ' )
213+ ->andReturn (new \ArrayIterator ([$ output = 'bar ' ]))
214+ ->getMock (),
215+ $ tag = 'foo '
216+ );
217+
218+ $ records = $ this ->porter ->import (MockFactory::mockImportSpecification (
219+ MockFactory::mockResource ($ provider )
220+ ->shouldReceive ('getProviderTag ' )
221+ ->andReturn ($ tag )
222+ ->getMock ()
223+ ));
224+
225+ self ::assertSame ($ output , $ records ->current ());
226+ }
227+
197228 public function testFilter ()
198229 {
199230 $ this ->provider ->shouldReceive ('fetch ' )->andReturn (new \ArrayIterator (range (1 , 10 )));
200231
201232 $ records = $ this ->porter ->import (
202- ( new ImportSpecification ( $ this ->resource ))
233+ $ this ->specification
203234 ->setFilter (function ($ record ) {
204235 return $ record % 2 ;
205236 })
@@ -219,9 +250,7 @@ public function testMap()
219250 ->once ()
220251 ->andReturn (new \ArrayIterator ($ result = ['foo ' => 'bar ' ]))
221252 ->getMock ()
222- )->import (
223- (new ImportSpecification ($ this ->resource ))->setMapping (\Mockery::mock (Mapping::class))
224- );
253+ )->import ($ this ->specification ->setMapping (\Mockery::mock (Mapping::class)));
225254
226255 self ::assertInstanceOf (PorterRecords::class, $ records );
227256 self ::assertInstanceOf (MappedRecords::class, $ records ->getPreviousCollection ());
@@ -246,6 +275,6 @@ public function testCacheUnavailable()
246275 {
247276 $ this ->setExpectedException (CacheUnavailableException::class);
248277
249- $ this ->porter ->import (( new ImportSpecification ( $ this ->resource )) ->setCacheAdvice (CacheAdvice::MUST_CACHE ()));
278+ $ this ->porter ->import ($ this ->specification ->setCacheAdvice (CacheAdvice::MUST_CACHE ()));
250279 }
251280}
0 commit comments