小编Den*_*ebe的帖子

Laravel单元测试依赖注入

我正在尝试为购物车编写测试类.这是我有的:

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在测试期间无法解决它?

我也提到了这篇文章,但仍然没有任何运气.

php phpunit unit-testing laravel

12
推荐指数
1
解决办法
7316
查看次数

使用Queue :: fake()测试监听器

我的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 unit-testing mocking laravel

11
推荐指数
1
解决办法
2306
查看次数

解密Laravel密码重置令牌

我正在编写一个测试,确保我的应用程序的密码重置功能正常工作.密码重置系统是使用该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.

php unit-testing laravel laravel-5.3 laravel-5.4

7
推荐指数
1
解决办法
4965
查看次数

创建动态Laravel访问器

我有一个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的访问器功能?

php laravel laravel-5.5

5
推荐指数
2
解决办法
2018
查看次数

哪些HTML标记支持name属性?

是否所有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获取.

html

0
推荐指数
1
解决办法
254
查看次数

仅在字符串中删除第二个字符

我有一组字符串.它们的值可以从字符串到字符串不同.以下是三个字符串的示例:

$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_replaceltrim却无法产生我想要的结果.想法?

php

0
推荐指数
1
解决办法
1121
查看次数