从Laravel 5.2.15开始,$ test参数被删除,但没有明确的原因,因为Symfony的UploadedFile仍然有$ test参数.
解决方法是暂时使用Laravel 5.2.14.
Laravel自己的UploadedFile没有传递$ test参数.查看这些资源:
我知道,还有另一个问题:如何在Laravel 5.2中测试文件上传,但标记的答案对我不起作用.
我创建了一个Symfony的UploadedFile类的实例,我设置$test
为true
.我将文件发布到file/upload
.
class FileControllerTest extends TestCase
{
use \Illuminate\Foundation\Testing\DatabaseTransactions;
private $file;
public function setUp()
{
parent::setUp();
$this->file = new Symfony\Component\HttpFoundation\File\UploadedFile(
public_path() . '/examples/example.jpg',
'example.jpg',
'image/jpeg',
filesize(public_path() . '/examples/example.jpg'),
null,
true // for $test
);
}
/** @test */
public function it_uploads_a_valid_file()
{
var_dump($this->file); // $test = true
$this->call('POST', 'file/upload', [], …
Run Code Online (Sandbox Code Playgroud) 我正在使用Laravel 5.1 Testsuite.
现在,我用方法seeJson()测试我的json api.这个方法需要一个数组,例如:
->seeJson(['created' => 1]);
Run Code Online (Sandbox Code Playgroud)
但是我的json api总是返回一个json对象或带有json对象的数组,在这种情况下:
Response::json(['created' => 1], 200);
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我的json api返回:
{created: 1}
Run Code Online (Sandbox Code Playgroud)
但是seeJson()正好查找给定的数组:
[created: 1]
Run Code Online (Sandbox Code Playgroud)
我从来没有让我的测试通过匹配.我怎么配?
我正在从AngularJS前端向Laravel Back-end做一个CORS POST请求.
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:x.local
Origin:http://y.local
Pragma:no-cache
Referer:http://y.local/
Run Code Online (Sandbox Code Playgroud)
Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Methods:POST, GET, OPTIONS, PUT
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Connection:keep-alive
Content-Length:16
Content-Type:application/json
Date:Mon, 31 Oct 2016 09:22:51 GMT
Server:nginx/1.11.1
Run Code Online (Sandbox Code Playgroud)
在我获得http状态200 OK(OPTIONS请求)后,Safari挂起.我该怎么办?
我在接口和实现之间的服务提供程序中使用绑定:
public function register()
{
$this->app->bind('MyInterface', MyImplementation::class);
}
Run Code Online (Sandbox Code Playgroud)
在我的中间件中,我向请求添加了一个属性:
public function handle($request, Closure $next)
{
$request->attributes->add(['foo' => 'bar]);
return $next($request);
}
Run Code Online (Sandbox Code Playgroud)
foo
我的服务提供商public function register()
{
$this->app->bind('MyInterface', new MyImplementation($this->request->attributes->get('foo')); // Request is not available
}
Run Code Online (Sandbox Code Playgroud)
如果设置了request-> attributes-> get('foo'),我正在寻找一种"重新绑定"的技术
我是PHP和Laravel的新手.
我正在按照本教程实现自定义用户提供程序:
https://blog.georgebuckingham.com/laravel-52-auth-custom-user-providers-drivers/
我使用的是Laravel 5.3版本.
我简要解释了我的需求:我的Laravel应用程序只是一个前端应用程序,所有业务逻辑(包括用户身份验证)都由暴露REST Web服务的Java后端应用程序执行.
拨打电话:
http://localhost:8080/Extranet/login
Run Code Online (Sandbox Code Playgroud)
并通过用户名和密码作为基本身份验证我获得了一个代表已记录用户的JSON响应:
{
"userName": "Painkiller",
"email": "painkiller@gmail.com",
"enabled": true
}
Run Code Online (Sandbox Code Playgroud)
因此,在我的Laravel应用程序中,我必须执行此调用,然后解析先前返回的JSON对象,以将经过身份验证的对象生成到前端应用程序会话中.
我已经实现了上一个教程的所有4个步骤,我认为我已经正确地将用户提供程序替换为执行Web服务调用的自定义用户提供程序,这是我的代码:
它尚未完成,但它似乎工作正常,因为它提交了http:// localhost:8000 /登录页面的Laravel登录表单,它输入到retrieveByCredentials(array $ credentials)方法,并且对我的REST Web服务的调用是正确的执行.
这时候我没有使用插入的凭证,但我只是因为我很懒而且我不想在每次进行测试时在登录表单中插入用户名和密码...
阅读这个官方教程:
https://laravel.com/docs/5.3/authentication#adding-custom-user-providers
它说:
该retrieveByCredentials方法接收传递给证书的阵列验证::尝试试图注册到应用程序时的方法.然后,该方法应"查询"与这些凭证匹配的用户的基础持久存储.通常,此方法将在$ credentials ['username']上运行带有"where"条件的查询.然后该方法应返回Authenticatable的实现.此方法不应尝试进行任何密码验证或身份验证.
该validateCredentials方法应与$凭证比较给定的$用户对用户进行认证.例如,此方法应该使用Hash :: check将$ user-> getAuthPassword()的值与$ credentials ['password']的值进行比较.此方法应返回true或false,指示密码是否有效.
因此,为简单起见,retrieveByCredentials()方法返回一个模拟的User对象(实现Authenticatable接口).
这是我的代码: …
我正在使用Events Calendar Pro插件(https://theeventscalendar.com/product/wordpress-events-calendar-pro/),并且需要获取每个事件的所有类别。
我尝试过single_cat_title()
,get_the_category()
但是它们不是我所需要的。
实际上,single_cat_title()
函数仅显示第一类,并get_the_category()
返回空数组。
我想测试我的 Laravel Artisan 命令。所以我需要模拟一个对象并存根这个模拟对象的方法。在我的测试中,我无法使用真实的SFTP环境。
这是handle()
我的命令:
public function handle()
{
$sftp = new SFTP('my.sftpenv.com');
$sftp->login('foo', 'bar');
}
Run Code Online (Sandbox Code Playgroud)
我想在测试中模拟 SFTP:
$sftp = $this->createMock(SFTP::class);
$sftp->expects($this->any())->method('login')->with('foo', 'bar');
$this->artisan('import:foo');
Run Code Online (Sandbox Code Playgroud)
运行测试结果在 a 中Cannot connect to ...:22
,它来自login
的原始方法SFTP
。所以mock/stub不生效。
所以我的问题是:如何在 Laravel Artisan 命令测试中模拟对象?
我已经从 Laravel 5.0 升级到 5.1
测试套件工作正常,我可以运行 phpunit 命令。但是,当我开始使用 api 测试进行测试时,总是会遇到 foreach 错误。
class ExampleTest extends TestCase {
public function testLoginCredentials()
{
$this->post('/srv/plc/auth/login', ['data' => 'some data'])
->seeJson([
'authorized' => true,
]);
}
}
Run Code Online (Sandbox Code Playgroud)
上面看起来像文档:http : //laravel.com/docs/5.1/testing#testing-json-apis
如果我通过 phpunit 运行我的测试,我会收到以下错误:
There was 1 error:
1) ExampleTest::testBasicExample
ErrorException: Invalid argument supplied for foreach()
/Applications/XAMPP/xamppfiles/htdocs/w/server/vendor/framework/src/Illuminate/Support/Arr.php:423
/Applications/XAMPP/xamppfiles/htdocs/w/server/vendor/framework/src/Illuminate/Support/helpers.php:301
/Applications/XAMPP/xamppfiles/htdocs/w/server/vendor/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php:365
/Applications/XAMPP/xamppfiles/htdocs/whennn/server/vendor/laravel/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php:352
/Applications/XAMPP/xamppfiles/htdocs/whennn/server/tests/ExampleTest.php:17
/Applications/XAMPP/xamppfiles/lib/php/PHPUnit/TextUI/Command.php:188
/Applications/XAMPP/xamppfiles/lib/php/PHPUnit/TextUI/Command.php:126
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
Run Code Online (Sandbox Code Playgroud)
如果我使用 $this->get 执行 get 请求,则会收到相同的错误。与其他端点相同的错误。
$this->visit 工作正常。
我在Laravel 5.1中使用视图作曲家:很好.但是如何将参数传递给视图编辑器?
在我的情况下,我使用de view composer将周信息(上一个,当前,下周,包括日期)发送到我的视图.本周是可变的,不仅来自网址,还来自控制器.
public function compose(View $view)
{
// I need a parameter here (integers)
}
Run Code Online (Sandbox Code Playgroud) laravel ×6
php ×6
laravel-5 ×4
laravel-5.1 ×2
laravel-5.2 ×2
laravel-5.3 ×2
phpunit ×2
unit-testing ×2
angular ×1
api ×1
command ×1
cors ×1
file-upload ×1
javascript ×1
json ×1
tdd ×1
view ×1
wordpress ×1