rest /tests/Doctrine/Tests/REST/FunctionalTest.php

Language PHP Lines 241
MD5 Hash 7f5c75a0daf1ad5574b2266393c1388d
Repository https://github.com/ner0tic/rest.git View Raw File
  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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php

namespace Doctrine\Tests\REST;

use Doctrine\ORM\EntityManager,
    Doctrine\REST\Client\Manager,
    Doctrine\REST\Client\Request,
    Doctrine\REST\Client\Entity,
    Doctrine\REST\Client\EntityConfiguration,
    Doctrine\REST\Client\Client,
    Doctrine\REST\Server\Server;

require_once __DIR__ . '/TestInit.php';

class FunctionalTest extends \PHPUnit_Framework_TestCase
{
    private $_manager;
    private $_client;

    public function setUpRest($type)
    {
        $config = new \Doctrine\ORM\Configuration();
        $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
        $config->setProxyDir('/tmp');
        $config->setProxyNamespace('Proxies');
        $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());

        $connectionOptions = array(
          'driver' => 'pdo_sqlite',
          'memory' => true
        );

        $em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
        $classes = array($em->getMetadataFactory()->getMetadataFor('Doctrine\Tests\REST\DoctrineUser'));

        $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($em);
        $schemaTool->dropSchema($classes);
        $schemaTool->createSchema($classes);

        if ($type === 'orm') {
            $this->_client = new TestFunctionalClient('user', $em);
        } else {
            $this->_client = new TestFunctionalClient('user', $em->getConnection());
        }

        $this->_manager = new Manager($this->_client);
        $this->_manager->registerEntity('Doctrine\Tests\REST\User');

        Entity::setManager($this->_manager);
    }

    public function testOrm()
    {
        $this->setUpRest('orm');
        $this->_testActiveRecordApi();
    }

    public function testDbal()
    {
        $this->setUpRest('dbal');
        $this->_testActiveRecordApi();
    }

    private function _testActiveRecordApi()
    {
        $user1 = new User();
        $user1->setUsername('jwage');
        $user1->save();

        $this->assertEquals(1, $user1->getId());

        $user2 = new User();
        $user2->setUsername('fabpot');
        $user2->save();

        $this->assertEquals(2, $user2->getId());

        $user3 = new User();
        $user3->setUsername('romanb');
        $user3->save();

        $this->assertEquals(3, $user3->getId());

        $user3->setUsername('romanb_new');
        $user3->save();

        $user3test = User::find($user3->getId());
        $this->assertEquals('romanb_new', $user3test->getUsername());

        $test = User::findAll();
        $this->assertEquals(3, count($test));
        $this->assertTrue($user1 === $test[0]);
        $this->assertTrue($user2 === $test[1]);
        $this->assertTrue($user3 === $test[2]);

        $user3->delete();

        $test = User::findAll();

        $this->assertEquals(2, count($test));
    }
}

class TestFunctionalClient extends Client
{
    public $name;
    public $source;
    public $data = array();
    public $count = 0;

    public function __construct($name, $source)
    {
        $this->name = $name;
        $this->source = $source;
    }

    public function execServer($request, $requestArray, $parameters = array(), $responseType = 'xml')
    {
        $requestArray = array_merge($requestArray, (array) $parameters);
        $server = new Server($this->source, $requestArray);
        if ($this->source instanceof EntityManager) {
            $server->setEntityAlias('Doctrine\Tests\REST\DoctrineUser', 'user');
        }
        $response = $server->getRequestHandler()->execute();
        $data = $request->getResponseTransformerImpl()->transform($response->getContent());
        return $data;
    }

    public function execute(Request $request)
    {
        $url = $request->getUrl();
        $method = $request->getMethod();
        $parameters = $request->getParameters();
        $responseType = $request->getResponseType();

        // GET api/user/1.xml (get)
        if ($method === 'GET' && preg_match_all('/api\/' . $this->name . '\/([0-9]).xml/', $url, $matches)) {
            $id = $matches[1][0];
            return $this->execServer($request, array(
                '_method' => $method, 
                '_format' => $responseType,
                '_entity' => $this->name,
                '_action' => 'get',
                '_id' => $id
            ), $parameters, $responseType);
        }

        // GET api/user.xml (list)
        if ($method === 'GET' && preg_match_all('/api\/' . $this->name . '.xml/', $url, $matches)) {
            return $this->execServer($request, array(
                '_method' => $method,
                '_format' => $responseType,
                '_entity' => $this->name,
                '_action' => 'list'
            ), $parameters, $responseType);
        }

        // PUT api/user.xml (insert)
        if ($method === 'PUT' && preg_match_all('/api\/' . $this->name . '.xml/', $url, $matches)) {
            return $this->execServer($request, array(
                '_method' => $method,
                '_format' => $responseType,
                '_entity' => $this->name,
                '_action' => 'insert'
            ), $parameters, $responseType);
        }


        // POST api/user/1.xml (update)
        if ($method === 'POST' && preg_match_all('/api\/' . $this->name . '\/([0-9]).xml/', $url, $matches)) {
            return $this->execServer($request, array(
                '_method' => $method,
                '_format' => $responseType,
                '_entity' => $this->name,
                '_action' => 'update',
                '_id' => $parameters['id']
            ), $parameters, $responseType);
        }

        // DELETE api/user/1.xml (delete)
        if ($method === 'DELETE' && preg_match_all('/api\/' . $this->name . '\/([0-9]).xml/', $url, $matches)) {
            return $this->execServer($request, array(
                '_method' => $method,
                '_format' => $responseType,
                '_entity' => $this->name,
                '_action' => 'delete',
                '_id' => $matches[1][0]
            ), $parameters, $responseType);
        }
    }
}

class User extends Entity
{
    protected $id;
    protected $username;

    public static function configure(EntityConfiguration $entityConfiguration)
    {
        $entityConfiguration->setUrl('api');
        $entityConfiguration->setName('user');
    }

    public function getId()
    {
        return $this->id;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function setUsername($username)
    {
        $this->username = $username;
    }
}

/**
 * @Entity
 * @Table(name="user")
 */
class DoctrineUser
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @Column(type="string", length=255, unique=true)
     */
    private $username;

    public function setUsername($username)
    {
        $this->username = $username;
    }
}
Back to Top