Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Eflyax committed Nov 2, 2017
1 parent 4654cb5 commit b7c708d
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 63 deletions.
4 changes: 2 additions & 2 deletions src/FacebookPixel/FacebookPixel.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public function addToCart(
)
{
$this->sendEventToOutput(
$this->template->event = self::EVENT_ADD_TO_CART,
self::EVENT_ADD_TO_CART,
$this->prepareProductsToParameters($contentIds, $contentName, $contentCategory, $value, $currency)
);
}

public function purchase($value, $currency, $contentIds = null)
{
$this->sendEventToOutput(
$this->template->event = self::EVENT_PURCHASE,
self::EVENT_PURCHASE,
$this->prepareProductsToParameters($contentIds, null, null, $value, $currency)
);
}
Expand Down
22 changes: 9 additions & 13 deletions src/FacebookPixel/templates/facebookPixel.latte
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
<script>
!function(f,b,e,v,n,t,s){ if(f.fbq)return;n=f.fbq=function(){ n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init', {$id|noescape});
!function (f, b, e, v, n, t, s){ if(f.fbq)return;n=f.fbq=function(){ n.callMethod?n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '{$id|noescape}');
fbq('track', 'PageView');
</script>
<noscript>
<img height="1" width="1" src="https://www.facebook.com/tr?id={$id}&ev=PageView&noscript=1"/>
</noscript>

{if count($specialEvents)}
<script>
{foreach $specialEvents as $event}
{$event|noescape}
{/foreach}
</script>
<script>
{foreach $specialEvents as $event}{$event|noescape}{/foreach}
</script>
{/if}
3 changes: 2 additions & 1 deletion tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
vendor
composer.lock
temp
!temp/.gitignore
!temp/cache/.htaccess
log
!log/.htaccess
tests/_temp
tests/_log
tests/_helpers
58 changes: 50 additions & 8 deletions tests/app/presenters/HomepagePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Presenters;

use Eflyax\FacebookPixel\FacebookPixel;
use Eflyax\FacebookPixel\FacebookPixelService;
use Eflyax\FacebookPixel\IFacebookPixelFactory;
use Nette\Application\UI\Presenter;
Expand All @@ -17,41 +18,82 @@ class HomepagePresenter extends Presenter
/** @var FacebookPixelService @inject */
public $facebookPixelService;

/** @var FacebookPixel */
public $facebookPixel;

private $product;

private $currencyCode = 'CZK';

protected function startup()
{
parent::startup();
$this->facebookPixel = $this['facebookPixel'];
$product = new \stdClass();
$product->id = 1;
$product->price = 42;
$product->title = 'Product title';
$product->description = 'Product description';

$this->product = $product;
}

public function actionProductDetail()
{
$this->template->product = $this->product;
$this->template->currencyCode = 'CZK';
$this->facebookPixel->viewContent(
$this->product->id,
$this->product->title,
null,
$this->product->price,
$this->currencyCode
);
}

public function actionPurchase()
{
$this->template->totalPrice = $this->product->price + self::SHIPPING_PRICE;
$this->template->currencyCode = 'CZK';
$this->template->productIds = [$this->product->id];
$this->facebookPixelService->eventStart(FacebookPixelService::EVENT_PURCHASE);
$totalPrice = $this->product->price + self::SHIPPING_PRICE;
$this->facebookPixel->purchase($totalPrice, $this->currencyCode);
}

public function actionAddToCart()
{
// ..add product to shopping cart
$this->facebookPixelService->eventStart(FacebookPixelService::EVENT_ADD_TO_CART);
$this->facebookPixel->addToCart(
$this->product->id,
$this->product->title,
null,
$this->product->price,
$this->currencyCode
);
$this->redirect('ProductDetail');
}

public function actionAddToCartAndPurchase()
{
$totalPrice = $this->product->price + self::SHIPPING_PRICE;
$this->facebookPixel->addToCart(
$this->product->id,
$this->product->title,
null,
$this->product->price,
$this->currencyCode
);
$this->facebookPixel->purchase($totalPrice, $this->currencyCode);
}

public function actionAddToCartAndPurchaseWithRedirect()
{
$totalPrice = $this->product->price + self::SHIPPING_PRICE;
$this->facebookPixel->addToCart(
$this->product->id,
$this->product->title,
null,
$this->product->price,
$this->currencyCode
);
$this->facebookPixel->purchase($totalPrice, $this->currencyCode);
$this->redirect('Homepage:');
}

protected function createComponentFacebookPixel()
{
return $this->IFacebookPixelFactory->create();
Expand Down
9 changes: 5 additions & 4 deletions tests/app/templates/@layout.latte
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<!doctype html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="utf-8">
<title></title>
</head>

<body>

{control facebookPixel}

<div class="page-content">
{block content}
{block content}{/block}
</div>

{control facebookPixel}

</body>
</html>
1 change: 1 addition & 0 deletions tests/app/templates/Homepage/addToCartAndPurchase.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{block content}
16 changes: 0 additions & 16 deletions tests/app/templates/Homepage/productDetail.latte
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
{block content}

{control facebookPixel:viewContent,
$product->id,
$product->title,
$product->description,
$product->price,
$currencyCode
}

{control facebookPixel:addToCart,
$product->id,
$product->title,
$product->description,
$product->price,
$currencyCode
}

<a n:href="addToCart">Add to cart</a>
6 changes: 0 additions & 6 deletions tests/app/templates/Homepage/purchase.latte
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
{block content}

{control facebookPixel:purchase,
$totalPrice,
$currencyCode,
$productIds
}
37 changes: 24 additions & 13 deletions tests/tests/functional/src/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class EventTest extends BasePresenterTest
public function testPageView()
{
$this->checkUrlAndResponse($this->generateLink('Homepage:'));
$this->tester->see('fbq(\'init\', ' . self::FB_PIXEL_ID . ')');
$this->tester->see('fbq(\'track\', \'PageView\')');
$this->tester->see("'init', '" . self::FB_PIXEL_ID . "'");
$this->tester->see("'track', 'PageView'");
$this->tester->seeElement('img', [
'src' => 'https://www.facebook.com/tr?id=' . self::FB_PIXEL_ID . '&ev=PageView&noscript=1'
]);
Expand All @@ -18,10 +18,10 @@ public function testPageView()
public function testViewContent()
{
$this->checkUrlAndResponse($this->generateLink('Homepage:productDetail'));
$this->tester->see('\'track\', "ViewContent"');
$this->tester->see("'track', 'ViewContent'");
$this->checkProduct();
// we didn't call start event AddToCart before
$this->tester->dontSee('\'track\', "AddToCart"');
// // we didn't call start event AddToCart before
$this->tester->dontSee("'track', 'AddToCart'");
}

public function testAddToCart()
Expand All @@ -30,24 +30,35 @@ public function testAddToCart()
$this->generateLink('Homepage:addToCart'),
$this->generateLink('Homepage:productDetail')
);
$this->tester->see('\'track\', "AddToCart"');
$this->tester->see("'track', 'AddToCart'");
$this->checkProduct();
}

public function testPurchase()
{
$this->checkUrlAndResponse($this->generateLink('Homepage:purchase'));
$this->tester->see('\'track\', "Purchase"');
$this->tester->see("'track', 'Purchase'");
}

public function testMultipleEvents()
{
$this->checkUrlAndResponse($this->generateLink('Homepage:addToCartAndPurchase'));
}

public function testMultipleEventsWithRedirect()
{
$this->checkUrlAndResponse(
$this->generateLink('Homepage:addToCartAndPurchaseWithRedirect'),
$this->generateLink('Homepage:')
);
}

private function checkProduct()
{
$this->tester->see('"content_type":"product"');
$this->tester->see('"content_ids":["1"]');
$this->tester->see('"content_name":"Product title"');
$this->tester->see('"content_category":"Product description"');
$this->tester->see('"value":"42.00"');
$this->tester->see('"currency":"\'CZK\'"');
$this->tester->see("'content_type':'product'");
$this->tester->see("'content_ids':['1']");
$this->tester->see("'content_name':'Product title'");
$this->tester->see("'currency':'CZK'");
}

}

0 comments on commit b7c708d

Please sign in to comment.