@@ -141,6 +141,37 @@ public function testHttpSource()
141141 );
142142 }
143143
144+ public function testHttpSourceSaveAndLoad ()
145+ {
146+ $ package = Mocks \MockFactory::datapackage ('mock-http://simple_valid_datapackage_mock_http_data.json ' );
147+
148+ $ filename = tempnam (sys_get_temp_dir (), 'datapackage-php-tests- ' ).'.zip ' ;
149+ //save the datapackage
150+ if (is_file ($ filename )) {
151+ unlink ($ filename );
152+ }
153+ $ package ->save ($ filename );
154+
155+ //load the new package
156+ $ package2 = Mocks \MockFactory::datapackage ($ filename );
157+
158+ $ this ->assertDatapackage (
159+ (object ) [
160+ 'name ' => 'datapackage-name ' ,
161+ 'resources ' => [
162+ (object ) [
163+ 'name ' => 'resource-name ' ,
164+ 'path ' => ['mock-http://foo.txt ' , 'mock-http://foo.txt ' ],
165+ ],
166+ ],
167+ ],
168+ ['resource-name ' => ['foo ' , 'foo ' ]],
169+ $ package2
170+ );
171+
172+ unlink ($ filename );
173+ }
174+
144175 public function testMultiDataDatapackage ()
145176 {
146177 $ out = [];
@@ -476,6 +507,29 @@ public function testCreateEditDatapackageDescriptor()
476507 $ zip ->close ();
477508 unlink ($ filename );
478509 $ tempdir = $ tempdir .DIRECTORY_SEPARATOR ;
510+
511+ //after saving to disk, the paths are updated
512+ $ expectedDatapackageDescriptor = (object ) [
513+ 'name ' => 'my-datapackage-name ' ,
514+ 'resources ' => [
515+ (object ) [
516+ 'name ' => 'my-default-resource ' ,
517+ 'path ' => ["resource-0-data-0 " , "resource-0-data-1 " ],
518+ ],
519+ (object ) [
520+ 'name ' => 'my-renamed-tabular-resource ' ,
521+ 'path ' => "resource-1.csv " ,
522+ 'profile ' => 'tabular-data-resource ' ,
523+ 'schema ' => (object ) [
524+ 'fields ' => [
525+ (object ) ['name ' => 'id ' , 'type ' => 'integer ' ],
526+ (object ) ['name ' => 'name ' , 'type ' => 'string ' ],
527+ ],
528+ ],
529+ ],
530+ ],
531+ ];
532+
479533 $ this ->assertEquals ($ expectedDatapackageDescriptor , json_decode (file_get_contents ($ tempdir .'datapackage.json ' )));
480534 $ this ->assertEquals ('foo ' , file_get_contents ($ tempdir .'resource-0-data-0 ' ));
481535 $ this ->assertEquals ("testing 改善 \n" , file_get_contents ($ tempdir .'resource-0-data-1 ' ));
@@ -484,6 +538,43 @@ public function testCreateEditDatapackageDescriptor()
484538 Utils::removeDir ($ tempdir );
485539 }
486540
541+ public function testSaveAndLoadZip ()
542+ {
543+ //generate a csv file
544+ $ csv_filepath = tempnam (sys_get_temp_dir (),'example-csv ' );
545+
546+ //create example csv
547+ file_put_contents ($ csv_filepath , "name,email \nJohn Doe,john@example.com " );
548+
549+ //create a new datapackage object
550+ $ package = Package::create (['name ' => 'csv-example ' ,'profile ' => 'tabular-data-package ' ]);
551+
552+ //add a csv file
553+ $ package ->addResource ('example.csv ' , [
554+ "profile " => "tabular-data-resource " ,
555+ "schema " => ["fields " => [["name " => "name " , "type " => "string " ],["name " => "email " , "type " => "string " ]]],
556+ "path " => $ csv_filepath
557+ ]);
558+
559+ //save the datapackage
560+ $ filename = tempnam (sys_get_temp_dir (), 'datapackage-php-tests- ' ).'.zip ' ;
561+ if (is_file ($ filename )) {
562+ unlink ($ filename );
563+ }
564+ $ package ->save ($ filename );
565+
566+ //delete example csv
567+ unlink ($ csv_filepath );
568+
569+ //load the new package
570+ $ package2 = Package::load ($ filename );
571+
572+ //assert you get expected content back out
573+ $ this ->assertEquals ([['name ' => 'John Doe ' , 'email ' => 'john@example.com ' ]], $ package2 ->resource ('example.csv ' )->read ());
574+
575+ unlink ($ filename );
576+ }
577+
487578 public function testLoadDatapackageZip ()
488579 {
489580 $ package = Package::load (dirname (__FILE__ ).'/fixtures/datapackage_zip.zip ' );
@@ -626,21 +717,21 @@ public function testCsvDialect()
626717 'CommitteeTypeDesc ' => 'ועדה משותפת ' ,
627718 'Email ' => null ,
628719 'StartDate ' => Carbon::__set_state (array (
629- 'date ' => '2004-08-12 00:00:00.000000 ' ,
630- 'timezone_type ' => 3 ,
631- 'timezone ' => 'UTC ' ,
632- )),
720+ 'date ' => '2004-08-12 00:00:00.000000 ' ,
721+ 'timezone_type ' => 3 ,
722+ 'timezone ' => 'UTC ' ,
723+ )),
633724 'FinishDate ' => null ,
634725 'AdditionalTypeID ' => null ,
635726 'AdditionalTypeDesc ' => null ,
636727 'ParentCommitteeID ' => null ,
637728 'CommitteeParentName ' => null ,
638729 'IsCurrent ' => true ,
639730 'LastUpdatedDate ' => Carbon::__set_state (array (
640- 'date ' => '2015-03-20 12:02:57.000000 ' ,
641- 'timezone_type ' => 3 ,
642- 'timezone ' => 'UTC ' ,
643- )),
731+ 'date ' => '2015-03-20 12:02:57.000000 ' ,
732+ 'timezone_type ' => 3 ,
733+ 'timezone ' => 'UTC ' ,
734+ )),
644735 ), $ row );
645736 }
646737 ++$ rowNum ;
0 commit comments