我正在尝试用phpunit编写一个使用doctrine 2的模型进行单元测试.我想模仿教条实体,但我真的不知道如何做到这一点.任何人都可以向我解释我需要这样做吗?我正在使用Zend Framework.
需要测试的模型
class Country extends App_Model
{
public function findById($id)
{
try {
return $this->_em->find('Entities\Country', $id);
} catch (\Doctrine\ORM\ORMException $e) {
return NULL;
}
}
public function findByIso($iso)
{
try {
return $this->_em->getRepository('Entities\Country')->findOneByIso($iso);
} catch (\Doctrine\ORM\ORMException $e) {
return NULL;
}
}
}
Run Code Online (Sandbox Code Playgroud)
bootstrap.php中
protected function _initDoctrine()
{
Some configuration of doctrine
...
// Create EntityManager
$em = EntityManager::create($connectionOptions, $dcConf);
Zend_Registry::set('EntityManager', $em);
}
Run Code Online (Sandbox Code Playgroud)
扩展模型
class App_Model
{
// Doctrine 2.0 entity manager
protected $_em;
public function __construct()
{ …Run Code Online (Sandbox Code Playgroud) 我在模拟重载的__get($ index)方法时遇到了问题.要模拟的类的代码和使用它的被测系统如下:
<?php
class ToBeMocked
{
protected $vars = array();
public function __get($index)
{
if (isset($this->vars[$index])) {
return $this->vars[$index];
} else {
return NULL;
}
}
}
class SUTclass
{
protected $mocky;
public function __construct(ToBeMocked $mocky)
{
$this->mocky = $mocky;
}
public function getSnack()
{
return $this->mocky->snack;
}
}
Run Code Online (Sandbox Code Playgroud)
测试看起来如下:
<?php
class GetSnackTest extends PHPUnit_Framework_TestCase
{
protected $stub;
protected $sut;
public function setUp()
{
$mock = $this->getMockBuilder('ToBeMocked')
->setMethods(array('__get')
->getMock();
$sut = new SUTclass($mock);
}
/**
* @test
*/
public function …Run Code Online (Sandbox Code Playgroud) 我在Symfony 2上有项目,我想在Windows 7上使用PHPUNIT.
On githut phpunit is:
Composer
Simply add a dependency on phpunit/phpunit to your project's composer.json file if you use Composer to manage the dependencies of your project. Here is a minimal example of a composer.json file that just defines a development-time dependency on PHPUnit 3.7:
{
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
}
For a system-wide installation via Composer, you can run:
composer global require 'phpunit/phpunit=3.7.*'
Make sure you have ~/.composer/vendor/bin/ in your path.
Run Code Online (Sandbox Code Playgroud)
首先我使用系统范围的安装,但我不知道什么时候安装.接下来我添加到我的composer.json require-dev.这在C:/ wamp/www/myproject/vendor/symfony中安装了phpunit.接下来我尝试命令:
composer …Run Code Online (Sandbox Code Playgroud) 我有一个带有两个环境和两个配置的Laravel 5应用程序:测试(用于PHPUnit配置,内存中的数据库)和本地(我的开发配置).
即使将环境配置为local,应用程序也只会在resources/config/testing文件夹中加载配置.我可以从APP_ENV环境变量中看到同一个应用程序中的环境local.
我应该不使用测试配置目录来配置我的测试吗?
在Laravel 5中配置测试环境的更好方法是什么?
我尝试为我的应用程序编写功能测试但是我收到以下错误:
InvalidArgumentException: The option "test_case" must be set.
Run Code Online (Sandbox Code Playgroud)
我试着谷歌搜索它,但发现字面上没有线索.
我的代码:
class WhateverTest extends WebTestCase {
public function testWhatever() {
$client = static::createClient();
$crawler = $client->request('GET', '/home');
$this->assertEquals(1, 1);
}
Run Code Online (Sandbox Code Playgroud)
在phpunit.xml中设置了内核
<php>
<server name="KERNEL_DIR" value="app/" />
</php>
Run Code Online (Sandbox Code Playgroud)
我AppKernel只有两个功能:registerBundles()和registerContainerConfiguration
我用PHPUnit得到了错误的代码覆盖率报告,我相信这是XDebug的一个错误.
如何配置PHPUnit以使用其他驱动程序,即PHPDBG?
(我使用的是PHPUnit 4.7.7和PHP 5.5.12)
我试图验证php7函数只接受整数.
这是班级:
<?php
declare(strict_types=1);
class Post
{
private $id;
public function setId(int $id)
{
$this->id = $id;
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试:
<?php
declare(strict_types=1);
class PostTest extends \PHPUnit_Framework_TestCase
{
private function getPostEntity()
{
return new Post();
}
public function testSetId()
{
$valuesExpected = [123, '123a'];
foreach ($valuesExpected as $input) {
$this->getPostEntity()->setId($input);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
TypeError: Argument 1 passed to Post::setId() must be of the type integer, string given, called in /path/test/PostTest.php on line 35
有可能验证这样的错误吗?还有,运行这样的支票是否有意义?
我正在尝试phpunit在Yii2基本安装中运行php vendor/bin/phpunit,但每当我从命令行运行时,我得到此输出:
注意:我在Windows 7上.
dir=$(d=${0%[/\\]*}; cd "$d"; cd "../phpunit/phpunit" && pwd)
# See if we are running in Cygwin by checking for cygpath program
if command -v 'cygpath' >/dev/null 2>&1; then
# Cygwin paths start with /cygdrive/ which will break windows PHP,
# so we need to translate the dir path to windows format. However
# we could be using cygwin PHP which does not require this, so we
# test if the path to …Run Code Online (Sandbox Code Playgroud) 我正在测试我的代码,我的标题有问题.在每个api我使用
$headers = getallheaders();
Run Code Online (Sandbox Code Playgroud)
为了得到它,当我使用app或crhome邮递员扩展测试时,这工作正常.当我开始测试时,就像这样
$client = $this->createClient();
$client->request('GET', '/api/shotcard',
['qrcode'=>'D0m1c173'], [],
['HTTP_API_TOKEN' => 'abc123']
);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
Run Code Online (Sandbox Code Playgroud)
我尝试用带有该测试令牌的用户拍摄带有该qrcode的卡(不是我将在应用程序中使用的令牌),我在这里看到这样的呼叫:https://stackoverflow.com/a/11681422/ 5475228.测试以这种方式失败:
PHP致命错误:在第42行的/var/www/pitstop/src/AppBackendBundle/Controller/ApiController.php中调用未定义的函数AppBackendBundle\Controller\getallheaders()
如何在laravel/phpunit中测试查询执行了多长时间?
它是否有可能不依赖别的东西?
phpunit ×10
php ×6
laravel ×2
symfony ×2
unit-testing ×2
api ×1
built-in ×1
cygwin ×1
doctrine ×1
environment ×1
http-headers ×1
laravel-5 ×1
mysql ×1
overloading ×1
php-7 ×1
phpdbg ×1
testing ×1
wamp ×1
windows ×1
yii2 ×1