标签: phpunit

PHPUnit和SplFileObject在只读对象上返回true isWritable

我有一个Logger接口,SplFileObject在构造函数中接受一个用作该特定日志的文件.还有log($timestamp, $message)一种可用于实际记录的方法.在我的第一个实现中,当实例化一个新对象并传递一个只读时,SplFileObject应抛出一个异常.我写了一个合适的单元测试:

<?php
class FileLoggerTest extends PHPUnit_Framework_TestCase {

    /**
     * @expectedException \InvalidArgumentException
     */
    public function testReadOnlyFileObjectFailure() {
        $file = '/Library/WebServer/Documents/sprayfire/tests/mockframework/logs/test-log.txt';
        $LogFile = new \SplFileObject($file);
        $Logger = new \libs\sprayfire\logger\FileLogger($LogFile);
        $Logger->log('test', 'something');
    }

}
?>
Run Code Online (Sandbox Code Playgroud)

通常我会有一个生成目录名的方法,但是当我开始遇到问题时,我将其更改为绝对路径以排除原因.

这是实施:

namespace libs\sprayfire\logger;
use \SplFileObject as SplFileObject;
use \InvalidArgumentException as InvalidArgumentException;
use libs\sprayfire\logger\Logger as Logger;

    /**
     * @brief A framework implemented class that adds a timestamp log message to
     * the end of an injected file.
     */
    class …
Run Code Online (Sandbox Code Playgroud)

php spl phpunit

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

将PHPUnit与Selenium一起使用,如何测试元素是否包含某些内容?

我正在使用PHPUnit和Selenium,并且$this->assertElementContainsText('id=foo', 'bar')当它发现这个时当前正在使用类似的东西:<p id="foo">bar</p>

但是,我也试图测试p#foo可能包含其他HTML的情况,我想测试内容是否完全匹配.在我看来,它看起来像$this->assertElementTextEquals('id=foo', '<a href="http://www.example.com/">bar</a>').

有没有现成的方法来实现PHPUnit和Selenium?

php testing selenium phpunit

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

如何对执行命令行程序的PHP类方法进行单元测试?

对于我正在开发的PHP应用程序,我需要阅读当前的git修订版SHA,当然我可以通过使用shell_exec或反引号来执行git命令行客户端.

我显然已将此调用放入其自己的方法中,以便我可以轻松地隔离并模拟其余的单元测试.所以我的班级看起来有点像这样:

class Task_Bundle
{

    public function execute()
    {
        // Do things
        $revision = $this->git_sha();
        // Do more things
    }

    protected function git_sha()
    {
        return `git rev-parse --short HEAD`;
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,虽然我可以通过模拟git_sha来测试大部分类,但我很难看到如何测试实际的git_sha()方法,因为我没有看到为它创建已知状态的方法.我不认为单元测试中有任何实际价值也要求git rev-parse比较结果?我想知道至少断言命令已经运行,但是我看不到任何方法来获取PHP执行的shell命令的历史 - 即使我指定PHP应该使用BASH而不是SH,历史列表会出现空,我认为因为单独的反引号执行是单独的终端会话.

我很想听听有关我如何测试这一点的任何建议,或者是否可以将该方法保持未经测试并在将来维护应用程序时要小心它?

php git phpunit unit-testing

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

symfony2测试用例请求cookie

在我的测试中,我想指定一个cookie来处理请求.我追溯了代码,看看如何在客户端的__construct中使用cookie jar.虽然此处的var_dump和服务器端的var_dump显示没有与请求一起发送cookie.我还尝试使用HTTP_COOKIE发送一个更简单的字符串,如图所示.

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\BrowserKit\CookieJar;
class DefaultControllerTest extends WebTestCase {
    public function test() {
        $jar = new CookieJar();
        $cookie = new Cookie('locale2', 'fr', time() + 3600 * 24 * 7, '/', null, false, false);
        $jar->set($cookie);
        $client = static::createClient(array(), array(), $jar);  //this doesn't seem to attach cookies as expected!
        $crawler = $client->request(
            'GET', //method
            '/', //uri
            array(), //parameters
            array(), //files
            array(
                'HTTP_ACCEPT_LANGUAGE' => 'en_US',
                //'HTTP_COOKIE' => 'locale2=fr' //this doesn't work either!
            ) //server
        );

        var_dump($client->getRequest());
    }
}
Run Code Online (Sandbox Code Playgroud)

phpunit symfony

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

在PHPUnit中使用流畅的界面时,如何启用严格的数据类型检查?

以下代码在运行时通过测试.我怎么能改变它,以便它抱怨123和之间的区别'123'

$obj = $this->getMockBuilder('Namespace\Object')
    ->disableOriginalConstructor()
    ->getMock();
$obj
    ->expects($this->once())
    ->method('do')
    ->with($this->equalTo('123')); // String

$obj->do(123);
Run Code Online (Sandbox Code Playgroud)

如何启用严格的数据类型检查?

php phpunit mocking

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

在symfony2中放置phpunit DB测试的最佳位置

我在Symfony2项目中的所有学说实体中进行了一些单元测试.我在UserTest类中测试setter/getter,在UserDbTest类中使用db persistance进行setter/getter测试,所有文件都在Entity测试文件夹下的文件夹中,测试两者都是一个很好的想法,以及放置这个文件的最佳位置是什么Db课程?

谢谢你的建议!

phpunit symfony doctrine-orm symfony-2.1

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

PHPUnit测试一个返回objects属性的方法

public function thisMethod {
    $example = $this->methodReturnsObject()->this1->that2->there3->id;
    return $example;
}
Run Code Online (Sandbox Code Playgroud)

你会如何在PHPUnit中测试thisMethod?

显然我可以写一个期望,即methodReturnsObject()会返回一些东西......但是什么呢?该对象具有与之关联的属性,但您如何模拟该值?

php phpunit zend-framework

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

如何测试Magento Block

我想测试一个Magento块功能.我不知道,如何调用外部.phtml文件的功能.有人知道像getModel()块一样的功能吗?

我发现

getBlockSingleton()

但是,它已被弃用,我无法让它工作.

singleton phpunit magento

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

ZF2/ZF2中的路由测试相当于Zend_Test_PHPUnit_Controller_TestCase?

到目前为止,我一直在测试我的ZF2控制器如下:

namespace Application\Controller;

use Application\Controller\IndexController;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use PHPUnit_Framework_TestCase;

class IndexControllerTest extends PHPUnit_Framework_TestCase
{
    public function testIndexActionCanBeAccessed()
    {
        $this->routeMatch->setParam('action', 'index');

        $result   = $this->controller->dispatch($this->request);
        $response = $this->controller->getResponse();

        $this->assertEquals(200, $response->getStatusCode());
        $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
    }

    protected function setUp()
    {
        \Zend\Mvc\Application::init(include 'config/application.config.php');

        $this->controller = new IndexController();
        $this->request    = new Request();
        $this->routeMatch = new RouteMatch(array('controller' => 'index'));
        $this->event      = new MvcEvent();
        $this->event->setRouteMatch($this->routeMatch);
        $this->controller->setEvent($this->event);
    }

    protected $controller = null;
    protected $event = null;
    protected $request = null;
    protected $response = null; …
Run Code Online (Sandbox Code Playgroud)

php phpunit unit-testing routes zend-framework2

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

单元测试Symfony2

我正在尝试使用Mockery来对我的sf2功能进行单元测试.我对我的第一次尝试感到不安.

首先尝试测试使用安全上下文的类:

public function setSecurityContext(SecurityContext $securityContext)
{
    $this->securityContext = $securityContext;
    try {
        $this->isLoggedIn = $securityContext->isGranted('IS_AUTHENTICATED_FULLY');
        $this->user = $securityContext->getToken()->getUser();
    } catch (\Exception $e) {
        $this->isLoggedIn = false;
        $this->user = $securityContext->getToken()->getUser();
    }
}
Run Code Online (Sandbox Code Playgroud)

我创建一个testsetSecurityContext函数,如下所示:

public function testsetSecurityContext()
{
    /* @var $securityContext SecurityContext */
    $securityContext = m::mock('Symfony\Component\Security\Core\SecurityContext');

    $securityContext->shouldReceive('isGranted')
    ->with('IS_AUTHENTICATED_FULLY')
    ->once()
    ->andReturn(true);

    $factory = m::mock('Knp\Menu\FactoryInterface');

    $menu = new MenuBuilder($factory);

    $menu->setSecurityContext($securityContext);
}
Run Code Online (Sandbox Code Playgroud)

运行单元测试时,我收到错误:

testsetSecurityContext

Mockery\Exception:方法isGranted标记为final,并且无法生成定义了此类方法的模拟对象.您应该将此对象的实例传递给Mockery以创建部分模拟.

所以我相应地改变了我的测试功能:

public function testsetSecurityContext()
{
    /* @var $securityContext SecurityContext */
    $securityContext = m::mock(new \Symfony\Component\Security\Core\SecurityContext());
    /* ... skipped ... …
Run Code Online (Sandbox Code Playgroud)

php phpunit symfony mockery

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