summaryrefslogtreecommitdiff
path: root/lib/codebird-php/test/requestparse_tests.php
blob: 279fdda22cde37934bce6e19e3449be876a7ac50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php

namespace Codebird;
require_once ('test/codebirdm.php');

/**
 * A Twitter library in PHP.
 *
 * @package   codebird-test
 * @author    Jublo Solutions <support@jublo.net>
 * @copyright 2010-2016 Jublo Solutions <support@jublo.net>
 * @license   https://opensource.org/licenses/GPL-3.0 GNU General Public License 3.0
 * @link      https://github.com/jublonet/codebird-php
 */

/**
 * Request parsing tests
 *
 * @package codebird-test
 */
class Requestparse_Test extends \PHPUnit_Framework_TestCase
{
  /**
   * Initialise Codebird class
   *
   * @return \Codebird\Codebird The Codebird class
   */
  protected function getCB()
  {
    Codebird::setConsumerKey('123', '456');
    $cb = new CodebirdM();
    $cb->setToken('234', '567');

    return $cb;
  }

  /**
   * Tests _parseApiParams
   */
  public function testParseApiParams()
  {
    $cb = $this->getCB();
    // empty list
    $this->assertEquals([], $cb->call('_parseApiParams', [[]]));
    // arrays
    $this->assertEquals(['test' => 1], $cb->call('_parseApiParams', [[['test' => 1]]]));
    $this->assertEquals(
      ['media[]' => '123'],
      $cb->call('_parseApiParams', [[['media[]' => 123]]])
    );
    // urlencoded strings
    $this->assertEquals(['testdata' => ''], $cb->call('_parseApiParams', [['testdata']]));
    $this->assertEquals(
      ['param1' => '12', 'param2' => 'ab'],
      $cb->call('_parseApiParams', [['param1=12&param2=ab']])
    );
    $this->assertEquals(
      ['media' => ['123', '456']],
      $cb->call('_parseApiParams', [['media[]=123&media[]=456']])
    );
  }

  /**
   * Tests _stringifyNullBoolParams
   */
  public function testStringifyNullBoolParams()
  {
    $cb = $this->getCB();
    $result = $cb->call(
        '_stringifyNullBoolParams',
        [['a' => 123, 'b' => null, 'c' => true, 'd' => false, 'e' => 'x']]
      );
    $this->assertEquals('123', $result['a']);
    $this->assertNull($result['b']);
    $this->assertEquals('true', $result['c']);
    $this->assertEquals('false', $result['d']);
  }

  /**
   * Tests _mapFnToApiMethod
   */
  public function testMapFnToApiMethod()
  {
    $cb = $this->getCB();
    $apiparams = [
      'test' => 1,
      'account_id' => '1234'
    ];
    $result = $cb->call(
        '_mapFnToApiMethod',
        'ads_accounts_ACCOUNT_ID_cards_appDownload',
        $apiparams
      );
    $this->assertEquals([
      'ads/accounts/1234/cards/app_download',
      'ads/accounts/:account_id/cards/app_download'
    ], $result);
    // check that inline parameter was removed from array
    $this->assertArrayNotHasKey('account_id', $apiparams);
  }

  /**
   * Tests _mapFnInsertSlashes
   */
  public function testMapFnInsertSlashes()
  {
    $cb = $this->getCB();
    $result = $cb->call(
        '_mapFnInsertSlashes',
        ['ads_accounts_ACCOUNT_ID_cards_appDownload']
      );
    $this->assertEquals(
      'ads/accounts/ACCOUNT/ID/cards/appDownload',
      $result
    );
  }

  /**
   * Tests _mapFnRestoreParamUnderscores
   */
  public function testMapFnRestoreParamUnderscores()
  {
    $cb = $this->getCB();
    $params_underscore = [
      'screen_name', 'place_id',
      'account_id', 'campaign_id', 'card_id', 'line_item_id',
      'tweet_id', 'web_event_tag_id'
    ];
    $params_slash = [];
    foreach ($params_underscore as $param) {
      $params_slash[] = str_replace('_', '/', $param);
    }
    for ($i = 0; $i < count($params_underscore); $i++) {
      $result = $cb->call(
          '_mapFnRestoreParamUnderscores',
          ['ads/accounts/' . strtoupper($params_slash[$i]) . '/cards/appDownload']
        );
      $this->assertEquals(
        'ads/accounts/' . strtoupper($params_underscore[$i]) . '/cards/appDownload',
        $result
      );
    }
  }

  /**
   * Tests _mapFnInlineParams
   */
  public function testMapFnInlineParams()
  {
    $cb = $this->getCB();
    // normal parameters
    $apiparams = [
      'test' => 1,
      'account_id' => '1234'
    ];
    $result = $cb->call(
        '_mapFnInlineParams',
        'ads/accounts/ACCOUNT_ID/cards/app_download',
        $apiparams
      );
    $this->assertEquals([
        'ads/accounts/1234/cards/app_download',
        'ads/accounts/:account_id/cards/app_download'
      ],
      $result
    );
    // check that inline parameter was removed from array
    $this->assertArrayNotHasKey('account_id', $apiparams);

    // special parameters (TON API)
    $apiparams = [
      'test'     => 1,
      'bucket'   => 'ta_partner',
      'file'     => 'test_Ab.mp4',
      'resumeId' => '56789'
    ];
    $result = $cb->call(
        '_mapFnInlineParams',
        'ton/bucket/BUCKET/FILE?resumable=true&resumeId=RESUMEID',
        $apiparams
      );
    $this->assertEquals([
        'ton/bucket/ta_partner/test_Ab.mp4?resumable=true&resumeId=56789',
        'ton/bucket/:bucket/:file?resumable=true&resumeId=:resumeId'
      ],
      $result
    );
    $this->assertArrayNotHasKey('bucket', $apiparams);
    $this->assertArrayNotHasKey('file', $apiparams);
    $this->assertArrayNotHasKey('resumeId', $apiparams);
    $this->assertEquals(['test' => 1], $apiparams);
  }

  /**
   * Tests _json_decode
   */
  public function testJsonDecode()
  {
    $json  = '{"id": 123456789123456789, "id_str": "123456789123456789"}';
    $array = [
      'id' => 123456789123456789,
      'id_str' => '123456789123456789'
    ];
    $object = (object) $array;

    $cb = $this->getCB();
    $result = $cb->call('_json_decode', [$json]);
    $this->assertEquals($object, $result);
    $result = $cb->call('_json_decode', [$json, true]);
    $this->assertEquals($array, $result);
  }
}