遗嘱,我在应用程序(又名项目)目录中。工匠文件在我所在的目录中。
接下来...如果我运行以下命令,我会得到输出(可用命令的列表):
php工匠列表
但是如果我运行这个命令(故意省略一个必需的参数),虽然我希望有一个错误,但我不会得到任何错误:
php工匠制作:命令
artisan 鉴于这个故意不完整的命令导致:没有输出,它的配置似乎不满意。
我尝试过的事情
find -L ./ -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l | grep "Errors parsing"没有发现有语法错误的文件composer updatecomposer installphp artisan optimize 请提供会引起php artisan bad:command投诉的解决方案。
大家好,我需要测试一段调用另一个我现在无法编辑的类的函数的代码。
我只需要测试它,但问题是这个函数有一个通过引用传递的值和一个返回值,所以我不知道如何模拟它。
这是列类的功能:
public function functionWithValuePassedByReference(&$matches = null)
{
$regex = 'my regex';
return ($matches === null) ? preg_match($regex, $this->field) : preg_match($regex, $this->field, $matches);
}
Run Code Online (Sandbox Code Playgroud)
这是被调用和我需要模拟的地方:
$matches = [];
if ($column->functionWithValuePassedByReference($matches)) {
if (strtolower($matches['parameters']) == 'distinct') {
//my code
}
}
Run Code Online (Sandbox Code Playgroud)
所以我试过了
$this->columnMock = $this->createMock(Column::class);
$this->columnMock
->method('functionWithValuePassedByReference')
->willReturn(true);
Run Code Online (Sandbox Code Playgroud)
如果我这样做会返回错误,索引parameters显然不存在,所以我试过这个:
$this->columnMock = $this->createMock(Column::class);
$this->columnMock
->method('functionWithValuePassedByReference')
->with([])
->willReturn(true);
Run Code Online (Sandbox Code Playgroud)
但是同样的错误,我如何模拟该功能?
谢谢
我第一次尝试对我的项目使用单元测试,但是我被一个断言模型正确存储的测试阻止了。
这是我要测试的 API 控制器:
public function store(QuestionFormRequest $request)
{
$questionRequest = $request->question();
$question = new Question($questionRequest);
$question->save();
$question->answers()->createMany($questionRequest['answers']);
return response()->json($question->load('answers'), 201);
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试:
public function it_can_store_a_question()
{
$surveyFactory = factory(Survey::class)->create();
$themeFactory = factory(Theme::class)->create();
$pillarFactory = factory(Pillar::class)->create();
$questionToStore = [
'survey_id' => $surveyFactory->id,
'theme_id' => $themeFactory->id,
'pillar_id' => $pillarFactory->id,
'name' => 'question',
'type' => 'simple',
'answers' => [
[
'label' => 'reponse1',
'points' => '3',
],
[
'label' => 'reponse2',
'points' => '5',
]
]
];
$response = $this->post('/api/1.0/question', $questionToStore);
$response->assertStatus(201); …Run Code Online (Sandbox Code Playgroud) 在测试控制器时是否有内置方法可以完全跳过授权?
示例控制器:
public function changePassword(Request $request, LdapInterface $ldap)
{
$this->authorize('change-password');
$this->validate($request, [
'pass' => 'min:8|confirmed|weakpass|required',
]);
$success = $ldap->updatePassword($request->get('pass'));
$message = $success ?
'Your e-mail password has been successfully changed' :
'An error occured while trying to change your alumni e-mail password.';
return response()->json(['message' => $message]);
}
Run Code Online (Sandbox Code Playgroud)
我想跳过change-password规则,它是在里面定义的AuthServiceProvider:
public function boot(GateContract $gate)
{
$gate->define('change-password', function ($user) {
// Some complex logic here
});
}
Run Code Online (Sandbox Code Playgroud)
我不想添加smt。就像if (env('APP_ENV') == 'testing') return;在代码里面一样。
我在测试 Laravel 5.5 时遇到问题。我需要在 TEST HEADER 中发送一个不记名令牌,但不起作用
public function testAuthCheckinvalidToken()
{
$response = $this->withHeaders([
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->token,
])->json('GET', 'auth/check');
...
}
Run Code Online (Sandbox Code Playgroud)
当我 dd($response) 时,只设置了默认的 HEADERS:
#headers: array:5 [
"cache-control" => array:1 [
0 => "no-cache, private"
]
"date" => array:1 [
0 => "Tue, 21 Nov 2017 18:48:27 GMT"
]
"content-type" => array:1 [
0 => "application/json"
]
"x-ratelimit-limit" => array:1 [
0 => 60
]
"x-ratelimit-remaining" => array:1 [
0 …Run Code Online (Sandbox Code Playgroud) phpunit laravel-5 laravel-request laravel-response laravel-5.5
在 PhpUnit 5 中,我们能够设置预期的类名,然后检查它的错误消息
$this->setExpectedException('Cake\Network\Exception\NotFoundException');
$this->assertEquals('Not Found', $this->_exception->getMessage());
Run Code Online (Sandbox Code Playgroud)
如何在 PhpUnit 6 中做同样的事情?
我已经实现了一个简单的测试来检查页面是否显示在我的应用程序中:
namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class MyControllerTest extends WebTestCase
{
public function testMyAction()
{
$client = static::createClient();
$client->request('GET', '/');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行 php vendor/phpunit/phpunit/phpunit 时,我收到以下错误消息:
1) App\Tests\Controller\MyControllerTest::testMyAction
Symfony\Component\DependencyInjection\Exception\EnvNotFoundException:
Environment variable not found: "DATABASE_URL".
Run Code Online (Sandbox Code Playgroud)
然而,这个变量在.env. 我该如何解决这个问题?
我创建了一个单元测试并使用以下命令来运行测试:
bin/phpunit -c path/to/DocumentDuplicateControllerTest.php
Run Code Online (Sandbox Code Playgroud)
...我发现我得到了这个输出:
ParsePI:PI php 永无止境......
应为开始标记,未找到“<”
不幸的是,关闭该-c标志不是一种选择,因为该命令在 Jenkins 作业期间使用该标志运行。
有人可以就如何解决这个问题提出建议吗?
如果我这样做:
$obj = factory(Object::class)->make();
collect($obj);
Run Code Online (Sandbox Code Playgroud)
我返回了一个类型的集合:
Illuminate\Support\Collection
Run Code Online (Sandbox Code Playgroud)
Laravel 还允许您使用特定方法定义自己的集合。在模型中,您执行以下操作:
public function newCollection(array $models = [])
{
return new CustomCollection($models);
}
Run Code Online (Sandbox Code Playgroud)
你会CustomCollection在文件 CustomCollection.php 中创建你的,像这样开始:
class CustomCollection extends Collection
{
Run Code Online (Sandbox Code Playgroud)
我想知道如何返回类型的集合:
Illuminate\Database\Eloquent\Collection
Run Code Online (Sandbox Code Playgroud)
或者,在创建我自己的自定义集合的情况下,我怎么能返回一个类型的集合:
App\Models\CustomCollection
Run Code Online (Sandbox Code Playgroud)
我想用 collect()帮助程序或任何其他方式因为我不会出于编写 PHPUnit 测试的目的访问数据库。
==========
编辑:我factory(Object::class)->make()用来欺骗我试图放入集合的 Eloquent 对象。
如果您只是这样做factory(Object::class, 1)->make(),工厂会Object::class为您将 的单个实例滚动到 Eloquent 集合中。
我已经使用 Laravel 5.7 有一段时间了,但是,我对 TDD 完全陌生。
如果创建了用户模型,register则会触发一个事件。但是为什么当我使用工厂创建用户模型时它没有被触发?
我的工厂:
$factory->define(App\User::class, function (Faker $faker) {
return [
'first_name' => $faker->name,
'sur_name' => $faker->name,
'phone' => $faker->phoneNumber,
'birthday' => Carbon::now()->subYears(25)->toDateTimeString(),
'gender' => 'm',
'email' => $faker->unique()->safeEmail,
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'remember_token' => str_random(10),
];
});
Run Code Online (Sandbox Code Playgroud)
和我的测试,失败了:
public function test()
{
$this->withoutExceptionHandling();
Event::fake();
$user = factory(User::class)->create();
Event::assertDispatched(Registered::class);
}
Run Code Online (Sandbox Code Playgroud) phpunit ×10
php ×4
laravel ×3
laravel-5 ×3
unit-testing ×2
json ×1
laravel-5.5 ×1
symfony ×1
xml ×1