我正在尝试为购物车编写测试类.这是我有的:
ShoppingCartTest.php
class ShoppingCartTest extends TestCase {
use DatabaseTransactions;
protected $shoppingCart;
public function __construct() {
$this->shoppingCart = resolve('App\Classes\Billing\ShoppingCart');
}
/** @test */
public function a_product_can_be_added_to_and_retrieved_from_the_shopping_cart() {
// just a placeholder at the moment
$this->assertTrue(true);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行phpunit时,似乎Laravel无法解析我的ShoppingCartClass.
这是错误:
Fatal error: Uncaught exception 'Illuminate\Contracts\Container\BindingResolutionException'
with message 'Unresolvable dependency resolving
[Parameter #0 [ <required> $app ]] in class Illuminate\Support\Manager'
in C:\Development Server\EasyPHP-Devserver-16.1\eds-www\nrponline\vendor\laravel\framework\src\Illuminate\Container\Container.php:850
Run Code Online (Sandbox Code Playgroud)
我让我的ShoppingCart类在很多不同的控制器中得到解决.
为什么Laravel在测试期间无法解决它?
我也提到了这篇文章,但仍然没有任何运气.
我的Laravel 5.5应用程序有一个Product
模型.该Product
模型具有如下dispatchesEvents
属性:
/**
* The event map for the model.
*
* @var array
*/
protected $dispatchesEvents = [
'created' => ProductCreated::class,
'updated' => ProductUpdated::class,
'deleted' => ProductDeleted::class
];
Run Code Online (Sandbox Code Playgroud)
我也有一个被调用的监听器,CreateProductInMagento
它被映射到该ProductCreated
事件中EventServiceProvider
.该侦听器实现了该ShouldQueue
接口.
创建产品时,将ProductCreated
触发事件并将CreateProductInMagento
侦听器推送到队列并运行.
我现在正在尝试为所有这些编写测试.这是我有的:
/** @test */
public function a_created_product_is_pushed_to_the_queue_so_it_can_be_added_to_magento()
{
Queue::fake();
factory(Product::class)->create();
Queue::assertPushed(CreateProductInMagento::class);
}
Run Code Online (Sandbox Code Playgroud)
但是我收到一条The expected [App\Listeners\Magento\Product\CreateProductInMagento] job was not pushed.
错误消息.
如何使用Laravel Queue::fake()
方法测试可队列侦听器?
我正在编写一个测试,确保我的应用程序的密码重置功能正常工作.密码重置系统是使用该php artisan make:auth
命令创建的.为了使测试通过我需要自动化的GET请求来/password/reset/{$token}
这里$token
是存储在值password_resets
表.Laravel像这样存储令牌:
$2y$10$9grKb3c6.Toiv0kjUWbCUeT8Q8D.Fg2gZ/xDLGQUAkmdyHigmRkNW
但是当Laravel将密码重置电子邮件发送给用户时,重置令牌在电子邮件中如下所示:
382aa64567ecd05a774c2e4ebb199d3340a1424300707053354c749c10487594
.
/password/reset/$2y$10$9grKb3c6.Toiv0kjUWbCUeT8Q8D.Fg2gZ/xDLGQUAkmdyHigmRkNW
由于重置令牌中的正斜杠,我的GET请求失败.(紧跟'g2gZ'之后)
我尝试使用辅助功能,decrypt()
但没有运气.
如何转换从password_resets
表中提取的密码重置令牌以匹配Laravel发送给用户的内容?
不确定这是否相关,但我确实将我的应用程序从5.3升级到5.4.
我有一个Product
模型,也有一个Attribute
模型.Product
和之间的关系Attribute
很多.在我的Product
模型上,我正在尝试创建一个动态访问器.我熟悉Laravel的访问器和mutator功能,如此处所述.我遇到的问题是每次创建产品属性时我都不想创建访问器.
例如,产品可能具有颜色属性,可以这样设置:
/**
* Get the product's color.
*
* @param string $value
* @return string
*/
public function getColorAttribute($value)
{
foreach ($this->productAttributes as $attribute) {
if ($attribute->code === 'color') {
return $attribute->pivot->value;
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
然后可以像这样访问产品的颜色$product->color
.如果我在产品中添加size
属性,我需要在Product
模型上设置另一个访问者,以便我可以像这样访问它$product->size
.
有没有办法设置一个"动态"访问器来处理作为属性访问时的所有属性?
我是否需要使用自己的覆盖Laravel的访问器功能?
是否所有HTML标记都支持name属性,或者只有少数人可以使用name属性?此外,所有标签都支持title属性吗?
我问的原因是因为我需要在这些属性中存储有关当前元素的信息.这是一个例子:
<img src="example-image.jpg" alt="Example Image" title="Additional Information" name="Even more info"/>
<div class="example-word" title="Information about this tag" name="More information about this tag">Word</div>
我存储在属性中的这些附加信息将通过javascript获取.
我有一组字符串.它们的值可以从字符串到字符串不同.以下是三个字符串的示例:
$string1 = "505";
$string2 = "500";
$string3 = "601";
Run Code Online (Sandbox Code Playgroud)
我需要删除第二个0只产生:
$string1 = "55";
$string2 = "50";
$string3 = "61";
Run Code Online (Sandbox Code Playgroud)
我曾尝试substr
,str_replace
而ltrim
却无法产生我想要的结果.想法?
php ×5
laravel ×4
unit-testing ×3
html ×1
laravel-5.3 ×1
laravel-5.4 ×1
laravel-5.5 ×1
mocking ×1
phpunit ×1