From 4a993719b46d48c64893b74f5aae69d4145a8021 Mon Sep 17 00:00:00 2001
From: aryapradipta9 <aryapradipta9@gmail.com>
Date: Thu, 19 Apr 2018 12:42:16 +0700
Subject: [PATCH] testing pengumuman

---
 addPengumuman.php         |  2 +-
 sendNotif.php             | 62 +++++++++++++++++++++------------------
 tests/unit/FNotifTest.php | 25 ++++++++++++++++
 3 files changed, 60 insertions(+), 29 deletions(-)
 create mode 100644 tests/unit/FNotifTest.php

diff --git a/addPengumuman.php b/addPengumuman.php
index 529d2c5..a652ed6 100755
--- a/addPengumuman.php
+++ b/addPengumuman.php
@@ -94,5 +94,5 @@ $stmt->execute();
 $stmt->close();
 $conn->close();
 
-sendNotif("Ada pengumuman baru", "Klik untuk membuka");
+\pmotraining\FirebaseNotif::sendNotif("Ada pengumuman baru", $judul, 1, 1);
 echo 'Sukses, pengumuman ' . $judul . ' berhasil ditambahkan.';
diff --git a/sendNotif.php b/sendNotif.php
index 3f9dc20..cb72de3 100755
--- a/sendNotif.php
+++ b/sendNotif.php
@@ -10,7 +10,11 @@
  * @link     http://pear.php.net/package/komunitas-pmo
  */
 
- /**
+namespace pmotraining;
+
+class FirebaseNotif
+{
+/**
   * Mengirimkan notifikasi ke firebase
   *
   * @param string $title judul notifikasi
@@ -18,31 +22,33 @@
   *
   * @return null
   */
-function sendNotif($title, $body)
-{
-    $ch = curl_init();
-    curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
-    curl_setopt($ch, CURLOPT_POST, 1);
-    $vars = [
-        "to" => "/topics/all",
-        "notification" => [
-            "title" => $title,
-            "body" => $body
-        ]
-    ];
-    // $cat = $_POST["kategori"];
-    // if ($cat == "pengumuman" ) {
-        $vars["notification"]["click_action"] = "mainActivity"; //testing
-        // $vars["data"] = array("id" => $_POST["id"]);
-    // }
-    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($vars));  //Post Fields
-    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-    $headers = [
-        'Authorization: key=AIzaSyDhcKAiH6wOm8QhHLnYZzrri8SyZZtcoe4',
-        'Content-Type: application/json'
-    ];
-    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-    $response = curl_exec($ch);
-    curl_close($ch);
-    // echo $response;
+    public static function sendNotif($title, $body, $kode, $id)
+    {
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
+        curl_setopt($ch, CURLOPT_POST, 1);
+        $vars = [
+            "to" => "/topics/all",
+            "notification" => [
+                "title" => $title,
+                "body" => $body
+            ]
+        ];
+        // $cat = $_POST["kategori"];
+        // if ($cat == "pengumuman" ) {
+            $vars["notification"]["click_action"] = "mainActivity"; //testing
+            // $vars["data"] = array("id" => $_POST["id"]);
+        // }
+        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($vars));  //Post Fields
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        $headers = [
+            'Authorization: key=AIzaSyDhcKAiH6wOm8QhHLnYZzrri8SyZZtcoe4',
+            'Content-Type: application/json'
+        ];
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+        $response = curl_exec($ch);
+        curl_close($ch);
+        return true;
+    }
 }
+ 
diff --git a/tests/unit/FNotifTest.php b/tests/unit/FNotifTest.php
new file mode 100644
index 0000000..239fc9a
--- /dev/null
+++ b/tests/unit/FNotifTest.php
@@ -0,0 +1,25 @@
+<?php
+
+require 'sendNotif.php';
+
+class FNotifTest extends \Codeception\Test\Unit
+{
+    /**
+     * @var \UnitTester
+     */
+    protected $tester;
+    
+    protected function _before()
+    {
+    }
+
+    protected function _after()
+    {
+    }
+
+    // tests
+    public function testNotification()
+    {
+        $this->assertTrue(\pmotraining\FirebaseNotif::sendNotif("Unit testing","Ini adalah testing",1,1));
+    }
+}
\ No newline at end of file
-- 
GitLab