update for version 1.0.1

This commit is contained in:
sujitprasad
2015-10-23 14:15:29 +05:30
parent 82b878e93b
commit 3d425dc380
8348 changed files with 10020 additions and 4171 deletions

View File

@@ -0,0 +1,127 @@
<?php
/*
* This file is part of Psy Shell
*
* (c) 2012-2014 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\Presenter;
use Psy\Presenter\ArrayPresenter;
use Psy\Presenter\ObjectPresenter;
use Psy\Presenter\PresenterManager;
use Psy\Presenter\ScalarPresenter;
class ArrayPresenterTest extends \PHPUnit_Framework_TestCase
{
private $presenter;
private $manager;
public function setUp()
{
$this->presenter = new ArrayPresenter();
$this->manager = new PresenterManager();
$this->manager->addPresenter(new ScalarPresenter());
$this->manager->addPresenter(new ObjectPresenter());
$this->manager->addPresenter($this->presenter);
}
/**
* @dataProvider presentData
*/
public function testPresent($array, $expect)
{
$this->assertEquals($expect, self::strip($this->presenter->present($array)));
}
public function presentData()
{
return array(
array(array(), '[]'),
array(array(1), '[<number>1</number>]'),
array(array(2, "string"), '[<number>2</number>,<string>"string"</string>]'),
array(array('a' => 1, 'b' => 2), '[<string>"a"</string>=><number>1</number>,<string>"b"</string>=><number>2</number>]'),
);
}
/**
* @dataProvider presentRefData
*/
public function testPresentRef($array, $expect)
{
$this->assertEquals($expect, $this->presenter->presentRef($array));
}
public function presentRefData()
{
return array(
array(array(), '[]'),
array(array(1), 'Array(<number>1</number>)'),
array(array(1, 2), 'Array(<number>2</number>)'),
array(array(1, 2, 3), 'Array(<number>3</number>)'),
);
}
/**
* @dataProvider presentArrayObjectsData
*/
public function testPresentArrayObjects($arrayObj, $expect, $expectRef)
{
$this->assertEquals($expect, $this->presenter->present($arrayObj));
$this->assertEquals($expectRef, $this->presenter->presentRef($arrayObj));
}
public function presentArrayObjectsData()
{
$obj1 = new \ArrayObject(array(1, "string"));
$hash1 = spl_object_hash($obj1);
$ref1 = '<object>\\<<class>ArrayObject</class> <strong>#' . $hash1 . '</strong>></object>';
$expect1 = <<<EOS
$ref1 [
<number>1</number>,
<string>"string"</string>
]
EOS;
$obj2 = new FakeArrayObject(array('a' => 'AAA', 'b' => 'BBB'));
$hash2 = spl_object_hash($obj2);
$ref2 = '<object>\\<<class>Psy\\Test\\Presenter\\FakeArrayObject</class> <strong>#' . $hash2 . '</strong>></object>';
$expect2 = <<<EOS
$ref2 [
<string>"a"</string> => <string>"AAA"</string>,
<string>"b"</string> => <string>"BBB"</string>
]
EOS;
return array(
array($obj1, $expect1, $ref1),
array($obj2, $expect2, $ref2),
);
}
public function testPresentsRecursively()
{
$obj = new \StdClass();
$array = array(1, $obj, "a");
$hash = spl_object_hash($obj);
$expected = <<<EOS
[
<number>1</number>,
<object>\<<class>stdClass</class> <strong>#$hash</strong>></object> {},
<string>"a"</string>
]
EOS;
$this->assertEquals($expected, $this->presenter->present($array));
}
private static function strip($text)
{
return preg_replace('/\\s/', '', $text);
}
}

View File

@@ -0,0 +1,88 @@
<?php
/*
* This file is part of Psy Shell
*
* (c) 2012-2014 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\Presenter;
use Psy\Presenter\ClosurePresenter;
use Psy\Presenter\ObjectPresenter;
use Psy\Presenter\PresenterManager;
use Psy\Presenter\ScalarPresenter;
class ClosurePresenterTest extends \PHPUnit_Framework_TestCase
{
private $presenter;
private $manager;
public function setUp()
{
$this->presenter = new ClosurePresenter();
$this->manager = new PresenterManager();
$this->manager->addPresenter(new ScalarPresenter());
$this->manager->addPresenter(new ObjectPresenter());
$this->manager->addPresenter($this->presenter);
}
/**
* @dataProvider presentData
*/
public function testPresent($closure, $expect)
{
$this->assertEquals($expect, $this->presenter->present($closure));
}
/**
* @dataProvider presentData
*/
public function testPresentRef($closure, $expect)
{
$this->assertEquals($expect, $this->presenter->presentRef($closure));
}
public function presentData()
{
$null = null;
$eol = version_compare(PHP_VERSION, '5.4.3', '>=') ? '<const>PHP_EOL</const>' : '<string>"\n"</string>';
return array(
array(
function () {
},
'<keyword>function</keyword> () { <comment>...</comment> }',
),
array(
function ($foo) {
},
'<keyword>function</keyword> ($<strong>foo</strong>) { <comment>...</comment> }',
),
array(
function ($foo, $bar = null) {
},
'<keyword>function</keyword> ($<strong>foo</strong>, $<strong>bar</strong> = <bool>null</bool>) { <comment>...</comment> }',
),
array(
function ($foo = "bar") {
},
'<keyword>function</keyword> ($<strong>foo</strong> = <string>"bar"</string>) { <comment>...</comment> }',
),
array(
function ($foo = \PHP_EOL) {
},
'<keyword>function</keyword> ($<strong>foo</strong> = ' . $eol . ') { <comment>...</comment> }',
),
array(
function ($foo) use ($eol, $null) {
},
'<keyword>function</keyword> ($<strong>foo</strong>) use ($<strong>eol</strong>, $<strong>null</strong>) { <comment>...</comment> }',
),
);
}
}

View File

@@ -0,0 +1,17 @@
<?php
/*
* This file is part of Psy Shell
*
* (c) 2012-2014 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\Presenter;
class FakeArrayObject extends \ArrayObject
{
// this space intentionally left blank.
}

View File

@@ -0,0 +1,21 @@
<?php
/*
* This file is part of Psy Shell
*
* (c) 2012-2014 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\Presenter\Fixtures;
class SimpleClass
{
public $hello = 'Hello world!';
protected $foo = 'bar';
private $secret = 42;
}

View File

@@ -0,0 +1,123 @@
<?php
/*
* This file is part of Psy Shell
*
* (c) 2012-2014 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\Presenter;
use Psy\Presenter\ArrayPresenter;
use Psy\Presenter\ObjectPresenter;
use Psy\Presenter\Presenter;
use Psy\Presenter\PresenterManager;
use Psy\Presenter\ScalarPresenter;
use Psy\Test\Presenter\Fixtures\SimpleClass;
class ObjectPresenterTest extends \PHPUnit_Framework_TestCase
{
private $presenter;
private $manager;
public function setUp()
{
$this->presenter = new ObjectPresenter();
$this->manager = new PresenterManager();
$this->manager->addPresenter(new ScalarPresenter());
$this->manager->addPresenter(new ArrayPresenter());
$this->manager->addPresenter($this->presenter);
}
public function testPresentEmptyObject()
{
$empty = new \StdClass();
$this->assertEquals(
$this->presenter->presentRef($empty) . ' {}',
$this->presenter->present($empty)
);
}
public function testPresentWithDepth()
{
$obj = new \StdClass();
$obj->name = 'std';
$obj->type = 'class';
$obj->tags = array('stuff', 'junk');
$obj->child = new \StdClass();
$obj->child->name = 'std, jr';
$hash = spl_object_hash($obj);
$childHash = spl_object_hash($obj->child);
$expected = <<<EOS
<object>\<<class>stdClass</class> <strong>#$hash</strong>></object> {
name: <string>"std"</string>,
type: <string>"class"</string>,
tags: Array(<number>2</number>),
child: <object>\<<class>stdClass</class> <strong>#$childHash</strong>></object>
}
EOS;
$this->assertStringMatchesFormat($expected, $this->presenter->present($obj, 1));
}
public function testPresentWithoutDepth()
{
$obj = new \StdClass();
$obj->name = 'std';
$obj->type = 'class';
$obj->tags = array('stuff', 'junk');
$obj->child = new \StdClass();
$obj->child->name = 'std, jr';
$hash = spl_object_hash($obj);
$childHash = spl_object_hash($obj->child);
$expected = <<<EOS
<object>\<<class>stdClass</class> <strong>#$hash</strong>></object> {
name: <string>"std"</string>,
type: <string>"class"</string>,
tags: [
<string>"stuff"</string>,
<string>"junk"</string>
],
child: <object>\<<class>stdClass</class> <strong>#$childHash</strong>></object> {
name: <string>"std, jr"</string>
}
}
EOS;
$this->assertStringMatchesFormat($expected, $this->presenter->present($obj));
}
public function testPresentRef()
{
$obj = new \StdClass();
$formatted = $this->presenter->presentRef($obj);
$this->assertStringMatchesFormat('<object>\<<class>stdClass</class> <strong>#%s</strong>></object>', $formatted);
$this->assertContains(spl_object_hash($obj), $formatted);
}
public function testPresentVerbose()
{
$obj = new SimpleClass();
$hash = spl_object_hash($obj);
$expected = <<<EOS
<object>\<<class>Psy\Test\Presenter\Fixtures\SimpleClass</class> <strong>#$hash</strong>></object> {
hello: <string>"Hello world!"</string>,
<protected>foo</protected>: <string>"bar"</string>,
<private>secret</private>: <number>42</number>
}
EOS;
$this->assertStringMatchesFormat($expected, $this->presenter->present($obj, null, Presenter::VERBOSE));
}
}

View File

@@ -0,0 +1,31 @@
<?php
/*
* This file is part of Psy Shell
*
* (c) 2012-2014 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\Presenter;
use Psy\Presenter\ResourcePresenter;
class ResourcePresenterTest extends \PHPUnit_Framework_TestCase
{
private $presenter;
public function setUp()
{
$this->presenter = new ResourcePresenter();
}
public function testPresent()
{
$resource = fopen('php://stdin', 'r');
$this->assertStringMatchesFormat('<resource>\<STDIO stream <strong>resource #%d</strong>></resource>', $this->presenter->present($resource));
fclose($resource);
}
}

View File

@@ -0,0 +1,53 @@
<?php
/*
* This file is part of Psy Shell
*
* (c) 2012-2014 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\Presenter;
use Psy\Presenter\ScalarPresenter;
class ScalarPresenterTest extends \PHPUnit_Framework_TestCase
{
private $presenter;
public function setUp()
{
$this->presenter = new ScalarPresenter();
}
/**
* @dataProvider scalarData
*/
public function testPresent($value, $expect)
{
$this->assertEquals($expect, $this->presenter->present($value));
}
public function scalarData()
{
return array(
array(1, '<number>1</number>'),
array(1.0, '<number>1.0</number>'),
array(1.5, '<number>1.5</number>'),
array('2', '<string>"2"</string>'),
array('2.5', '<string>"2.5"</string>'),
array('alpha', '<string>"alpha"</string>'),
array("a\nb", '<string>"a\\nb"</string>'),
array(true, '<bool>true</bool>'),
array(false, '<bool>false</bool>'),
array(null, '<bool>null</bool>'),
array(NAN, '<number>NAN</number>'), // heh.
array(acos(8), '<number>NAN</number>'),
array(INF, '<number>INF</number>'),
array(-INF, '<number>-INF</number>'),
array(log(0), '<number>-INF</number>'),
);
}
}