标签: phpunit

使用Zend Auth和Zend ACL进行PHP单元测试

我有一个登录后面的应用程序,并使用zend_acl和zend_auth.

在预分派期间,我有一个ACL插件,可以为ACL创建所有规则.我还有一个Auth插件,用于检查您是否已登录,如果可以,则根据ACL访问所请求的资源.

由于应用程序完全在登录后面,因此只有在您登录时才会创建ACL.

单元测试这似乎是不可能的,或者更可能是我错过了一些明显的东西.

在我的单元测试设置方法中,我模拟了一个返回zend_auth实例的成功登录.通过的测试表明此登录成功.

但是,如果我然后通过测试尝试分派到另一个位置,或者评估登录用户是否可以访问给定资源,则它总是被插件拒绝,因为它们仍然没有登录.我不知道为什么这是,有人可以提供建议吗?

例如,这传递:

 public function testLoggedIn()
 {
  $this->assertTrue( Zend_Auth::getInstance()->hasIdentity() );
 }
Run Code Online (Sandbox Code Playgroud)

这失败了,因为它被插件拒绝了:

 public function testUserAccess()
 {

   $this->dispatch('/home');
          $this->assertResponseCode(200);
          $this->assertQueryContentContains('#nav_side');  
          $this->resetRequest()
           ->resetResponse();

 }
Run Code Online (Sandbox Code Playgroud)

这个,我发现似乎仍然会重定向回登录页面,因为插件不知道用户已登录.

任何帮助非常感谢.

php phpunit unit-testing zend-auth zend-acl

10
推荐指数
1
解决办法
3513
查看次数

PHPUnit和DBUnit - 入门

有没有人有关于如何开始将DBUnit层添加到我的PHPUNit测试的好的,有用的教程或书籍的链接?

我试过跟随代码

protected function getDatabaseTester()
{
    $pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'pass');
    $connection = new PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection($pdo);
    $tester = new PHPUnit_Extensions_Database_DefaultTester($connection);
    $tester->setSetUpOperation(PHPUnit_Extensions_Database_Operation_Factory::CLEAN_INSERT());
    $tester->setTearDownOperation(PHPUnit_Extensions_Database_Operation_Factory::NONE());
    /*
    * the next line fails with the error

    PHP Fatal error:  __autoload(): Failed opening required 'PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet.php' (include_path= *** 

    */
    $tester->setDataSet(new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(dirname(__FILE__).'/../../../files/xml_database_export.xml'));
    return $tester;
}
Run Code Online (Sandbox Code Playgroud)

XML是通过mysqldump命令创建的.我很乐意使用CSV,甚至是内存中的数组(无论什么工作)

不幸的是,我似乎无法启动此系统.

php phpunit dbunit

10
推荐指数
1
解决办法
1万
查看次数

PHPUnit和PHPSpec之间的异同

我目前正在研究我应该用于php的测试框架.我有两个选择是PHPUnit和PHPSpec.我知道PHPUnit是TDD(测试驱动开发),PHPSpec是BDD(行为驱动开发).然而,由于PHPSpec网站缺乏任何真正的PHPSpec教程和有限的文档,我无法得出完整的结论.我向大家提出的问题是,这两个框架之间有哪些相似点和不同之处?是的,BDD和TDD是大的,但还有其他吗?它们看似相似,但没有人详细谈论它们的相似之处.谢谢.

php phpunit unit-testing phpspec

10
推荐指数
1
解决办法
3863
查看次数

如何在Laravel中进行单元测试期间获取视图数据

我想检查给定一个控制器函数中的视图的数组是否具有某些键值对.我如何使用phpunit测试做到这一点?

//my controller I am testing


public function getEdit ($user_id)
{
    $this->data['user'] = $user = \Models\User::find($user_id);

    $this->data['page_title'] = "Users | Edit";

    $this->data['clients'] = $user->account()->firstOrFail()->clients()->lists('name', 'id');

    $this->layout->with($this->data);

    $this->layout->content = \View::make('user/edit', $this->data);
}

//my test
public function testPostEdit (){

    $user = Models\User::find(parent::ACCOUNT_1_USER_1);

    $this->be($user);

    $response = $this->call('GET', 'user/edit/'.parent::ACCOUNT_1_USER_1);   

    //clients is an array.  I want to get this 
    //array and use $this->assetArrayContains() or something
    $this->assertViewHas('clients');

    $this->assertViewHas('content');

}
Run Code Online (Sandbox Code Playgroud)

php phpunit laravel laravel-4

10
推荐指数
4
解决办法
2万
查看次数

如何在运行PHPUnit的NetBeans上修复"无法识别的选项 - 运行"

我正在尝试将PHPUnit运行到NetBeans 8.0.2中.

如果我在我的文件夹中运行#phpunit测试所有测试运行.所以似乎已经设置好了.

但是在NetBeans输出中我总是得到:

"C:\nginx\php\5.6.12\php.exe" "C:\nginx\php\5.6.12\phpunit.phar" "--colors" "--log-junit" "C:\Users\...\AppData\Local\Temp\nb-phpunit-log.xml" "--bootstrap" "E:\var\www\...\tests\TestHelper.php" "--configuration" "E:\var\www\...\tests\phpunit.xml" "C:\Program Files\NetBeans 8.0.2\php\phpunit\NetBeansSuite.php" "--run=E:\var\www\...\tests\app\utils\FormatUtilTest.php"
Run Code Online (Sandbox Code Playgroud)

Sebastian Bergmann和贡献者的PHPUnit 4.8.2.

无法识别的选项 - 运行

完成.

也许" - 运行消息"是正确的,因为PHPUnit手册中不存在此命令.但如果是这样,如何为NetBeans创建另一个脚本来执行测试?

php phpunit netbeans

10
推荐指数
2
解决办法
1998
查看次数

在所有测试之前运行命令

我创建了一个Symfony命令来将我的应用程序重置为初始状态.要从cli运行该命令,我需要输入:

php bin/console app:reset
Run Code Online (Sandbox Code Playgroud)

我想在所有单元测试之前运行该命令一次.我可以设法在每次测试之前完成,并且肯定在所有课程之前.因此我使用了那段代码:

public function setUp()
{
    $kernel = new \AppKernel('test', true);
    $kernel->boot();
    $app = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
    $app->setAutoExit(false);

    $app->run(new ArrayInput([
        'command' => 'app:reset', ['-q']
    ]), new NullOutput());
}
Run Code Online (Sandbox Code Playgroud)

如上所述,这在每次测试之前都很好用,并且setUpBeforeClass()我可以在每个类之前使用它,但是在所有测试之前就足够了,因为该命令需要一些时间才能运行.

phpunit symfony

10
推荐指数
2
解决办法
5344
查看次数

什么可能导致PHPUnit不打印非常大的错误消息?

这似乎只发生在我的系统上.没有其他人可以重现它.如果错误的消息太大(在我的情况下大约65k字节),屏幕上不会打印任何内容.我在Windows 7上使用PHP 7.1.3,默认的php.ini(内存限制设置为8gb)和默认的PHPUnit 6.0.13配置.错误不会出现在prompt和powershell中.

<?php

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\TestCase;

class MyConstraint extends Constraint
{
    protected $expected;

    function __construct($expected){
        parent::__construct();
        $this->expected = $expected;
    }

    protected function matches($actual){
        return false;
    }

    function failureDescription($other): string{
        return "desc";
    }

    function toString(){
        return "description";
    }

    function additionalFailureDescription($other): string{
        return str_repeat("x", 100000);
        // If set to a smaller dump, error will appear
        // some people I asked to try could dump one million
        // bytes without problems, while I can't print more
        // than about 50k …
Run Code Online (Sandbox Code Playgroud)

php phpunit

10
推荐指数
1
解决办法
412
查看次数

PHPUnit中的Mocks vs Stubs

我知道存根验证状态,模拟验证行为.

如何在PHPUnit中进行模拟以验证方法的行为?Phpunit没有验证方法(verify()),而且我不知道如何使moks成为PHPUnit.

在文档中,创建存根很好地解释了:

// Create a stub for the SomeClass class.
$stub = $this->createMock(SomeClass::class);

// Configure the stub.
$stub
    ->method('doSomething')
    ->willReturn('foo');

// Calling $stub->doSomething() will now return 'foo'.
$this->assertEquals('foo', $stub->doSomething());
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,我正在验证状态,说回复一个答案.

如何创建模拟和验证行为的示例?

testing phpunit unit-testing mocking stubs

10
推荐指数
2
解决办法
7477
查看次数

Laravel phpunit测试获取参数

我正在为我的控制器编写一些测试,但我的一个测试不起作用.它倾向于搜索并将结果返回到页面.但它实际上是重定向到主页.这是我的代码:

use DatabaseMigrations;
protected $user;
public function setUp()
{
    parent::setUp();

    $this->seed();

    $this->user = factory(User::class)->create(['role_id' => 3]);
}

/** @test */
public function test_manage_search_user()
{
    $response = $this->followingRedirects()->actingAs($this->user)->get('/manage/users/search', [
        'choices' => 'username',
        'search' => $this->user->username,
    ]);

    $response->assertViewIs('manage.users');
    $response->assertSuccessful();
    $response->assertSee($this->user->email);
}
Run Code Online (Sandbox Code Playgroud)

您应该使其工作的URL如下所示:

http://localhost/manage/users/search?choices=username&search=Test
Run Code Online (Sandbox Code Playgroud)

我再次检查,看起来它没有在带有get请求参数中给出.我怎样才能解决这个问题?

php phpunit laravel

10
推荐指数
3
解决办法
7999
查看次数

使用PHP7.2和7.1运行的phpunit测试比使用PHP7.0运行时慢3倍

如果我使用PHP7.2或PHP7.1运行我的测试,它们比使用PHP7.0运行它们慢大约3倍.无论如何都要了解为什么会发生这种情况?

即使我单独运行测试套件(功能和单元),我仍然看到减速.只有当我单独运行测试时,速度差才变得无关紧要.

我正在使用Laravel 5.5.20和Laravel Homestead 7.0.1.我有47个相当简单的测试,有些是数据库,有些只是简单的断言; 所以没有什么需要花费很长时间.

我安装了johnkary/phpunit-speedtrap以查看哪些测试时间最长,所以我可以删除那些但是没有特定的测试需要很长时间,因为如果我删除了有问题的测试,下一个测试需要很长时间(见下文) .

First Run                Second Run
Test A    0.2 sec        Test A    0.2 sec
Test B.   0.3 sec        Test B.   0.3 sec
Test C    0.1 sec        Test C    0.1 sec
Test D    0.1 sec        Test D    0.1 sec
Test E    9.3 sec        REMOVED Test E
Test F    0.3 sec        Test F    9.3 sec <-- Test F now takes ages
Test G    0.2 sec        Test G    0.2 sec
Run Code Online (Sandbox Code Playgroud)

我也在使用内存中的SQLite3数据库,以及Laravel CreatesApplicationRefreshDatabase …

phpunit php-7 php-7.1 php-7.2 laravel-5.5

10
推荐指数
1
解决办法
1307
查看次数