Skip to content

Commit ef44931

Browse files
committed
Added methods to test if resource is local or remote; fixed BaseDatapackage->save to not attempt to save remote resources;
Added a test to verify that http resources work as expected. Made DatapackageTest to use php-generated temporary files rather than hard-coded names Added a few docblocks
1 parent f7b20c9 commit ef44931

4 files changed

Lines changed: 87 additions & 9 deletions

File tree

src/Datapackages/BaseDatapackage.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ public function save($zip_filename)
191191
'datapackage.json' => $base.'datapackage.json',
192192
];
193193
$ri = 0;
194-
foreach ($packageCopy->resources() as $resource) {
194+
foreach ($packageCopy as $resource) {
195+
if ($resource->isRemote()) continue;
196+
195197
$resourceFiles = [];
196198
$fileNames = $resource->save($base.'resource-'.$ri);
197199
foreach ($fileNames as $fileName) {
@@ -217,6 +219,11 @@ public function save($zip_filename)
217219
}
218220
}
219221

222+
/**
223+
* Make a new Datapackage object that is a copy of the current Datapackage. Bypasses validation.
224+
* @return $this
225+
* @throws DatapackageValidationFailedException
226+
*/
220227
protected function copy()
221228
{
222229
return new static($this->descriptor, $this->basePath, true);

src/Resources/BaseResource.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public function read($readOptions = null)
7575
return $rows;
7676
}
7777

78+
/**
79+
* Loads $this->dataStreams based on $this->path() and $this->data
80+
* @return BaseDataStream[]|null
81+
*/
7882
public function dataStreams()
7983
{
8084
if (is_null($this->dataStreams)) {
@@ -121,6 +125,25 @@ public function path()
121125
}
122126
}
123127

128+
/**
129+
* If the resource's $path is local (non-http)
130+
* @return bool
131+
*/
132+
public function isLocal()
133+
{
134+
return !$this->isRemote();
135+
}
136+
137+
/**
138+
* If the resource's $path is remote (http)
139+
* @return bool
140+
*/
141+
public function isRemote()
142+
{
143+
$path = $this->path();
144+
return Utils::isHttpSource(is_array($path) && count($path) > 0 ? $path[0] : $path);
145+
}
146+
124147
public function data()
125148
{
126149
return isset($this->descriptor()->data) ? $this->descriptor()->data : null;

tests/DatapackageTest.php

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = [];
@@ -509,8 +540,11 @@ public function testCreateEditDatapackageDescriptor()
509540

510541
public function testSaveAndLoadZip()
511542
{
543+
//generate a csv file
544+
$csv_filepath = tempnam(sys_get_temp_dir(),'example-csv');
545+
512546
//create example csv
513-
file_put_contents('/tmp/example.csv', "name,email\nJohn Doe,john@example.com");
547+
file_put_contents($csv_filepath, "name,email\nJohn Doe,john@example.com");
514548

515549
//create a new datapackage object
516550
$package = Package::create(['name' => 'csv-example','profile' => 'tabular-data-package']);
@@ -519,25 +553,26 @@ public function testSaveAndLoadZip()
519553
$package->addResource('example.csv', [
520554
"profile" => "tabular-data-resource",
521555
"schema" => ["fields" => [["name" => "name", "type" => "string"],["name" => "email", "type" => "string"]]],
522-
"path" => '/tmp/example.csv'
556+
"path" => $csv_filepath
523557
]);
524558

525559
//save the datapackage
526-
if (is_file('datapackage.zip')) {
527-
unlink('datapackage.zip');
560+
$filename = tempnam(sys_get_temp_dir(), 'datapackage-php-tests-').'.zip';
561+
if (is_file($filename)) {
562+
unlink($filename);
528563
}
529-
$package->save("datapackage.zip");
564+
$package->save($filename);
530565

531566
//delete example csv
532-
unlink('/tmp/example.csv');
567+
unlink($csv_filepath);
533568

534569
//load the new package
535-
$package2 = Package::load('datapackage.zip');
570+
$package2 = Package::load($filename);
536571

537572
//assert you get expected content back out
538573
$this->assertEquals([['name' => 'John Doe', 'email' => 'john@example.com']], $package2->resource('example.csv')->read());
539574

540-
unlink('datapackage.zip');
575+
unlink($filename);
541576
}
542577

543578
public function testLoadDatapackageZip()

tests/Mocks/MockDefaultResource.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,17 @@ protected function validateResource()
4141
{
4242
return MockResourceValidator::validate($this->descriptor(), $this->basePath);
4343
}
44+
45+
46+
/**
47+
* @inheritDoc
48+
* @return bool
49+
*/
50+
public function isRemote()
51+
{
52+
$path = $this->path();
53+
$path_to_check = is_array($path) && count($path) > 0 ? $path[0] : $path;
54+
return strpos($path_to_check, 'mock-http://') === 0 || parent::isRemote();
55+
}
56+
4457
}

0 commit comments

Comments
 (0)