我正在使用phpunit来运行功能测试,但我遇到了一些问题.问题是phpunit不知道JS,我有一个表单,其中包含一个需要jQuery的动态填充选择框.
所以我需要直接传递表单数据.'book'给出了以下示例:
// Directly submit a form (but using the Crawler is easier!)
$client->request('POST', '/submit', array('name' => 'Fabien'));
Run Code Online (Sandbox Code Playgroud)
当我使用这个例子时,控制器没有收到任何表单数据.最初我看到传递数组键'name'在我的情况下不正确,因为我需要在我的代码中使用'timesheet'的表单名称.所以我尝试了类似的东西:
$client->request('POST', '/timesheet/create', array('timesheet[project]' => '100'));
Run Code Online (Sandbox Code Playgroud)
但这仍然无效.在控制器中,我试图了解发生了什么以及如果收到了什么:
$postData = $request->request->get('timesheet');
$project = $postData['project'];
Run Code Online (Sandbox Code Playgroud)
这不起作用,$ project仍然是空的.但是,如果我使用以下代码,我得到了值:
$project = $request->request->get('timesheet[project]');
Run Code Online (Sandbox Code Playgroud)
但显然这不是我想要的.至少虽然我可以看到有一些POST数据.我的最后一次尝试是在测试方法中尝试以下方法:
$this->crawler = $this->client->request('POST', '/timesheet/create/', array('timesheet' => array(project => '100'));
Run Code Online (Sandbox Code Playgroud)
所以我试图传递'timesheet'数组作为请求参数数组的第一个元素.但有了这个我得到错误:
Symfony\Component\Form\Exception\UnexpectedTypeException: Expected argument of type "array", "string" given (uncaught exception) at /mnt/hgfs/pmt/src/vendor/symfony/src/Symfony/Component/Form/Form.php line 489
Run Code Online (Sandbox Code Playgroud)
如果有人可以扩展"书"中有关我应该如何工作的内容,我将非常高兴.
控制器中的表单绑定:
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
$postData = $request->request->get('timesheet');
$project = $postData['project'];
$timesheetmanager = …Run Code Online (Sandbox Code Playgroud) 我试图使用Jackson CsvParser将csv文件解析为一个对象,该对象还包含另一个类的列表.
因此前两列包含需要绑定到父类的数据,之后的数据需要绑定到另一个类.
public class Person {
private String name;
private String age;
private List<CarDetails> carDetails;
//Getters+setters
}
public class CarDetails {
private String carMake;
private String carRegistration;
//Getters+setters
}
Run Code Online (Sandbox Code Playgroud)
要解析的日志如下所示:
John Doe, 30, Honda, D32GHF
Run Code Online (Sandbox Code Playgroud)
或者在另一个拥有2辆汽车的用户日志中,它可能看起来像:
Jane Doe, 29, Mini, F64RTZ, BMW, T56DFG
Run Code Online (Sandbox Code Playgroud)
要将最初的2项数据解析为"Person"类是没有问题的.
CsvMapper mapper = new CsvMapper();
CsvSchema schema = CsvSchema.builder()
.addColumn("name")
.addColumn("age")
for(numberOfCars=2; numberOfCars!=0 ; numberOfCars--)
schema = schema.rebuild()
.addColumn("carMake")
.addColumn("carRegistration")
MappingIterator<Map.Entry> it = mapper
.reader(Person.class)
.with(schema)
.readValues(personLog);
List<Person> people = new ArrayList<Person>();
while (it.hasNextValue()) { …Run Code Online (Sandbox Code Playgroud)