Skip to content

Commit

Permalink
Mini program datacube. (#655)
Browse files Browse the repository at this point in the history
* mini-program datacube.

* stats testcase.

* service provider.
  • Loading branch information
mingyoung authored and overtrue committed Apr 18, 2017
1 parent 19170b9 commit 299ca00
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use EasyWeChat\MiniProgram\Server\Guard;
use EasyWeChat\MiniProgram\Sns\Sns;
use EasyWeChat\MiniProgram\Staff\Staff;
use EasyWeChat\MiniProgram\Stats\Stats;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

Expand Down Expand Up @@ -85,6 +86,13 @@ public function register(Container $pimple)
return new Temporary($pimple['mini_program.access_token']);
};

$pimple['mini_program.stats'] = function ($pimple) {
return new Stats(
$pimple['mini_program.access_token'],
$pimple['config']['mini_program']
);
};

$pimple['mini_program.sns'] = function ($pimple) {
return new Sns(
$pimple['mini_program.access_token'],
Expand Down
1 change: 1 addition & 0 deletions src/MiniProgram/MiniProgram.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @property \EasyWeChat\MiniProgram\Staff\Staff $staff
* @property \EasyWeChat\MiniProgram\QRCode\QRCode $qrcode
* @property \EasyWeChat\MiniProgram\Material\Temporary $material_temporary
* @property \EasyWeChat\MiniProgram\Stats\Stats $stats
*/
class MiniProgram
{
Expand Down
177 changes: 177 additions & 0 deletions src/MiniProgram/Stats/Stats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

/**
* Stats.php.
*
* Part of Overtrue\WeChat.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author mingyoung <[email protected]>
* @copyright 2017
*
* @see https://github.com/overtrue
* @see http://overtrue.me
*/

namespace EasyWeChat\MiniProgram\Stats;

use EasyWeChat\MiniProgram\Core\AbstractMiniProgram;

class Stats extends AbstractMiniProgram
{
const SUMMARY_TREND = 'https://api.weixin.qq.com/datacube/getweanalysisappiddailysummarytrend';
const DAILY_VISIT_TREND = 'https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend';
const WEEKLY_VISIT_TREND = 'https://api.weixin.qq.com/datacube/getweanalysisappidweeklyvisittrend';
const MONTHLY_VISIT_TREND = 'https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyvisittrend';
const VISIT_DISTRIBUTION = 'https://api.weixin.qq.com/datacube/getweanalysisappidvisitdistribution';
const DAILY_RETAIN_INFO = 'https://api.weixin.qq.com/datacube/getweanalysisappiddailyretaininfo';
const WEEKLY_RETAIN_INFO = 'https://api.weixin.qq.com/datacube/getweanalysisappidweeklyretaininfo';
const MONTHLY_RETAIN_INFO = 'https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyretaininfo';
const VISIT_PAGE = 'https://api.weixin.qq.com/datacube/getweanalysisappidvisitpage';

/**
* Get summary trend.
*
* @param string $from
* @param string $to
*
* @return \EasyWeChat\Support\Collection
*/
public function summaryTrend($from, $to)
{
return $this->query(self::SUMMARY_TREND, $from, $to);
}

/**
* Get daily visit trend.
*
* @param string $from
* @param string $to
*
* @return \EasyWeChat\Support\Collection
*/
public function dailyVisitTrend($from, $to)
{
return $this->query(self::DAILY_VISIT_TREND, $from, $to);
}

/**
* Get weekly visit trend.
*
* @param string $from
* @param string $to
*
* @return \EasyWeChat\Support\Collection
*/
public function weeklyVisitTrend($from, $to)
{
return $this->query(self::WEEKLY_VISIT_TREND, $from, $to);
}

/**
* Get monthly visit trend.
*
* @param string $from
* @param string $to
*
* @return \EasyWeChat\Support\Collection
*/
public function monthlyVisitTrend($from, $to)
{
return $this->query(self::MONTHLY_VISIT_TREND, $from, $to);
}

/**
* Get visit distribution.
*
* @param string $from
* @param string $to
*
* @return \EasyWeChat\Support\Collection
*/
public function visitDistribution($from, $to)
{
return $this->query(self::VISIT_DISTRIBUTION, $from, $to);
}

/**
* Get daily retain info.
*
* @param string $from
* @param string $to
*
* @return \EasyWeChat\Support\Collection
*/
public function dailyRetainInfo($from, $to)
{
return $this->query(self::DAILY_RETAIN_INFO, $from, $to);
}

/**
* Get weekly retain info.
*
* @param string $from
* @param string $to
*
* @return \EasyWeChat\Support\Collection
*/
public function weeklyRetainInfo($from, $to)
{
return $this->query(self::WEEKLY_RETAIN_INFO, $from, $to);
}

/**
* Get monthly retain info.
*
* @param string $from
* @param string $to
*
* @return \EasyWeChat\Support\Collection
*/
public function montylyRetainInfo($from, $to)
{
return $this->query(self::MONTHLY_RETAIN_INFO, $from, $to);
}

/**
* Get visit page.
*
* @param string $from
* @param string $to
*
* @return \EasyWeChat\Support\Collection
*/
public function visitPage($from, $to)
{
return $this->query(self::VISIT_PAGE, $from, $to);
}

/**
* Unify query.
*
* @param string $from
* @param string $to
*
* @return \EasyWeChat\Support\Collection
*/
protected function query($api, $from, $to)
{
$params = [
'begin_date' => $from,
'end_date' => $to,
];

return $this->parseJSON('json', [$api, $params]);
}
}
130 changes: 130 additions & 0 deletions tests/MiniProgram/Stats/StatsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace EasyWeChat\tests\MiniProgram\Stats;

use EasyWeChat\Tests\TestCase;
use Mockery as m;

class StatsTest extends TestCase
{
public function getStats()
{
$stats = m::mock('EasyWeChat\MiniProgram\Stats\Stats[parseJSON]', [m::mock('EasyWeChat\MiniProgram\AccessToken'), []]);
$stats->shouldReceive('parseJSON')->andReturnUsing(function ($method, $params) {
return [
'api' => $params[0],
'params' => empty($params[1]) ? null : $params[1],
];
});

return $stats;
}

/**
* Test summaryTrend().
*/
public function testSummaryTrend()
{
$stats = $this->getStats();
$result = $stats->summaryTrend('20170313', '20170313');

$this->assertEquals('https://api.weixin.qq.com/datacube/getweanalysisappiddailysummarytrend', $result['api']);
$this->assertEquals(['begin_date' => '20170313', 'end_date' => '20170313'], $result['params']);
}

/**
* Test dailyVisitTrend().
*/
public function testDailyVisitTrend()
{
$stats = $this->getStats();
$result = $stats->dailyVisitTrend('20170313', '20170313');

$this->assertEquals('https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend', $result['api']);
$this->assertEquals(['begin_date' => '20170313', 'end_date' => '20170313'], $result['params']);
}

/**
* Test weeklyVisitTrend().
*/
public function testWeeklyVisitTrend()
{
$stats = $this->getStats();
$result = $stats->weeklyVisitTrend('20170306', '20170312');

$this->assertEquals('https://api.weixin.qq.com/datacube/getweanalysisappidweeklyvisittrend', $result['api']);
$this->assertEquals(['begin_date' => '20170306', 'end_date' => '20170312'], $result['params']);
}

/**
* Test monthlyVisitTrend().
*/
public function testMonthlyVisitTrend()
{
$stats = $this->getStats();
$result = $stats->monthlyVisitTrend('20170201', '20170228');

$this->assertEquals('https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyvisittrend', $result['api']);
$this->assertEquals(['begin_date' => '20170201', 'end_date' => '20170228'], $result['params']);
}

/**
* Test visitDistribution().
*/
public function testVisitDistribution()
{
$stats = $this->getStats();
$result = $stats->visitDistribution('20170313', '20170313');

$this->assertEquals('https://api.weixin.qq.com/datacube/getweanalysisappidvisitdistribution', $result['api']);
$this->assertEquals(['begin_date' => '20170313', 'end_date' => '20170313'], $result['params']);
}

/**
* Test dailyRetainInfo().
*/
public function testDailyRetainInfo()
{
$stats = $this->getStats();
$result = $stats->dailyRetainInfo('20170313', '20170313');

$this->assertEquals('https://api.weixin.qq.com/datacube/getweanalysisappiddailyretaininfo', $result['api']);
$this->assertEquals(['begin_date' => '20170313', 'end_date' => '20170313'], $result['params']);
}

/**
* Test weeklyRetainInfo().
*/
public function testWeeklyRetainInfo()
{
$stats = $this->getStats();
$result = $stats->weeklyRetainInfo('20170306', '20170312');

$this->assertEquals('https://api.weixin.qq.com/datacube/getweanalysisappidweeklyretaininfo', $result['api']);
$this->assertEquals(['begin_date' => '20170306', 'end_date' => '20170312'], $result['params']);
}

/**
* Test montylyRetainInfo().
*/
public function testMontylyRetainInfo()
{
$stats = $this->getStats();
$result = $stats->montylyRetainInfo('20170201', '20170228');

$this->assertEquals('https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyretaininfo', $result['api']);
$this->assertEquals(['begin_date' => '20170201', 'end_date' => '20170228'], $result['params']);
}

/**
* Test visitPage().
*/
public function testVisitPage()
{
$stats = $this->getStats();
$result = $stats->visitPage('20170313', '20170313');

$this->assertEquals('https://api.weixin.qq.com/datacube/getweanalysisappidvisitpage', $result['api']);
$this->assertEquals(['begin_date' => '20170313', 'end_date' => '20170313'], $result['params']);
}
}

0 comments on commit 299ca00

Please sign in to comment.