我对使用PHP进行测试有些新意.我使用Cucumber,RSpec,Capybara和Factory Girl对Rails进行了相当多的测试,但我几乎没有用PHP进行任何测试.
我会用尽可能最普遍的方式问一个关于我当前挑战的问题,因为我已经走了几条特定的道路而且只是沮丧地遇到了.
我想编写一个用于签署用户的功能测试.创建测试用户对象的好方法是什么,以便尝试登录?
更一般地说,在Symfony2中创建测试对象的事实标准是什么?赛程?某种工厂?
在Ruby中,我会使用Factory Girl,因为它允许您以干净,干燥的方式处理任何对象的依赖项.在Phactory中似乎有一个与Apple相当的工厂女孩,但不幸的是,该工具似乎没有得到广泛使用而且不再维护.
在PHPUnit中,可以在不同的测试套件中组织测试:
<phpunit bootstrap="Bootstrap.php">
<testsuites>
<testsuite name="zf2sandbox">
<directory>./AlbumTest</directory>
</testsuite>
</testsuites>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
此外,您可以定义过滤器
<filter>
<whitelist>
<directory suffix=".php">/var/www/sandbox/zf2sandbox/module/Album/src/Album/</directory>
</whitelist>
</filter>
Run Code Online (Sandbox Code Playgroud)
现在我想结合这两个胎儿.不允许将filter标记放入a testsuite(过滤器只是被忽略).
<phpunit bootstrap="Bootstrap.php">
<testsuites>
<testsuite name="zf2sandbox">
<directory>./AlbumTest</directory>
<filter>
<whitelist>
<directory suffix=".php">/var/www/sandbox/zf2sandbox/module/Album/src/Album/</directory>
</whitelist>
</filter>
</testsuite>
</testsuites>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以为每个过滤器定义过滤器(whilists,blacklists等)testsuite?
对于C#,有一种方法可以编写一个语句来等待页面上的元素出现:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Id("someDynamicElement"));
});
Run Code Online (Sandbox Code Playgroud)
但有没有办法在phpunit的selenium扩展中做同样的事情?
我发现的唯一的事情是$this->timeouts()->implicitWait(),但显然它不是我想要的.
这个问题是关于Selenium2和PHPUnit_Selenium2相应的扩展.
我想使用PHPUnit来测试以正确的顺序调用方法.
我->at()在模拟对象上使用的第一次尝试不起作用.例如,我预计以下内容会失败,但它不会:
public function test_at_constraint()
{
$x = $this->getMock('FirstSecond', array('first', 'second'));
$x->expects($this->at(0))->method('first');
$x->expects($this->at(1))->method('second');
$x->second();
$x->first();
}
Run Code Online (Sandbox Code Playgroud)
如果以错误的顺序调用事件,我能想到的唯一方法就是强制失败是这样的:
public function test_at_constraint_with_exception()
{
$x = $this->getMock('FirstSecond', array('first', 'second'));
$x->expects($this->at(0))->method('first');
$x->expects($this->at(1))->method('first')
->will($this->throwException(new Exception("called at wrong index")));
$x->expects($this->at(1))->method('second');
$x->expects($this->at(0))->method('second')
->will($this->throwException(new Exception("called at wrong index")));
$x->second();
$x->first();
}
Run Code Online (Sandbox Code Playgroud)
有没有更优雅的方式来做到这一点?谢谢!
我可能在这里遗漏了一些东西但是我有一个非常简单的帮助器类来创建一个目录:
// Helper class
<?php namespace MyApp\Helpers;
use User;
use File;
class FileSystemHelper
{
protected $userBin = 'users/uploads';
public function createUserUploadBin(User $user)
{
$path = $this->userBin . '/' . $user->id;
if ( ! File::isDirectory($path))
{
File::makeDirectory($path);
}
}
}
Run Code Online (Sandbox Code Playgroud)
以及相关测试:
// Associated test class
<?php
use MyApp\Helpers\FileSystemHelper;
class FileSystemHelperTest extends TestCase {
protected $fileSystemHelper;
public function setUp()
{
$this->fileSystemHelper = new FileSystemHelper;
}
public function testNewUploadBinCreatedWhenNotExists()
{
$user = new User; // this would be mocked
File::shouldReceive('makeDirectory')->once();
$this->fileSystemHelper->createUserUploadBin($user);
}
} …Run Code Online (Sandbox Code Playgroud) 我已经开始学习如何使用PHPUnit了.但是,我面临一个问题,我不知道如何解决.
我有一个名为的文件夹lessons,里面有一个composer.json我安装了PHPUnit 的文件夹.输出结果是一个名为的文件夹vendor和一个名为的子文件夹bin,其中包含phpunit文件.
在cmd我打字:cd c:/xampp/htdocs/lessons/vendor/bin.现在cmd文件夹设置为与phpunit相同的文件夹.我已经创建了一个目录lessons,我在其中调用了tests(lessons/tests),我将所有测试存储在其中.我创建了一个文件,PracticeTest.php其中包含一个非常简单的测试脚本.
当我回去cmd,然后键入phpunit tests我得到cannot open file tests.php当我尝试输入phpunit PracticeTest我得到cannot open file PracticeTest.php.当我尝试phpunit tests/PracticeTest(有.php或没有)我得到相同的错误,无法打开文件.
我怀疑它与cmd指向lessons/vendor/bin目录的那个面有关,但我不确定它是否真的是问题或如何解决它.
只是为了安排路径:
lessons\vendor\bin\lessons\tests\lessons\tests\PracticeTest.php提前致谢!
我刚刚开始在Zend中使用PHPUnit,并且无需帮助弄清楚这些测试应该如何工作.
如果我没有传递任何POST参数,我想测试表单是否返回任何错误消息.
问题是我的表单中的一个字段是使用Doctrine的 DoctrineModule\Form\Element\ObjectSelect
...
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'user',
'attributes' => array(
'id' => 'user-label',
),
'options' => array(
'object_manager' => $em,
'target_class' => 'Application\Entity\User',
'property' => 'username',
'label' => 'User:',
'display_empty_item' => true,
'empty_item_label' => '---',
'label_generator' => function($entity) {
return $entity->getUsername();
},
),
));
...
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Fatal error: Call to a member function getIdentifierFieldNames() on null
我尝试使用模拟对象覆盖此字段,但是Zend不允许objects输入type,只是类名(string),因此此代码不起作用:
public function testIfFormIsValid()
{
$objectSelect = $this->getMockBuilder('DoctrineModule\Form\Element\ObjectSelect')
->disableOriginalConstructor()
->getMock();
$objectSelect->expects($this->any())
->method('getValueOptions')
->will($this->returnValue(array())); …Run Code Online (Sandbox Code Playgroud) 我最近在一个基于CakePhp 3.x的应用程序的IntegrationTestCase中将PHPunit从5.3更新到5.5.我不明白如何更新我的模拟生成脚本.
最初我创建了这样的模拟:
$stub = $this->getMock('SomeClass', array('execute'));
$stub->method('execute')
->will($this->returnValue($this->returnUrl));
Run Code Online (Sandbox Code Playgroud)
更改为PHPUnit 5.5后,这给我发出以下警告:
PHPUnit_Framework_TestCase::getMock() is deprecated,
use PHPUnit_Framework_TestCase::createMock()
or PHPUnit_Framework_TestCase::getMockBuilder() instead
Run Code Online (Sandbox Code Playgroud)
为了修复此警告,我将模拟生成更改为:
$stub = $this->getMockBuilder('SomeClass', array('execute'))->getMock();
$stub->method('execute')
->will($this->returnValue($this->returnUrl));```
Run Code Online (Sandbox Code Playgroud)
现在,我在运行测试时收到以下错误消息:
exception 'PHPUnit_Framework_MockObject_RuntimeException'
with message 'Trying to configure method "execute" which cannot be
configured because it does not exist, has not been specified,
is final, or is static'
Run Code Online (Sandbox Code Playgroud)
有谁知道,如何避免这个错误?谢谢.
标题说明了一切.我想知道如何使用内存中的SQLite数据库正确地使用Dusk建立一个新的Laravel 5.4项目.
我可以运行测试,但是我收到一个错误:"没有这样的表:用户"
我添加了一个.env.dusk.local包含
APP_ENV=local
APP_KEY=RANDOM_STRING_HERE
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://laravel54.dev
DB_CONNECTION=sqlite
DB_DATABASE=':memory:' // I've also tried just :memory: and also adding these details to the config/database.php file but to no avail
Run Code Online (Sandbox Code Playgroud)
这是我正在运行的测试(直接来自文档)
<?php
namespace Tests\Browser;
use App\User;
use Tests\DuskTestCase;
use Laravel\Dusk\Chrome;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class LoginTest extends DuskTestCase
{
use DatabaseMigrations;
public function test_login_page()
{
$user = factory(User::class)->create();
$this->browse(function ($browser) use ($user) {
$browser->visit('/login')
->type('email', $user->email)
->type('password', 'secret')
->press('Sign in') …Run Code Online (Sandbox Code Playgroud) 我尝试为我User和Shop模型之间的关系创建单元测试,但是当我运行vendor\\bin\\phpunit这个错误时,我不知道这个,因为我是单元测试的新手.我试图在我的控制器上运行我的代码以查看关系是否真的有效,幸运的是它按预期工作,但在phpunit中运行时却没有.这个phpunit没有与Models一起工作,我做错了什么?
Fatal error:Uncaught Error: Call to a member function connection() on null in E:\projects\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1013
Stack trace:E:\projects\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(979): Illuminate\Database\Eloquent\Model::resolveConnection(NULL)
这是我的UserTest.php
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\User;
use App\Shop;
class UserTest extends TestCase
{
protected $user, $shop;
function __construct()
{
$this->setUp();
}
function setUp()
{
$user = new User([
'id' => 1,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'JohnDoe@example.com',
'password' => 'secret',
'facebook_id' => null,
'type' => 'customer', …Run Code Online (Sandbox Code Playgroud) phpunit ×10
php ×6
laravel ×2
laravel-5.4 ×2
testing ×2
unit-testing ×2
cakephp ×1
cakephp-3.0 ×1
eloquent ×1
filter ×1
laravel-4 ×1
laravel-dusk ×1
mockery ×1
mocking ×1
selenium ×1
sqlite ×1
symfony ×1
test-suite ×1