小编Kia*_*ter的帖子

Laravel 事件触发,但 PHPUnit 未检测到它已触发

我正在运行测试以确保我的事件正常触发。

我的测试

/** @test */
public function foil_products_DO_trigger_oos_event ()
{
    $this->setUpTheWorld(true, 1);
    $response = $this->sendInStockData($this->pusher, Carbon::now()->subHour());

    $this->expectsEvents(\App\Events\InventoryOutOfStock::class);
    $this->sendCustomStockData($this->pusher, 0, 1, Carbon::now());

    // TEST THE RESULTS OF THE LISTENER FOR THAT EVENT
    $this->pusher = $this->pusher->fresh();
    $this->assertEquals(1, $this->pusher->oos);

    $this->assertCount(2, $this->pusher->inventories, "Pusher doesnt have 2 Inventories");
    $this->assertEquals(0, $this->pusher->latestInventory->tags_blocked);
}
Run Code Online (Sandbox Code Playgroud)

如果我注释掉该行:

    $this->expectsEvents(\App\Events\InventoryOutOfStock::class);
Run Code Online (Sandbox Code Playgroud)

那么我的测试就会通过。该行:

    $this->assertEquals(1, $this->pusher->oos);
Run Code Online (Sandbox Code Playgroud)

通过,并且只有在事件触发时才会发生这种情况。此外,我查看了我的日志文件,我可以保证该事件已触发......

我做错了什么告诉 PHPUnit 这些事件将被触发吗???

谢谢你!

事件服务提供者

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [ …
Run Code Online (Sandbox Code Playgroud)

testing events phpunit laravel laravel-5

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

Laravel - Eloquent 覆盖自定义时间戳......为什么?

我正在制作一个库存管理系统。当产品缺货时,我会在表中输入一个条目并记下带有日期/时间的“oos_at”字段。

稍后,当它重新进货时,我找到了该条目并更新了“restocked_at”时间戳字段。

但是,当我执行第二个操作时,我的“oos_at”字段被查询('updated_at')字段的时间戳覆盖......我必须在另一个实体上跟踪这个“oos_at”字段,所以它不会被覆盖,然后在更新该表时使用该字段再次更新“oos_at”字段...

下面是我的代码......

class Pusher extends Model {
/**
 *
 *
 * @param Carbon $oos_at
 * @return bool
 */
public function setAsOutOfStock(Carbon $oos_at)
{
    Log::info([
        'FILE'  => get_class($this),
        'method'   => 'setAsOutOfStock',
        'pusher'    => $this->id,
        'param:oos_at'  => $oos_at->toDateTimeString(),
    ]);

    $this->pusherOutOfStocks()->create([
        'location_id'   => $this->location_id,
        'product_id'    => $this->product_id,
        'oos_at'        => $oos_at->toDateTimeString(),
    ]);

    $this->oos = true;
    $this->oos_at = $oos_at;
    return $this->save();
}

/**
 * Clear the PusherOutOfStocks attached to $this Pusher
 * (This Pusher has been restocked!)
 *
 * @param Carbon $time
 * …
Run Code Online (Sandbox Code Playgroud)

mysql laravel eloquent

3
推荐指数
1
解决办法
1777
查看次数

标签 统计

laravel ×2

eloquent ×1

events ×1

laravel-5 ×1

mysql ×1

phpunit ×1

testing ×1