只是好奇是否有一种方法可以为netbeans提供常规变量的类型提示,以便intellisense选择它.我知道你可以为类属性,函数参数,返回类型等做这些,但我无法弄清楚如何为常规变量做到这一点.在你有一个可以返回不同对象类型的方法(比如服务定位器)的情况下,它会非常有用.
像这样的东西:
/**
* @var Some_Service $someService
*/
$someService = ServiceLocator::locate('someService');
Run Code Online (Sandbox Code Playgroud)
在之后使用$ someService的情况下,netbeans将提供Some_Service类中定义的所有可用方法.
我正在寻找测试以下静态方法的最佳方法(特别是使用Doctrine模型):
class Model_User extends Doctrine_Record
{
public static function create($userData)
{
$newUser = new self();
$newUser->fromArray($userData);
$newUser->save();
}
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我会使用模拟对象来确保调用"fromArray"(使用提供的用户数据)和"save",但这是不可能的,因为该方法是静态的.
有什么建议?
我遇到了PHPUnit模拟对象的一个奇怪问题.我有一个应该被调用两次的方法,所以我使用的是"at"匹配器.这是第一次调用该方法,但由于某种原因,第二次调用它时,我得到"模拟方法不存在".我之前使用过"at"匹配器并且从未遇到过这种情况.
我的代码看起来像:
class MyTest extends PHPUnit_Framework_TestCase
{
...
public function testThis()
{
$mock = $this->getMock('MyClass', array('exists', 'another_method', '...'));
$mock->expects($this->at(0))
->method('exists')
->with($this->equalTo('foo'))
->will($this->returnValue(true));
$mock->expects($this->at(1))
->method('exists')
->with($this->equalTo('bar'))
->will($this->returnValue(false));
}
...
}
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,我得到:
Expectation failed for method name is equal to <string:exists> when invoked at sequence index 1.
Mocked method does not exist.
Run Code Online (Sandbox Code Playgroud)
如果我删除第二个匹配器,我不会收到错误.
有没有人遇到过这个?
谢谢!
是否可以通过命令行测试运行器指定从配置文件运行哪个测试套件?例如,如果我有以下xml配置:
<phpunit ...>
<testsuites>
<testsuite name="My Test Suite 1">
<directory>./MyTestSuite1/</directory>
</testsuite>
<testsuite name="My Test Suite 2">
<directory>./MyTestSuite2/</directory>
</testsuite>
</testsuites>
...
</phpunit>
Run Code Online (Sandbox Code Playgroud)
我可以只运行"我的测试套件1"吗?
我正在使用d3.js在svg中渲染世界地图(使用https://github.com/johan/world.geo.json/blob/master/countries.geo.json获取这些功能).我将渲染逻辑封装在Backbone View中.当我渲染视图并将其附加到DOM时,虽然在查看生成的HTML时正确生成了SVG标记,但浏览器中没有显示任何内容.当没有封装在Backbone.View中时,这会很好.这是我使用Backbone.view的代码:
/**
* SVG Map view
*/
var MapView = Backbone.View.extend({
tagName: 'svg',
translationOffset: [480, 500],
zoomLevel: 1000,
/**
* Sets up the map projector and svg path generator
*/
initialize: function() {
this.projector = d3.geo.mercator();
this.path = d3.geo.path().projection(this.projector);
this.projector.translate(this.translationOffset);
this.projector.scale(this.zoomLevel);
},
/**
* Renders the map using the supplied features collection
*/
render: function() {
d3.select(this.el)
.selectAll('path')
.data(this.options.featureCollection.features)
.enter().append('path')
.attr('d', this.path);
},
/**
* Updates the zoom level
*/
zoom: function(level) {
this.projector.scale(this.zoomLevel = level);
}, …
Run Code Online (Sandbox Code Playgroud) 我正在为一个类方法编写单元测试,该方法使用mock调用另一个类的方法,只需要调用的方法被声明为final,因此PHPUnit无法模拟它.我可以采取不同的方法吗?
例:
要被嘲笑的阶级
class Class_To_Mock
{
final public function needsToBeCalled($options)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
我的测试用例
class MyTest extends PHPUnit_Framework_TestCase
{
public function testDoSomething()
{
$mock = $this->getMock('Class_To_Mock', array('needsToBeCalled'));
$mock->expects($this->once())
->method('needsToBeCalled')
->with($this->equalTo(array('option'));
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:如果使用Mike B提供的解决方案,并且你有一个setter/getter用于你正在模拟的对象进行类型检查(为了确保将正确的对象传递给setter),你需要模拟getter on你正在测试的类,让它返回另一个模拟.
例:
要被嘲笑的阶级
class Class_To_Mock
{
final public function needsToBeCalled($options)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
嘲笑
class Class_To_MockMock
{
public function needsToBeCalled($options)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
要测试的课程
class Class_To_Be_Tested
{
public function setClassToMock(Class_To_Mock $classToMock)
{
...
}
public function getClassToMock()
{
...
}
public function doSomething() …
Run Code Online (Sandbox Code Playgroud) 我最近遇到了一些代码,它使用自定义错误处理程序将任何PHP错误转换为通用应用程序异常.还定义了一个自定义异常处理程序,如果异常位于特定的错误代码范围内,它将记录该异常.例:
class AppException extends Exception
{
}
function error_handler($errno, $errstr, $errfile, $errline)
{
throw new AppException($errstr, $errno);
}
function exception_handler($exception)
{
$min = ...;
$max = ...;
if ($exception->getCode() >= $min && $exception->getCode() <= $max)
{
// log exception
}
}
set_error_handler('error_handler');
set_exception_handler('exception_handler');
$a[1]; // throws exception
Run Code Online (Sandbox Code Playgroud)
问题是我看到了以下内容:
try
{
do_something();
}
catch (AppException $exception)
{
}
Run Code Online (Sandbox Code Playgroud)
这意味着实际编程错误与"异常"行为之间没有区别.在进一步挖掘之后,我发现了围绕PHP错误代表"异常"行为的想法设计的部分代码,例如:
...
function my_function($param1, $param2)
{
// do something great
}
try
{
my_function('only_one_param');
}
catch (AppException $exception)
{
}
Run Code Online (Sandbox Code Playgroud)
这最终会混淆错误和应用程序界面的设计.
您对这种处理错误有何看法?是否值得将PHP的原生错误转化为异常?在上述代码库是围绕这个想法设计的情况下你做了什么?
我正在编写一个脚本,用boto旋转一个新的EC2实例,并使用Paramiko SSH客户端在实例上执行远程命令.无论出于何种原因,Paramiko客户端无法连接,我收到错误:
Traceback (most recent call last):
File "scripts/sconfigure.py", line 29, in <module>
ssh.connect(instance.ip_address, username='ubuntu', key_filename=os.path.expanduser('~/.ssh/test'))
File "build/bdist.macosx-10.3-fat/egg/paramiko/client.py", line 291, in connect
File "<string>", line 1, in connect
socket.error: [Errno 61] Connection refused
Run Code Online (Sandbox Code Playgroud)
我可以使用相同的密钥文件和用户手动ssh.有没有人使用Paramiko遇到问题?我的完整代码如下.谢谢.
import boto.ec2, time, paramiko, os
# Connect to the us-west-1 region
ec2 = boto.ec2.regions()[3].connect()
image_id = 'ami-ad7e2ee8'
image_name = 'Ubuntu 10.10 (Maverick Meerkat) 32-bit EBS'
new_reservation = ec2.run_instances(
image_id=image_id,
key_name='test',
security_groups=['web'])
instance = new_reservation.instances[0]
print "Spinning up instance for '%s' - %s. Waiting for it to …
Run Code Online (Sandbox Code Playgroud) 我正在处理的应用程序让用户以明文形式输入内容,稍后将以HTML格式显示.为了尽可能好地显示用户的内容,我们将内容转换如下:
由两个或多个新行字符分隔的任何文本块都包含在<p>标记中.剥离出新的行字符(以及其间的任何空格).
任何单个新行字符(以及周围的空格)都将替换为<br />标记.
我目前通过将文本放在两个正则表达式替换中来实现这一点,但是想知道它是否可以合并为一个.这就是我现在所拥有的(JavaScript):
// content holds the text to process
content = '<p>' + content.replace(/\n([ \t]*\n)+/g, '</p><p>')
.replace(/\n/g, '<br />') + '</p>';
Run Code Online (Sandbox Code Playgroud) 我在一个项目中使用Zend Framework和Doctrine,并且想知道是否有人可以建议将Doctrine的验证与Zend_Form集成的好方法.我正在努力避免代码重复.
php ×7
phpunit ×4
unit-testing ×4
mocking ×3
doctrine ×2
javascript ×2
amazon-ec2 ×1
backbone.js ×1
boto ×1
d3.js ×1
exception ×1
html5 ×1
interface ×1
paramiko ×1
php-ide ×1
phpdoc ×1
python ×1
regex ×1
string ×1
svg ×1
validation ×1