为此搜索了stackoverflow并找不到答案
来自Ruby On Rails和Rspec,我需要一个像rspec这样的工具(更容易过渡).通过PEAR安装并试图运行它但是它还没有工作(还)
只是想问周围是否有人使用它有同样的问题,因为它根本没有运行
尝试使用手册中的示例运行它 - http://dev.phpspec.org/manual/en/before.writing.code.specify.its.required.behaviour.html
phpspec NewFileSystemLoggerSpec
Run Code Online (Sandbox Code Playgroud)
没有回报
甚至跑步
phpspec some_dummy_value
Run Code Online (Sandbox Code Playgroud)
没有回报
我想检查具有访问控制的方法,例如,只授予具有特定角色的方法.因此,我在Symfony中了解两种方式:
当谈到单元测试时(对于我的案例phpspec,但我认为phpunit行为在这种情况下几乎相同),我想测试只有匿名用户应该能够调用方法.数字2,它工作正常.在这里我的设置:
RegistrationHandlerSpec:
class RegistrationHandlerSpec extends ObjectBehavior
{
function let(Container $container, AuthorizationCheckerInterface $auth) {
$container->get('security.authorization_checker')->willReturn($auth);
$this->setContainer($container);
}
function it_should_block_authenticated_users(AuthorizationCheckerInterface $auth)
{
$auth->isGranted("ROLE_USER")->willReturn(true);
$this->shouldThrow('Symfony\Component\Security\Core\Exception\AccessDeniedException')->during('process', array());
}
}
Run Code Online (Sandbox Code Playgroud)
在RegistrationHandler中,我有以下方法:
class RegistrationHandler
{
public function process()
{
$authorizationChecker = $this->get('security.authorization_checker');
if ($authorizationChecker->isGranted('ROLE_USER')) {
throw new AccessDeniedException();
}
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,这种方法工作正常 - 但通常情况下,我更喜欢使用1.带安全性注释(Sensio FrameworkExtraBundle),因此,它不起作用/我不知道为什么当它被编写为注释时没有触发异常:
/**
* @Security("!has_role('ROLE_USER')")
*/
public function process()
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
有没有人知道如何使用@Security注释的第一种方法使这个例子工作,这是更可读和symfony推荐的最佳做法?
我在项目中将 Doctrine 2.5 更新为 2.6,但 phpspec 已损坏。
该函数getEntityChangeSet()现在通过引用返回。phpspec 似乎不支持。
$unitOfWork
->getEntityChangeSet($site)
->willReturn(['_dataParent' => [0 => 2, 1 => 3]]);
Run Code Online (Sandbox Code Playgroud)
响应是
returning by reference not supported
底层函数(doctrine/doctrine2)是
public function & getEntityChangeSet($entity)
{
$oid = spl_object_hash($entity);
$data = [];
if (!isset($this->entityChangeSets[$oid])) {
return $data;
}
return $this->entityChangeSets[$oid];
}
Run Code Online (Sandbox Code Playgroud)
您知道是否可以绕过这个或更改测试以使其工作?
我试图通过travis-ci运行phpspec.
规格在我的本地机器上运行正常,但在travis上它找不到phpspec文件.
0.01s$ bin/phpspec run -v
/home/travis/build.sh: line 41: bin/phpspec: No such file or directory
The command "bin/phpspec run -v" exited with 127.
Run Code Online (Sandbox Code Playgroud)
我的travis.yml看起来像这样:
语言:php
php:
- 5.4
- 5.5
- 5.6
before_script:
- composer self-update
install:
- composer install --prefer-source --no-interaction --dev
script:
- bin/phpspec run -v
Run Code Online (Sandbox Code Playgroud)
我的作曲家是这样的
"require": {
"php": ">=5.4.0",
"illuminate/support": "4.2.*",
"guzzlehttp/guzzle": "~4.0"
},
"autoload": {
"psr-0": {
"..."
}
},
"minimum-stability": "stable",
"require-dev": {
"phpspec/phpspec": "2.0.*@dev"
}
Run Code Online (Sandbox Code Playgroud)
有关如何使其工作的任何想法?
这个类非常简单,如果有空间,它会在字符串中添加一个twitter标签.Twitter只允许140个字符(网址减去23).因此,如果有一个空间,则标签会不断添加.
我不认为它100%按预期工作,但这与我在下面的问题无关.
class Hashtags {
private $url_character_count = 23;
private $characters_allowed = 140;
public function __construct(Article $article)
{
$this->article = $article;
$this->characters_remaining = $this->characters_allowed - $this->url_character_count;
}
public function createHashtagString()
{
$hashtags = '';
$hashtags .= $this->addEachNodeHashtag();
$hashtags .= $this->addHashtagIfSpace($this->article->topic_hashtag);
$hashtags .= $this->addHashtagIfSpace($this->article->pubissue_hashtag);
$hashtags .= $this->addHashtagIfSpace($this->article->subject_area_hashtag);
$hashtags .= $this->addHashtagIfSpace('#aviation');
return $hashtags;
}
private function addEachNodeHashtag()
{
//Returns a hashtag or calls hashtagString() if it is a comma separated list
}
private function hashtagString()
{
//Explodes a comma seperated string of …Run Code Online (Sandbox Code Playgroud) 在phpspec中,如何测试类属性是否包含特定类型的数组?
例如:
class MyClass
{
private $_mySpecialTypes = array();
// Constructor ommitted which sets the mySpecialTypes value
public function getMySpecialTypes()
{
return $this->_mySpecialTypes;
}
}
Run Code Online (Sandbox Code Playgroud)
我的规格看起来像这样:
public function it_should_have_an_array_of_myspecialtypes()
{
$this->getMySpecialTypes()->shouldBeArray();
}
Run Code Online (Sandbox Code Playgroud)
但我想确保数组中的每个元素都是类型 MySpecialType
什么是在phpspec中做到这一点的最好方法?
我搜索关于调度程序symfony2的phpspec进行功能测试的可能性
我想这样做:
$dispatcher->dispatch('workflow.post_extract', $event)->shouldBeCalled();
Run Code Online (Sandbox Code Playgroud)
我的代码在这里:
function it_should_dispatch_post_extract(
EventDispatcher $dispatcher, GenericEvent $event,
TransformerInterface $transformer, ContextInterface $context, LoaderInterface $loader
)
{
$c = new \Pimple([
'etl' => new \Pimple([
'e' => function() {
return new ExtractorMock();
},
't' => function() {
return new Transformer();
},
'l' => function() {
return new Loader();
},
'c' => function() {
return new Context();
},
])
]);
$dispatcher->dispatch('workflow.post_extract', $event)->shouldBeCalled();
$this->process($c);
}
Run Code Online (Sandbox Code Playgroud)
phpspec的答案是:
! should dispatch post extract
method call:
Double\Symfony\Component\EventDispatcher\EventDispatcher\P10->dispatch("workflow.post_extract", Symfony\Component\EventDispatcher\GenericEvent:0000000043279e10000000004637de0f)
was not expected.
Expected calls …Run Code Online (Sandbox Code Playgroud) 好的,所以我试图将我的一个软件包移到PHPSpec测试中,但很快我遇到了这个问题.这些包是一个购物车包,所以我想测试一下,当你向购物车添加两个商品时,购物车的数量为2,简单.不过,当然,在一个购物车,增加了两个相同的项目的时候,不会有在车一个新的条目,但原来的项目将得到的2"数量"所以,而不是当他们是,例如,不同的尺寸.因此,每个项目都由唯一的rowId标识,基于它的ID和选项.
这是生成rowId的代码(由add()方法使用):
protected function generateRowId(CartItem $item)
{
return md5($item->getId() . serialize($item->getOptions()));
}
Run Code Online (Sandbox Code Playgroud)
现在我写了这样的测试:
public function it_can_add_multiple_instances_of_a_cart_item(CartItem $cartItem1, CartItem $cartItem2)
{
$this->add($cartItem1);
$this->add($cartItem2);
$this->shouldHaveCount(2);
}
Run Code Online (Sandbox Code Playgroud)
但问题是,两个存根都返回null该getId()方法.所以我尝试设置willReturn()for该方法,所以我的测试成了这样:
public function it_can_add_multiple_instances_of_a_cart_item(CartItem $cartItem1, CartItem $cartItem2)
{
$cartItem1->getId()->willReturn(1);
$cartItem2->getId()->willReturn(2);
$this->add($cartItem1);
$this->add($cartItem2);
$this->shouldHaveCount(2);
}
Run Code Online (Sandbox Code Playgroud)
但现在我得到错误,告诉我意外的方法被称为getName().所以我必须对CartItem接口上调用的所有方法做同样的事情:
public function it_can_add_multiple_instances_of_a_cart_item(CartItem $cartItem1, CartItem $cartItem2)
{
$cartItem1->getId()->willReturn(1);
$cartItem1->getName()->willReturn(null);
$cartItem1->getPrice()->willReturn(null);
$cartItem1->getOptions()->willReturn([]);
$cartItem2->getId()->willReturn(2);
$cartItem2->getName()->willReturn(null);
$cartItem2->getPrice()->willReturn(null);
$cartItem2->getOptions()->willReturn([]);
$this->add($cartItem1);
$this->add($cartItem2);
$this->shouldHaveCount(2);
}
Run Code Online (Sandbox Code Playgroud)
现在这个工作,测试是绿色的.但感觉不对......我错过了什么或者这是对PHPSpec的限制吗?
我很难决定或理解在Laravel中测试的最佳方法.
我非常喜欢PHPSpec测试的行为方面,尽管它与测试Eloquent模型或与活动记录ORM相关的任何内容都不兼容.
当测试像服务提供商PHPSpec这样的东西似乎是要走的路.
**是否需要使用像PHPUnit这样的模型测试模型,然后测试其他非ORM层,例如PHPSpec之类的服务提供商?***
我在phpspec中仍然很新鲜,但通常我会在遇到困难时找到解决方案,但这个很难.
我尝试了很多不同的方法,但我还没有找到解决方案.我正在使用Symfony2.
我有一个我想测试的课程:
class MyClass
{
public function getDataForChildren(MyObject $object)
{
foreach ($object->getChildren() as $child) {
$query = \json_decode($child->getJsonQuery(), true);
$data = $this->someFetcher->getData($query);
$child->setData($data);
}
return $object;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我的spec类:
class MyClassSpec
{
function let(SomeFetcher $someFetcher)
{
$this->beConstructedWith($someFetcher);
}
function it_is_initializable()
{
$this->shouldHaveType('MyClass');
}
function it_should_get_data_for_children_and_return_object(
MyClass $object,
MyClass $child, // it means that MyClass has a self-reference to MyClass
$someFetcher
)
{
$query = '{"id":1}';
$returnCollection = new ArrayCollection(array($child));
$object->getChildren()->shouldBeCalled()->willReturn($returnCollection);
$child->getJsonQuery()->shouldBeCalled()->willReturn($query);
$someFetcher->getData($query)->shouldBeCalled();
$this->getDataForChildren($object);
}
}
Run Code Online (Sandbox Code Playgroud)
运行phpspec后,我收到此错误:
warning: json_decode() expects …Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用PhpSpec.当我这样做:
bin/phpspec desc src/CRMPicco/GolfBundle/Controller/CourseGuideController
我收到以下错误:
[PhpSpec\Exception\Locator\ResourceCreationException]
Can not find appropriate suite scope for class `src/CRMPicco/GolfBundle/Controller/CourseGuideController`.
Run Code Online (Sandbox Code Playgroud)
我CourseGuideController在该目录中创建了一个空类,因此该类存在.
我的phpspec.yml:
suites:
CRMPiccoGolfBundle: { namespace: CRMPicco, spec_path: src/CRMPicco/GolfBundle }
Run Code Online (Sandbox Code Playgroud)
CourseGuideController.php:
namespace CRMPicco\GolfBundle\Controller;
class CourseGuideController
{
}
Run Code Online (Sandbox Code Playgroud) 我正在学习phpspec,无法弄清楚测试没有通过的原因.
这是我的功能:
public function isTaskForChange($task)
{
$supportedTasks = array_keys($this->availableTasks());
$isTaskForChange = in_array($task, $supportedTasks);
return $isTaskForChange;
}
Run Code Online (Sandbox Code Playgroud)
这是phpspec的测试:
public function it_validates_if_task_should_be_changed()
{
$this->isTaskForChange('write')->shouldReturn(true);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此代码时,我会回来:
warning: array_keys() expects parameter 1 to be array, null given
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何模拟$ this-> availableTasks()来返回值?