我刚刚开始学习phpspec以取代PHPUnit.不幸的是,我更喜欢使用PHPStorm编辑器的代码完成功能,这使得即使是PHPUnit的详细模拟界面也可以非常快速地输入.
phpspec没有这样的运气.鉴于这样的类:
<?php
namespace spec\MyVendor\MyClass;
use PhpSpec\ObjectBehavior;
class MyClassSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('MyVendor\MyClass');
}
function it_should_do_something()
{
$this->???
}
}
Run Code Online (Sandbox Code Playgroud)
首先,shouldHaveType显示'找不到方法'突出显示,其次,如果我尝试自动完成???,我的选项仅限于少数方法ObjectBehaviour.我希望看到的东西一样shouldHaveType,shouldImplement和许多.
我在Github上找到了这个phpspec-stubs存储库,但它似乎只定义了一个方法,并且需要扩展一个包装类.
还有一个PHPStorm插件,但我不清楚这是否应该提供自动完成,并且当前版本在输入任何PHP文件时立即在PHPStorm中给我一个NullPointerException.
那么,你们所有的phpspec用户都打字很多,还是有另一种解决方案?
给出包含Doctrine注释的PHPDoc注释的以下属性:
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
Run Code Online (Sandbox Code Playgroud)
"注释"其中一个注释行的最佳方法是什么?例如:
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* //Comment out please// @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
Run Code Online (Sandbox Code Playgroud)
这样做是否有受支持的方式或一般惯例?
我有一个Yaml加载器,为"配置文件"加载其他配置项(其中一个应用程序可以使用不同的配置文件,例如,对于同一站点的不同本地版本).
我的装载机非常简单:
# YamlProfileLoader.php
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Yaml\Yaml;
class YamlProfileLoader extends FileLoader
{
public function load($resource, $type = null)
{
$configValues = Yaml::parse($resource);
return $configValues;
}
public function supports($resource, $type = null)
{
return is_string($resource) && 'yml' === pathinfo(
$resource,
PATHINFO_EXTENSION
);
}
}
Run Code Online (Sandbox Code Playgroud)
加载器或多或少都像这样使用(简化了一下,因为还有缓存):
$loaderResolver = new LoaderResolver(array(new YamlProfileLoader($locator)));
$delegatingLoader = new DelegatingLoader($loaderResolver);
foreach ($yamlProfileFiles as $yamlProfileFile) {
$profileName = basename($yamlProfileFile, '.yml');
$profiles[$profileName] = $delegatingLoader->load($yamlProfileFile);
}
Run Code Online (Sandbox Code Playgroud)
它解析的Yaml文件也是如此:
# profiles/germany.yml
locale: de_DE
hostname: %profiles.germany.host_name%
Run Code Online (Sandbox Code Playgroud)
目前,结果数组包含字面上'%profiles.germany.host_name%'的'hostname'数组键.
那么,如何解析%参数以获取实际参数值? …
鉴于这个简单的Go程序,它只需要一个命令行参数,我该如何改进它以便flag.Usage()提供有用的输出?
package main
import (
"flag"
"fmt"
"os"
)
func main() {
flag.Parse()
if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(1)
}
args := flag.Args()
fmt.Println(args[0])
}
Run Code Online (Sandbox Code Playgroud)
当前输出没有给出参数:
$ ./args
Usage of ./args:
Run Code Online (Sandbox Code Playgroud)
(即用法是空的,因为我无法告诉usage()函数需要哪些参数).
我可以删除flag.Usage()并替换为这样的东西:
fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "<argument>")
Run Code Online (Sandbox Code Playgroud)
但是,如果有一个很好的方法,我不想重新发明轮子flag.Usage().特别是因为它已经处理了可选参数:
$ ./args -foo
flag provided but not defined: -foo
Usage of ./args:
Run Code Online (Sandbox Code Playgroud) 使用PHPUnit和模拟对象,我试图测试一些代码,用于get_class确定过滤器是否包含对象.
这是要测试的类:
class BlockFilter implements FilterInterface
{
private $classes;
public function __construct(array $classes = array())
{
$this->classes = $classes;
}
public function isIncluded(NodeTraversableInterface $node)
{
if (Type::BLOCK != $node->getDocumentType()) {
return false;
}
if (! empty($this->classes)) {
/*** HERE IS THE PROBLEM: ***/
return in_array(get_class($node), $this->classes);
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我单元测试的方法:
public function testIfContainerBlockIsIncluded()
{
$containerBlock = $this->getMock('Pwn\ContentBundle\Document\ContainerBlock');
$containerBlock->expects($this->any())->method('getDocumentType')->will($this->returnValue(Type::BLOCK));
$filter = new BlockFilter(array('Pwn\ContentBundle\Document\ContainerBlock'));
$this->assertTrue($filter->isIncluded($containerBlock));
}
Run Code Online (Sandbox Code Playgroud)
模拟对象的$containerBlock行为类似于真实对象Pwn\ContentBundle\Document\ContainerBlock; 甚至代码使用instanceof作品(因为PHPUnit使它成为真正类的子类,我相信).
正在测试的代码get_class用于获取类的字符串值,并将其与预期类名称数组进行比较.不幸的是,对于mock对象,get_class返回如下内容:
Mock_ContainerBlock_ac231064 …Run Code Online (Sandbox Code Playgroud) 我想在JavaScript中使用Flickr API方法flickr.photos.search来搜索具有特定标记的所有照片:
http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key={API_KEY}&user_id=50001188%40N00&tags=fazynet&format=rest
Run Code Online (Sandbox Code Playgroud)
(要使其工作,请更改{API_KEY}为有效的Flickr API密钥.)
我的问题是:在客户端JavaScript中包含该密钥是否安全?
我已经看过一些教程网站正在做这件事,但是什么阻止某人复制他们的API密钥(或我的)并超过速率限制?
当然,我可以在Node.js中设置一个非常简单的代理来插入密钥(甚至可能是Apache或Nginx都可以这样做),但是设置和监控还有一件事.
如果有一个安全,纯粹的JavaScript解决方案,我会接受的.:)
注意,我目前正在使用此查询,不需要身份验证:
http://api.flickr.com/services/feeds/photos_public.gne?id=50001188@N00&tags=fazynet&format=json
Run Code Online (Sandbox Code Playgroud)
但是,结果似乎不再反映变化(至少等待24小时后).
我正在使用Symfony2的树构建器,我看到它有一些基本的验证规则,如下所述:http://symfony.com/doc/current/components/config/definition.html#validation-rules
有没有办法通过正则表达式进行验证?
这是我现在正在做的事情,但我不确定这是否是"最佳做法".我要验证的配置项是root_node.
config.yml
my_bundle:
root_node: /some/path # this one is valid
Run Code Online (Sandbox Code Playgroud)
的configuration.php
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('my_bundle');
$rootNode
->children()
->scalarNode('root_node')
->end()
->end()
->end();
return $treeBuilder;
Run Code Online (Sandbox Code Playgroud)
MyBundleExtension.php
$nodePattern = '#/\w+(/w+)*#';
if (! preg_match($nodePattern, $config['root_node'])) {
throw new \Exception("root_node is not valid: must match the pattern: $nodePattern");
}
Run Code Online (Sandbox Code Playgroud)
所以,我真正追求的是TreeBuilder方法:
->validate()->ifNotMatchesRegex()->thenInvalid()
Run Code Online (Sandbox Code Playgroud)
或者,如果没有,那就是执行我的验证规则的最佳方法.
使用Go,我想将长字符串截断为任意长度(例如,用于记录).
const maxLen = 100
func main() {
myString := "This string might be longer, so we'll keep all except the first 100 bytes."
fmt.Println(myString[:10]) // Prints the first 10 bytes
fmt.Println(myString[:maxLen]) // panic: runtime error: slice bounds out of range
}
Run Code Online (Sandbox Code Playgroud)
现在,我可以使用额外的变量和if语句来解决它,但这看起来非常冗长:
const maxLen = 100
func main() {
myString := "This string might be longer, so we'll keep all except the first 100 bytes."
limit := len(myString)
if limit > maxLen {
limit = maxLen
} …Run Code Online (Sandbox Code Playgroud) php ×5
go ×2
symfony ×2
doctrine-orm ×1
flickr ×1
javascript ×1
phpspec ×1
phpstorm ×1
phpunit ×1
security ×1