Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ filter:
excluded_paths:
- "public/"
- "tests/"
- "resources/assets/js/mixpanel.js"
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ notifications:
on_success: change # options: [always|never|change] default: always
on_failure: always # options: [always|never|change] default: always
on_start: never # options: [always|never|change] default: always

7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
(This is already detected in subscription update.)
- Filter any incoming web-hook events that are in test mode.

## [WIP] - 5 Nov 2017
### Added
- unit and feature tests.

### Changed
- class structure as part of refactoring.

## [0.7.2] - 5 Nov 2017
### Fixed
- inclusion of auto-track JS scripts. NPM library is broken and seems to not be maintained anymore, switched to script provided by mixpanel setup instructions.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"require-dev": {
"fzaninotto/faker": "~1.4",
"laravel/laravel": "5.5.*",
"laravel/browser-kit-testing": "^2.0",
"mockery/mockery": "0.9.*",
"phpmd/phpmd": "^2.6",
"phpunit/phpunit": "5.7.*",
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MIXPANEL_TOKEN" value="68dffdba4c272b791a2d4883b43ccfd7"/>
</php>
</phpunit>
12 changes: 0 additions & 12 deletions src/LaravelMixpanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ class LaravelMixpanel extends Mixpanel
private $defaults;
private $request;

/**
* @param Request $request
* @param array $options
*
* @internal param Result $browser
*/
public function __construct(Request $request, array $options = [])
{
$this->defaults = [
Expand All @@ -33,12 +27,6 @@ public function __construct(Request $request, array $options = [])
);
}

/**
* @param string $event
* @param array $properties
*
* @internal param array $data
*/
public function track($event, $properties = [])
{
$browserInfo = new Browser();
Expand Down
47 changes: 0 additions & 47 deletions src/Listeners/LaravelMixpanelEventHandler.php

This file was deleted.

10 changes: 5 additions & 5 deletions src/Listeners/LaravelMixpanelUserObserver.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<?php namespace GeneaLabs\LaravelMixpanel\Listeners;

use GeneaLabs\LaravelMixpanel\Events\MixpanelEvent;
use GeneaLabs\LaravelMixpanel\Events\MixpanelEvent as Mixpanel;

class LaravelMixpanelUserObserver
{
public function created($user)
{
event(new MixpanelEvent($user, 'User: Registered'));
event(new Mixpanel($user, 'User: Registered'));
}

public function saving($user)
{
event(new MixpanelEvent($user, 'User: Updated'));
event(new Mixpanel($user, 'User: Updated'));
}

public function deleting($user)
{
event(new MixpanelEvent($user, 'User: Deactivated'));
event(new Mixpanel($user, 'User: Deactivated'));
}

public function restored($user)
{
event(new MixpanelEvent($user, 'User: Reactivated'));
event(new Mixpanel($user, 'User: Reactivated'));
}
}
12 changes: 12 additions & 0 deletions src/Listeners/Login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php namespace GeneaLabs\LaravelMixpanel\Listeners;

use GeneaLabs\LaravelMixpanel\Events\MixpanelEvent as Mixpanel;
use Illuminate\Auth\Events\Login as LoginEvent;

class Login
{
public function handle(LoginEvent $login)
{
event(new Mixpanel($login->user, 'User Logged In'));
}
}
20 changes: 20 additions & 0 deletions src/Listeners/LoginAttempt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php namespace GeneaLabs\LaravelMixpanel\Listeners;

use GeneaLabs\LaravelMixpanel\Events\MixpanelEvent as Mixpanel;
use Illuminate\Auth\Events\Attempting;

class LoginAttempt
{
public function handle(Attempting $event)
{
$email = $event->credentials['email'] ?? $event['email'] ?? '';

$authModel = config('auth.providers.users.model', config('auth.model'));
$user = app($authModel)
->where('email', $email)
->first();
$eventName = 'Login Attempted';

event(new Mixpanel($user, $eventName));
}
}
12 changes: 12 additions & 0 deletions src/Listeners/Logout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php namespace GeneaLabs\LaravelMixpanel\Listeners;

use GeneaLabs\LaravelMixpanel\Events\MixpanelEvent as Mixpanel;
use Illuminate\Auth\Events\Logout as LogoutEvent;

class Logout
{
public function handle(LogoutEvent $logout)
{
event(new Mixpanel($logout->user, 'User Logged Out'));
}
}
25 changes: 11 additions & 14 deletions src/Providers/Service.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?php namespace GeneaLabs\LaravelMixpanel\Providers;

use GeneaLabs\LaravelMixpanel\LaravelMixpanel;
use GeneaLabs\LaravelMixpanel\Listeners\LaravelMixpanelEventHandler;
use GeneaLabs\LaravelMixpanel\Listeners\Login as LoginListener;
use GeneaLabs\LaravelMixpanel\Listeners\LoginAttempt;
use GeneaLabs\LaravelMixpanel\Listeners\Logout as LogoutListener;
use GeneaLabs\LaravelMixpanel\Listeners\LaravelMixpanelUserObserver;
use GeneaLabs\LaravelMixpanel\Console\Commands\Publish;
use GeneaLabs\LaravelMixpanel\Events\MixpanelEvent;
use GeneaLabs\LaravelMixpanel\Listeners\MixpanelEvent as MixpanelEventListener;
use Illuminate\Contracts\View\View;
use Illuminate\Auth\Events\Attempting;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\HTTP\Request;
use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Support\Providers\EventServiceProvider;
Expand All @@ -15,20 +20,16 @@ class Service extends EventServiceProvider
{
protected $defer = false;
protected $listen = [
MixpanelEvent::class => [
MixpanelEventListener::class,
],
MixpanelEvent::class => [MixpanelEventListener::class],
Attempting::class => [LoginAttempt::class],
Login::class => [LoginListener::class],
Logout::class => [LogoutListener::class],
];

public function boot()
{
parent::boot();

$this->initialize();
}

protected function initialize()
{
include __DIR__ . '/../../routes/api.php';

$this->loadViewsFrom(__DIR__ . '/../../resources/views', 'genealabs-laravel-mixpanel');
Expand All @@ -39,7 +40,6 @@ protected function initialize()
if (config('services.mixpanel.enable-default-tracking')) {
$authModel = config('auth.providers.users.model') ?? config('auth.model');
$this->app->make($authModel)->observe(new LaravelMixpanelUserObserver());
app('events')->subscribe(new LaravelMixpanelEventHandler());
}
}

Expand All @@ -50,10 +50,7 @@ public function register()
$this->app->singleton('mixpanel', LaravelMixpanel::class);
}

/**
* @return array
*/
public function provides()
public function provides() : array
{
return ['genealabs-laravel-mixpanel'];
}
Expand Down
40 changes: 40 additions & 0 deletions tests/CreatesApplication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php namespace GeneaLabs\LaravelMixpanel\Tests;

use GeneaLabs\LaravelMixpanel\Providers\Service;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Application;

trait CreatesApplication
{
public function createApplication()
{
$this->copyFixtures([
__DIR__ . '/Fixtures/app/Http/Controllers/HomeController.php' => __DIR__ . '/../vendor/laravel/laravel/app/Http/Controllers/HomeController.php',
__DIR__ . '/Fixtures/resources/views/home.blade.php' => __DIR__ . '/../vendor/laravel/laravel/resources/views/home.blade.php',
__DIR__ . '/Fixtures/resources/views/auth/login.blade.php' => __DIR__ . '/../vendor/laravel/laravel/resources/views/auth/login.blade.php',
__DIR__ . '/Fixtures/resources/views/auth/register.blade.php' => __DIR__ . '/../vendor/laravel/laravel/resources/views/auth/register.blade.php',
__DIR__ . '/Fixtures/resources/views/auth/passwords/email.blade.php' => __DIR__ . '/../vendor/laravel/laravel/resources/views/auth/passwords/email.blade.php',
__DIR__ . '/Fixtures/resources/views/auth/passwords/reset.blade.php' => __DIR__ . '/../vendor/laravel/laravel/resources/views/auth/passwords/email.blade.php',
__DIR__ . '/Fixtures/resources/views/layouts/app.blade.php' => __DIR__ . '/../vendor/laravel/laravel/resources/views/layouts/app.blade.php',
__DIR__ . '/Fixtures/routes/web.php' => __DIR__ . '/../vendor/laravel/laravel/routes/web.php',
]);
$app = require __DIR__ . '/../vendor/laravel/laravel/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
$app->register(Service::class);

return $app;
}

protected function copyFixtures(array $fixtures)
{
$fixtures = collect($fixtures)->each(function ($destination, $source) {
$pathInfo = pathinfo($destination);

if (! file_exists($pathInfo['dirname'])) {
mkdir($pathInfo['dirname'], 0777, true);
}

copy($source, $destination);
});
}
}
69 changes: 69 additions & 0 deletions tests/Feature/AuthenticationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php namespace GeneaLabs\LaravelMixpanel\Tests\Feature;

use App\User;
use GeneaLabs\LaravelMixpanel\Tests\FeatureTestCase;
use GeneaLabs\LaravelMixpanel\Listeners\LoginAttempt;
use GeneaLabs\LaravelMixpanel\Events\MixpanelEvent;
use Illuminate\Auth\Events\Attempting;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Event;
use Mockery;

class AuthenticationTest extends FeatureTestCase
{
use DatabaseMigrations;

public function testLoginAttempt()
{
Event::fake([MixpanelEvent::class]);
$user = factory(User::class)->create();

$result = $this->visit('/login')
->type($user->email, 'email')
->type('hoogabaloo', 'password')
->press('Login');

$this->assertResponseStatus(200);
$result->seePageIs('/login');
Event::assertDispatched(MixpanelEvent::class, function ($event) use ($user) {
return ($event->user->email === $user->email && $event->eventName === 'Login Attempted');
});
}

public function testLoginSuccess()
{
Event::fake([MixpanelEvent::class]);
$password = 'hoogabaloo';
$user = factory(User::class)->create([
'password' => bcrypt($password),
]);

$result = $this->visit('/login')
->type($user->email, 'email')
->type($password, 'password')
->press('Login');

$this->assertResponseStatus(200);
$result->seePageIs('/home');
Event::assertDispatched(MixpanelEvent::class, function ($event) use ($user) {
return ($event->user->email === $user->email && $event->eventName === 'User Logged In');
});
}

public function testLogoutSuccess()
{
Event::fake([MixpanelEvent::class]);
$user = factory(User::class)->create();

$result = $this->actingAs($user)
->post('/logout');

$this->assertRedirectedTo('/');
Event::assertDispatched(MixpanelEvent::class, function ($event) use ($user) {
return ($event->user->email === $user->email && $event->eventName === 'User Logged Out');
});
}
}
13 changes: 13 additions & 0 deletions tests/FeatureTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php namespace GeneaLabs\LaravelMixpanel\Tests;

use GeneaLabs\LaravelMixpanel\Providers\Service;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Application;
use Laravel\BrowserKitTesting\TestCase;

abstract class FeatureTestCase extends TestCase
{
use CreatesApplication;

public $baseUrl = 'http://localhost';
}
Loading