Skip to content

Commit

Permalink
Fix #25
Browse files Browse the repository at this point in the history
  • Loading branch information
ben182 committed Nov 17, 2021
1 parent 39618c3 commit 628ed68
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/AbTesting.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ public function isExperiment(string $name)
{
$this->pageView();

return $this->getExperiment()->name === $name;
if (!$experiment = $this->getExperiment()) {
return false;
}

return $experiment->name === $name;
}

/**
Expand All @@ -136,8 +140,10 @@ public function isExperiment(string $name)
*/
public function completeGoal(string $goal)
{
$this->pageView();

if (! $this->getExperiment()) {
$this->pageView();
return false;
}

$goal = $this->getExperiment()->goals->where('name', $goal)->first();
Expand Down
10 changes: 10 additions & 0 deletions tests/GoalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,14 @@ public function test_that_completed_goals_works()

$this->assertEquals($goal->pluck('id')->toArray(), AbTestingFacade::getCompletedGoals()->pluck('id')->toArray());
}

public function test_that_completeGoal_works_with_crawlers()
{
config([
'ab-testing.ignore_crawlers' => true,
]);
$_SERVER['HTTP_USER_AGENT'] = 'Googlebot';

$this->assertFalse(AbTestingFacade::completeGoal('firstGoal'));
}
}
11 changes: 11 additions & 0 deletions tests/PageViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Ben182\AbTesting\Events\ExperimentNewVisitor;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Event;
use Jaybizzle\CrawlerDetect\CrawlerDetect;

class PageViewTest extends TestCase
{
Expand Down Expand Up @@ -97,4 +98,14 @@ public function test_blade_macro()

$this->assertTrue(Blade::check('ab', 'firstExperiment'));
}

public function test_that_isExperiment_works_with_crawlers()
{
config([
'ab-testing.ignore_crawlers' => true,
]);
$_SERVER['HTTP_USER_AGENT'] = 'Googlebot';

$this->assertFalse(AbTestingFacade::isExperiment('firstExperiment'));
}
}

0 comments on commit 628ed68

Please sign in to comment.