1<?php
2/**
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements.  See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership.  The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License.  You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied.  See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 */
20
21use PHPUnit\Framework\TestCase;
22
23class TestModel extends Google_Model
24{
25  public function mapTypes($array)
26  {
27    return parent::mapTypes($array);
28  }
29
30  public function isAssociativeArray($array)
31  {
32    return parent::isAssociativeArray($array);
33  }
34}
35
36class TestService extends Google_Service
37{
38  public $batchPath = 'batch/test';
39}
40
41class Google_ServiceTest extends TestCase
42{
43  public function testCreateBatch()
44  {
45    $response = $this->getMock('Psr\Http\Message\ResponseInterface');
46    $client = $this->getMock('Google_Client');
47    $client
48      ->expects($this->once())
49      ->method('execute')
50      ->with($this->callback(function ($request) {
51        $this->assertEquals('/batch/test', $request->getRequestTarget());
52        return $request;
53      }))
54      ->will($this->returnValue($response));
55    $model = new TestService($client);
56    $batch = $model->createBatch();
57    $this->assertInstanceOf('Google_Http_Batch', $batch);
58    $batch->execute();
59  }
60
61  public function testModel()
62  {
63    $model = new TestModel();
64
65    $model->mapTypes(
66        array(
67          'name' => 'asdf',
68          'gender' => 'z',
69        )
70    );
71    $this->assertEquals('asdf', $model->name);
72    $this->assertEquals('z', $model->gender);
73    $model->mapTypes(
74        array(
75          '__infoType' => 'Google_Model',
76          '__infoDataType' => 'map',
77          'info' => array (
78            'location' => 'mars',
79            'timezone' => 'mst',
80          ),
81          'name' => 'asdf',
82          'gender' => 'z',
83        )
84    );
85    $this->assertEquals('asdf', $model->name);
86    $this->assertEquals('z', $model->gender);
87
88    $this->assertFalse($model->isAssociativeArray(""));
89    $this->assertFalse($model->isAssociativeArray(false));
90    $this->assertFalse($model->isAssociativeArray(null));
91    $this->assertFalse($model->isAssociativeArray(array()));
92    $this->assertFalse($model->isAssociativeArray(array(1, 2)));
93    $this->assertFalse($model->isAssociativeArray(array(1 => 2)));
94
95    $this->assertTrue($model->isAssociativeArray(array('test' => 'a')));
96    $this->assertTrue($model->isAssociativeArray(array("a", "b" => 2)));
97  }
98
99  /**
100   * @dataProvider serviceProvider
101   */
102  public function testIncludes($class)
103  {
104    $this->assertTrue(
105        class_exists($class),
106        sprintf('Failed asserting class %s exists.', $class)
107    );
108  }
109
110  public function serviceProvider()
111  {
112    $classes = array();
113    $path = dirname(dirname(__DIR__)) . '/src/Google/Service';
114    foreach (glob($path . "/*.php") as $file) {
115      $classes[] = array('Google_Service_' . basename($file, '.php'));
116    }
117
118    return $classes;
119  }
120}
121