我正在构建一个小型 Laravel 应用程序,我想使用 phpunit 对其进行测试。是一个非常简单的应用程序,只有一个控制器:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
class MyController extends Controller
{
protected $client;
public function __construct(Client $client)
{
$this->client = $client;
}
public function doSomething(Request $request)
{
...
$this->client->post(...);
...
}
}
Run Code Online (Sandbox Code Playgroud)
因此,正如您所看到的,我正在控制器中传递 Guzzle 对象(我的想法是用模拟处理程序替换该客户端。我想测试函数 doSomething 是否正在执行我期望的操作,但我不知道怎么称呼。
我可以轻松地(在我的 MyControllerTest 中)执行以下操作:
$controller = new MyController($fakeGuzzleObject);
Run Code Online (Sandbox Code Playgroud)
但是,我如何调用 doSomething 并传递一个 Request 实例?
或者,我可以执行以下操作:
$this->get(route/to/access/doSomething)
Run Code Online (Sandbox Code Playgroud)
让 Laravel 注入请求,但我如何告诉它使用模拟的 Guzzle 实例?
我正在开发我的第一个 RESTful api,(不幸的是)这是在一个已经存在的系统上完成的,这个想法是允许第三方访问这个系统。
一切都很好,直到我意识到我有多种方法可以访问相同的资源。我将尝试使用系统的一部分来解释它。该系统是使用 Laravel 5.8 构建的,其中包括以下数据库表:
每个用户可以拥有多封电子邮件,每封电子邮件只属于一个用户。这同样适用于文本消息。
我已经“忽略”了当前的所有代码,因为它没有以正确的方式构建以使其成为 RESTful api,所以我创建了一个新文件夹Api,我的所有代码都在那里。
我认为有以下端点是有意义的
/api/v1/users
/api/v1/users/1
/api/v1/users/1/emails
/api/v1/users/1/emails/1
/api/v1/users/1/sms
/api/v1/users/1/sms/1
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我可以获得用户列表、获取用户的所有详细信息、获取电子邮件/短信列表以及特定电子邮件/短信的所有详细信息。但是,其中一项要求是拥有一个包含电子邮件/短信列表的页面,因此拥有以下内容开始变得有意义:
/api/v1/emails
/api/v1/emails/1
/api/v1/sms
/api/v1/sms/1
Run Code Online (Sandbox Code Playgroud)
为了避免有 2 个端点来获取相同的资源(/api/v1/users/1/emails/1并/api/v1/emails/1返回 ID 为 1 的电子邮件),我正在考虑摆脱深层端点/api/v1/users/1/emails并将它们更改为类似/api/v1/emails?user_id=1.
这是否违反了 RESTful 原则?我无法就让 2 个端点访问同一资源的研究得出结论,但“感觉”不对。另一方面, have/api/v1/emails?user_id=1可能会引起一些安全/隐私问题(例如,我需要确保用户 1 只能访问/api/v1/emails?user_id=1而不能访问/api/v1/emails?user_id=2),但似乎更灵活,因为我可以单独使用它来获取所有资源或与user_id 过滤器以仅获取特定资源。
这种情况有约定吗?
这可能是我见过的最奇怪的情况。基本上我有一个在 Laravel 5.1 中构建的系统,它需要向外部系统发出请求。问题是,有时它有效,但有时我得到
GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host: hosthere
Run Code Online (Sandbox Code Playgroud)
就代码而言,绝对没有任何变化。我使用完全相同的参数运行应用程序,有时会收到错误,有时会得到正确的响应。
有任何想法吗?
提前致谢
编辑2 当我执行 nslookup domainthatineedtouse.com 时,我得到
;; Got SERVFAIL reply from 8.8.8.8, trying next server
Server: 8.8.4.4
Address: 8.8.4.4#53
Non-authoritative answer:
Name: domainthatineedtouse.com
Address: therealipaddressishere
Run Code Online (Sandbox Code Playgroud)
这可以与问题相关吗?
编辑 这是建立连接的类的一部分。
<?php
use GuzzleHttp\ClientInterface;
class MyClass
{
const ENDPOINT = 'http://example.com/v1/endpoint';
/**
* @var \GuzzleHttp\ClientInterface
*/
protected $client;
/**
* @var string The api key used to connect to the service
*/
protected $apiKey;
/**
* Constructor.
* …Run Code Online (Sandbox Code Playgroud) 我有一个资源控制器(包含所有操作:索引,创建,存储,显示,编辑,更新和销毁),我想知道编辑单个字段列的最佳方法是什么?
假设我们有一个带有名称,电子邮件,密码和活动的Users表(active是一个小的int 0或1).
在用户管理页面中,有一个用于激活/停用用户的按钮(向服务器发出请求以更新所选用户的"活动"字段).
我应该在Controller中创建一个新方法updateStatus,还是有办法使用update方法处理它?
在更新"活动"列时,我不想错误地允许名称,电子邮件或密码中的空值,因此我需要保留验证规则(简而言之,所有字段都是必需的),但这意味着更新时在"活动"字段中,我需要传递请求中的所有用户数据.
在这一点上,我很困惑,所有的帮助将不胜感激.
提前致谢!
我正在尝试使用SweetAlert2,但在出现问题时无法显示错误消息。此外,当用户按下“取消”按钮时,控制台会显示错误:未捕获(承诺)取消。
简而言之,我的网站上有一个按钮。如果用户按下该按钮,则会出现确认对话框(使用 SweetAlert2)。这是我的代码:
swal({
title: "Are you sure?",
text: "You will not be able to undo this action!",
type: "warning",
showCancelButton: true,
cancelButtonText: 'No, cancel!',
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, do it!",
allowOutsideClick: false
preConfirm: function () {
return axios.put('/api/endpoint')
.then(response => {
console.log('success')
})
.catch(error => {
console.log('failure')
swal({
title: "Something went wrong",
type: "error",
})
});
}
}).then(function (result) {
if (result.value) {
swal('Deleted!', 'Your file has been deleted.', 'success')
} else if (result.dismiss === 'cancel') { …Run Code Online (Sandbox Code Playgroud) 我正在写一个Laravel包,但我遇到了问题.该程序包调度执行以下操作的作业:
class ExampleJob
{
protected $exampleProperty;
function __construct($parameter)
{
$this->exampleProperty = $parameter;
}
}
Run Code Online (Sandbox Code Playgroud)
我需要测试是否使用正确的$参数调度此作业(此值是从数据库中检索的,并且根据情况,它将是不同的值).
根据文档,Laravel允许这样做:
Bus::assertDispatched(ShipOrder::class, function ($job) use ($order) {
return $job->order->id === $order->id;
});
Run Code Online (Sandbox Code Playgroud)
但这意味着$ order属性需要公开(在我的情况下,我有:protected $ exampleProperty;).
这是一个好习惯吗?我的意思是将类属性声明为公共属性?在OOP中封装的概念怎么样?
有什么想法吗?
php ×5
laravel ×4
laravel-5 ×2
phpunit ×2
rest ×2
curl ×1
javascript ×1
laravel-5.1 ×1
promise ×1
sweetalert2 ×1
unit-testing ×1