我正在开发一个 vagrant box,它是为达到目的而定制的。我的 PHPUnit5.2.12版本是5.2.22.
当我执行phpunit命令时,出现以下错误:
PHPUnit_Framework_Exception: PHPUnit_Framework_TestCase::$name must not be null.
Run Code Online (Sandbox Code Playgroud)
代码
下面是我的 phpunit.xml 内容:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="true"
stopOnFailure="false"
stderr="true">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">app/</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
</phpunit>
Run Code Online (Sandbox Code Playgroud) 我在 Laravel 5.2 的 route.php 文件中有我自己的方法。它有效,但是当我尝试在 phpunit 上运行测试时,出现此消息:
Fatal error: Cannot redeclare getRoutes() (previously declared in C:\(...)\ppm\app\Http\routes.php:55) in C:\(...)\ppm\app\Http\routes.php on line 76
Run Code Online (Sandbox Code Playgroud)
当我运行./vendor/bin/phpunit检查我的事件和侦听器时,存在如下错误:
致命错误:无法在 C:\xampp\htdocs\myshop\app 中重新声明 view()(之前在 C:\xampp\htdocs\myshop\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:850 中声明) \Http\helpers.php 第 92 行 PHP 致命错误:无法重新声明 view()(之前在 C:\xampp\htdocs\myshop\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:850 中声明) C:\xampp\htdocs\myshop\app\Http\helpers.php 第 92 行
我该如何解决?
我正在尝试编写一个单元测试来检查使用base_path()helper 的属性方法,但是,我收到一个异常:Call to undefined method Illuminate\Container\Container::basePath().
完整的堆栈跟踪如下:
\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:179
\app\Message.php:47
\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php:432
\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php:333
\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php:306
\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1279
\tests\Unit\MessageTest.php:59
Run Code Online (Sandbox Code Playgroud)
我已经追踪到使用setUp和tearDown固定装置 - 即使我有:
public function setUp()
{
//$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__) . "/../..";
}
public function tearDown()
{
//unset($_SERVER['DOCUMENT_ROOT']);
}
Run Code Online (Sandbox Code Playgroud)
我收到上述错误。如果我完全移除固定装置,错误就会消失。
我把它换成与给定的方法后setUpBeforeClass和tearDownAfterClass错误消失,但我想知道是什么原因造成的。
据我所知,这是 vanilla Laravel 5.4 安装(完全是 5.4.36),但它安装了额外的库(我真的不能说是什么库)。我还没有设置phpunit.xml文件,但公平地说,我不知道要寻找什么。
我已经用全新安装的 Laravel(相同版本)对其进行了测试,它确实是开箱即用的(带有未改动的 phpunit.xml 文件);在 5.5 版上没有。
我正在尝试设置一个简单的方法WebTestCase,它GET使用 Symfony 4(和"phpunit/phpunit": "^6.5")向给定的 URL 发出请求。但是,测试失败:
无法启动会话,因为标头已经由 \vendor\phpunit\phpunit\src\Util\Printer.php 发送;在第 112 行。(500 内部服务器错误)
测试类:
class ControllerTest extends WebTestCase
{
public function testGetArticles()
{
$client = static::createClient();
$client->request('GET', '/articles');
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
}
}
Run Code Online (Sandbox Code Playgroud)
控制器(包含articles路由:
/** @Route(path="articles", name="article_list") */
public function article_list(Request $request)
{
return $this->render("article/list.html.twig", array(
"articles" => $this->entityManager->getRepository("App:Article")->getArticles()
));
}
Run Code Online (Sandbox Code Playgroud)
框架.yaml:
framework:
secret: '%env(APP_SECRET)%'
csrf_protection: ~
session:
# With this config, PHP's native session handling is used
handler_id: ~
Run Code Online (Sandbox Code Playgroud)
这些测试在 Symfony 3 中运行良好,在 …
我安装了 PHPUnit,如 PHPUnit 网站上所述。最后,在执行命令时:
phpunit --version
Run Code Online (Sandbox Code Playgroud)
告诉我这个错误:
致命错误:无法使用 PHPUnit\Framework\MockObject\Stub 作为 Stub,因为该名称已在第 16 行的 phar://C:/bin/phpunit.phar/phpunit-mock-objects/Builder/InvocationMocker.php 中使用
我使用最新版本 6.5.5。尝试其他版本,5.0.0 版本正常运行!
我究竟如何为使用实时数据库的方法编写测试?
考虑这个代码:
class PricingRepository extends GenericRepository
{
public function getOptionPrice(int $productId, int $quantity, float $productPrice = 0.0): float
{
//retrieves option record for a given product
$row = $this->getMySql()->paramQuery("
select * from pricing
where product_id = ?", array(
$productId
))->getSingleArray();
//based on pricing type computes appropriate value
if ($row['pricing_type'] === 'Quantity-based')
return $row['base'] + $row['amount_per_quantity'] * $quantity;
if ($row['pricing_type'] === 'Percentage-based')
return $productPrice * $row['percentage'];
throw new \InvalidArgumentException("invalid pricing type detected");
}
}
Run Code Online (Sandbox Code Playgroud)
我有很多像上面那样的方法,所以我想确保我的测试是可靠的,并且不会在数据库数据发生变化时发生变化。我正在寻找关于一流单元测试方法的建议/解决方案,以及一种可能不依赖于数据库中数据更改的方法。
我现在编写一个简单的单元测试的方式可能是这样的:
use PHPUnit\Framework\TestCase;
class OptionPricingTest extends …Run Code Online (Sandbox Code Playgroud) 我刚刚将我的 PhpStorm 项目从 PHPUnit 4.x 升级到 PHPUnit 5.x。运行我的单元测试工作正常。
为了让“运行覆盖”工作,我必须编辑我的运行/调试配置并添加--whitelist=/path/to/whitelisted/dir到测试运行器选项。现在,当我选择“Run with coverage”时会生成覆盖率,但是我收到了这个 Xdebug 警告。
Testing started at 12:06 PM ...
/usr/local/opt/php56/bin/php -dzend_extension=/usr/local/opt/php56-
xdebug/xdebug.so -dxdebug.coverage_enable=1
/Users/bwood/code/php/WpsConsole/vendor/phpunit/phpunit/phpunit --
whitelist=/Users/bwood/code/php/WpsConsole/src --coverage-clover /Users/bwood/Library/Caches/PhpStorm2017.3/coverage/WpsConsole$NEW.coverage --no-configuration /Users/bwood/code/php/WpsConsole/tests/UnitTests --teamcity
Cannot load Xdebug - extension already loaded
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.
Run Code Online (Sandbox Code Playgroud)
我认为问题是正在调用 php
-dzend_extension=/usr/local/opt/php56-xdebug/xdebug.so
Run Code Online (Sandbox Code Playgroud)
但我不知道在哪里可以删除该选项。我正在测试控制台应用程序,因此没有网络服务器配置。
我如何要求 PHPUnit 断言一个长字符串包含两个可能的选项之一?任何一个结果都应该断言为真。像这样的东西?
$multi_kilobyte_string = "lorem ipsum...";
$option1 = "dolor";
$option2 = "amet";
$this->assertContains([$option1, $option2], $multi_kilobyte_string);
Run Code Online (Sandbox Code Playgroud) 我有以下 phpUnit 功能测试:
namespace Tests\AppBundle\Controller;
/**
* @testtype Functional
*/
class DefaultControllerTest extends BasicHttpController
{
/**
* {@inheritdoc}
*/
public function setUp()
{
$client = static::createClient();
$container = $client->getContainer();
$doctrine = $container->get('doctrine');
$entityManager = $doctrine->getManager();
$fixture = new YourFixture();
$fixture->load($entityManager);
}
/**
* {@inheritdoc}
*/
public function tearDown()
{
//Database is being destroyed here....
}
public function testIndex()
{
$client = static::createClient();
$crawler = $client->request('GET', '/');
$response=$client->getResponse();
$this->assertEquals(302, $response->getStatusCode());
$this->assertEquals('/login',$response->headers->get('Location'));
//@todo Create Dummy Users
$this->checkPanelAfterSucessfullLogin($crawler); //How I can create some …Run Code Online (Sandbox Code Playgroud) phpunit ×10
php ×7
laravel ×3
unit-testing ×3
symfony ×2
laravel-5.3 ×1
laravel-5.4 ×1
php-7 ×1
phpstorm ×1
symfony-3.4 ×1
testing ×1
vagrant ×1