From 362ee49ce4ac36b22e57a2840a3b8c49cafee9c5 Mon Sep 17 00:00:00 2001 From: Jonathan Labreuille Date: Tue, 19 Jul 2016 17:38:45 +0200 Subject: [PATCH] Let the statisticsForms model return the total signups instead of the form model - added unit test for getTotalSignups() method --- lib/Models/Form.php | 5 ----- lib/Models/StatisticsForms.php | 4 ++++ lib/Router/Forms.php | 7 ++++--- tests/unit/Models/StatisticsFormsTest.php | 14 ++++++++++++++ 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/Models/Form.php b/lib/Models/Form.php index 56a9de6913..64f0b5ad99 100644 --- a/lib/Models/Form.php +++ b/lib/Models/Form.php @@ -39,11 +39,6 @@ class Form extends Model { return parent::save(); } - function withSignups() { - $this->signups = StatisticsForms::where('form_id', $this->id)->count(); - return $this; - } - static function search($orm, $search = '') { return $orm->whereLike('name', '%'.$search.'%'); } diff --git a/lib/Models/StatisticsForms.php b/lib/Models/StatisticsForms.php index 9eda6a5740..d9c3560e2d 100644 --- a/lib/Models/StatisticsForms.php +++ b/lib/Models/StatisticsForms.php @@ -6,6 +6,10 @@ if(!defined('ABSPATH')) exit; class StatisticsForms extends Model { public static $_table = MP_STATISTICS_FORMS_TABLE; + public static function getTotalSignups($form_id = false) { + return self::where('form_id', $form_id)->count(); + } + public static function record($form_id, $subscriber_id) { if($form_id > 0 && $subscriber_id > 0) { // check if we already have a record for today diff --git a/lib/Router/Forms.php b/lib/Router/Forms.php index 31546b4346..f5f25b435a 100644 --- a/lib/Router/Forms.php +++ b/lib/Router/Forms.php @@ -1,6 +1,7 @@ $form) { - $form = $form - ->withSignups() - ->asArray(); + $form = $form->asArray(); + + $form['signups'] = StatisticsForms::getTotalSignups($form['id']); $form['segments'] = ( !empty($form['settings']['segments']) diff --git a/tests/unit/Models/StatisticsFormsTest.php b/tests/unit/Models/StatisticsFormsTest.php index cff5579a45..a2a92ae880 100644 --- a/tests/unit/Models/StatisticsFormsTest.php +++ b/tests/unit/Models/StatisticsFormsTest.php @@ -36,6 +36,20 @@ class StatisticsFormsTest extends MailPoetTest { expect($record)->false(); } + function testItCanReturnTheTotalSignupsOfAForm() { + // simulate 2 signups for form #1 + StatisticsForms::record($form_id = 1, $subscriber_id = 2); + StatisticsForms::record($form_id = 1, $subscriber_id = 1); + // simulate 1 signup for form #2 + StatisticsForms::record($form_id = 2, $subscriber_id = 2); + + $form_1_signups = StatisticsForms::getTotalSignups($form_id = 1); + expect($form_1_signups)->equals(2); + + $form_2_signups = StatisticsForms::getTotalSignups($form_id = 2); + expect($form_2_signups)->equals(1); + } + function _after() { StatisticsForms::deleteMany(); }