Skip to content
This repository has been archived by the owner on Jul 31, 2018. It is now read-only.

Commit

Permalink
Merge pull request #127 from algolia/5.4-compatibility
Browse files Browse the repository at this point in the history
fix(5.4) : make compatible with Laravel 5.4
  • Loading branch information
rayrutjes authored Jan 30, 2017
2 parents 7fc362d + c359a99 commit 8ca9de8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/EloquentSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace AlgoliaSearch\Laravel;

use Illuminate\Database\Eloquent\Model;

class EloquentSubscriber
{
private $modelHelper;
Expand All @@ -11,8 +13,10 @@ public function __construct(ModelHelper $modelHelper)
$this->modelHelper = $modelHelper;
}

public function saved($model)
public function saved($eventName, $payload = null)
{
$model = $this->getModelFromParams($eventName, $payload);

if (!$this->modelHelper->isAutoIndex($model)) {
return true;
}
Expand All @@ -29,8 +33,10 @@ public function saved($model)
return true;
}

public function deleted($model)
public function deleted($eventName, $payload = null)
{
$model = $this->getModelFromParams($eventName, $payload);

if (!$this->modelHelper->isAutoDelete($model)) {
return true;
}
Expand All @@ -43,6 +49,23 @@ public function deleted($model)
return true;
}

/**
* @param string|Model $eventName
* @param array|null $payload
*
* @return Model
*/
private function getModelFromParams($eventName, $payload = null)
{
if($eventName instanceof Model) {
// Laravel < 5.4
return $eventName;
}

// Laravel >= 5.4
return $payload[0];
}

public function subscribe($events)
{
$events->listen('eloquent.saved*', '\AlgoliaSearch\Laravel\EloquentSubscriber@saved');
Expand Down
63 changes: 63 additions & 0 deletions tests/EloquentSubscriberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace AlgoliaSearch\Tests;

use AlgoliaSearch\Laravel\EloquentSubscriber;
use AlgoliaSearch\Tests\Models\Model1;


class EloquentSubscriberTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $modelHelper;

/**
* @var EloquentSubscriber
*/
private $eloquentSubscriber;

protected function setUp()
{
$this->modelHelper = $this->getMockBuilder('AlgoliaSearch\Laravel\ModelHelper')->disableOriginalConstructor()->getMock();
$this->eloquentSubscriber = new EloquentSubscriber($this->modelHelper);
}

/**
* @dataProvider listenerDataProvider
*/
public function testSkipSyncOnSaveIfAutoSyncDisabled($eventName, $payload)
{
$this->modelHelper->expects($this->once())
->method('isAutoIndex')
->with(new Model1())
->willReturn(false);

$this->eloquentSubscriber->saved($eventName, $payload);
}

/**
* @dataProvider listenerDataProvider
*/
public function testSkipSyncOnDeleteIfAutoSyncDisabled($eventName, $payload)
{
$this->modelHelper->expects($this->once())
->method('isAutoDelete')
->with(new Model1())
->willReturn(false);

$this->eloquentSubscriber->deleted($eventName, $payload);
}

public function listenerDataProvider()
{
return [
[new Model1(), null], // Laravel 5.3
['eloquent.event', [new Model1()]], // Laravel 5.4
];
}



}

0 comments on commit 8ca9de8

Please sign in to comment.