forked from jsonrainbow/json-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more unit tests (jsonrainbow#366)
* Add test coverage for coercion API * Complete test coverage for SchemaStorage * Add test coverage for ObjectIterator * Add exception test for JsonPointer * MabeEnum\Enum appears to use singletons - add testing const * Don't check this line for coverage mbstring is on all test platforms, so this line will never be reached. * Add test for TypeConstraint::validateTypeNameWording() * Add test for exception on TypeConstraint::validateType() * PHPunit doesn't like an explanation with its @codeCoverageIgnore... * Add various tests for UriRetriever * Add tests for FileGetContents * Add tests for JsonSchema\Uri\Retrievers\Curl * Add missing bad-syntax test file * Restrict ignore to the exception line only * Fix exception scope
- Loading branch information
Showing
12 changed files
with
362 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the JsonSchema package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace JsonSchema\Tests\Iterators; | ||
|
||
use JsonSchema\Iterator\ObjectIterator; | ||
|
||
class ObjectIteratorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $testObject; | ||
|
||
public function setUp() | ||
{ | ||
$this->testObject = (object) array( | ||
'subOne' => (object) array( | ||
'propertyOne' => 'valueOne', | ||
'propertyTwo' => 'valueTwo', | ||
'propertyThree' => 'valueThree' | ||
), | ||
'subTwo' => (object) array( | ||
'propertyFour' => 'valueFour', | ||
'subThree' => (object) array( | ||
'propertyFive' => 'valueFive', | ||
'propertySix' => 'valueSix' | ||
) | ||
), | ||
'propertySeven' => 'valueSeven' | ||
); | ||
} | ||
|
||
public function testCreate() | ||
{ | ||
$i = new ObjectIterator($this->testObject); | ||
|
||
$this->assertInstanceOf('\JsonSchema\Iterator\ObjectIterator', $i); | ||
} | ||
|
||
public function testInitialState() | ||
{ | ||
$i = new ObjectIterator($this->testObject); | ||
|
||
$this->assertEquals($this->testObject, $i->current()); | ||
} | ||
|
||
public function testCount() | ||
{ | ||
$i = new ObjectIterator($this->testObject); | ||
|
||
$this->assertEquals(4, $i->count()); | ||
} | ||
|
||
public function testKey() | ||
{ | ||
$i = new ObjectIterator($this->testObject); | ||
|
||
while ($i->key() != 2) { | ||
$i->next(); | ||
} | ||
|
||
$this->assertEquals($this->testObject->subTwo->subThree, $i->current()); | ||
} | ||
|
||
public function testAlwaysObjects() | ||
{ | ||
$i= new ObjectIterator($this->testObject); | ||
|
||
foreach ($i as $item) { | ||
$this->assertInstanceOf('\StdClass', $item); | ||
} | ||
} | ||
|
||
public function testReachesAllProperties() | ||
{ | ||
$i = new ObjectIterator($this->testObject); | ||
|
||
$count = 0; | ||
foreach ($i as $item) { | ||
$count += count(get_object_vars($item)); | ||
} | ||
|
||
$this->assertEquals(10, $count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace JsonSchema\Tests\Uri\Retrievers | ||
{ | ||
use JsonSchema\Uri\Retrievers\Curl; | ||
|
||
class CurlTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testRetrieveFile() | ||
{ | ||
$c = new Curl(); | ||
$c->retrieve(realpath(__DIR__ . '/../../fixtures/foobar.json')); | ||
} | ||
|
||
public function testRetrieveNonexistantFile() | ||
{ | ||
$c = new Curl(); | ||
|
||
$this->setExpectedException( | ||
'\JsonSchema\Exception\ResourceNotFoundException', | ||
'JSON schema not found' | ||
); | ||
$c->retrieve(__DIR__ . '/notARealFile'); | ||
} | ||
|
||
public function testNoContentType() | ||
{ | ||
$c = new Curl(); | ||
$c->retrieve(realpath(__DIR__ . '/../../fixtures') . '/foobar-noheader.json'); | ||
} | ||
} | ||
} | ||
|
||
namespace JsonSchema\Uri\Retrievers | ||
{ | ||
function curl_exec($curl) | ||
{ | ||
$uri = curl_getinfo($curl, \CURLINFO_EFFECTIVE_URL); | ||
|
||
if ($uri === realpath(__DIR__ . '/../../fixtures/foobar.json')) { | ||
// return file with headers | ||
$headers = implode("\n", array( | ||
'Content-Type: application/json' | ||
)); | ||
|
||
return sprintf("%s\r\n\r\n%s", $headers, file_get_contents($uri)); | ||
} elseif ($uri === realpath(__DIR__ . '/../../fixtures') . '/foobar-noheader.json') { | ||
// return file without headers | ||
$uri = realpath(__DIR__ . '/../../fixtures/foobar.json'); | ||
|
||
return "\r\n\r\n" . file_get_contents($uri); | ||
} | ||
|
||
// fallback to real curl_exec | ||
return \curl_exec($curl); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,74 @@ | ||
<?php | ||
|
||
namespace JsonSchema\Tests\Uri\Retrievers; | ||
|
||
use JsonSchema\Uri\Retrievers\FileGetContents; | ||
|
||
/** | ||
* @group FileGetContents | ||
*/ | ||
class FileGetContentsTest extends \PHPUnit_Framework_TestCase | ||
namespace JsonSchema\Tests\Uri\Retrievers | ||
{ | ||
use JsonSchema\Uri\Retrievers\FileGetContents; | ||
|
||
/** | ||
* @expectedException \JsonSchema\Exception\ResourceNotFoundException | ||
* @group FileGetContents | ||
*/ | ||
public function testFetchMissingFile() | ||
class FileGetContentsTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
$res = new FileGetContents(); | ||
$res->retrieve(__DIR__ . '/Fixture/missing.json'); | ||
/** | ||
* @expectedException \JsonSchema\Exception\ResourceNotFoundException | ||
*/ | ||
public function testFetchMissingFile() | ||
{ | ||
$res = new FileGetContents(); | ||
$res->retrieve(__DIR__ . '/Fixture/missing.json'); | ||
} | ||
|
||
public function testFetchFile() | ||
{ | ||
$res = new FileGetContents(); | ||
$result = $res->retrieve(__DIR__ . '/../Fixture/child.json'); | ||
$this->assertNotEmpty($result); | ||
} | ||
|
||
public function testFalseReturn() | ||
{ | ||
$res = new FileGetContents(); | ||
|
||
$this->setExpectedException( | ||
'\JsonSchema\Exception\ResourceNotFoundException', | ||
'JSON schema not found at http://example.com/false' | ||
); | ||
$res->retrieve('http://example.com/false'); | ||
} | ||
|
||
public function testFetchDirectory() | ||
{ | ||
$res = new FileGetContents(); | ||
|
||
$this->setExpectedException( | ||
'\JsonSchema\Exception\ResourceNotFoundException', | ||
'JSON schema not found at file:///this/is/a/directory/' | ||
); | ||
$res->retrieve('file:///this/is/a/directory/'); | ||
} | ||
|
||
public function testContentType() | ||
{ | ||
$res = new FileGetContents(); | ||
|
||
$reflector = new \ReflectionObject($res); | ||
$fetchContentType = $reflector->getMethod('fetchContentType'); | ||
$fetchContentType->setAccessible(true); | ||
|
||
$this->assertTrue($fetchContentType->invoke($res, array('Content-Type: application/json'))); | ||
$this->assertFalse($fetchContentType->invoke($res, array('X-Some-Header: whateverValue'))); | ||
} | ||
} | ||
} | ||
|
||
public function testFetchFile() | ||
namespace JsonSchema\Uri\Retrievers | ||
{ | ||
function file_get_contents($uri) | ||
{ | ||
$res = new FileGetContents(); | ||
$result = $res->retrieve(__DIR__ . '/../Fixture/child.json'); | ||
$this->assertNotEmpty($result); | ||
switch ($uri) { | ||
case 'http://example.com/false': return false; | ||
case 'file:///this/is/a/directory/': return ''; | ||
default: return \file_get_contents($uri); | ||
} | ||
} | ||
} |
Oops, something went wrong.