Completed
Push — master ( dc473b...805453 )
by Joschi
03:52
created

BasicServerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 13
Bugs 0 Features 2
Metric Value
wmc 5
c 13
b 0
f 2
lcom 0
cbo 8
dl 0
loc 82
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 13 1
A testServerInstance() 0 5 1
A testRegisterDispatchRoute() 0 15 1
A testObjectRoutes() 0 9 1
A testCustomTemplateResources() 0 15 1
1
<?php
2
3
/**
4
 * apparat-server
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Server
8
 * @subpackage  Apparat\Server\Tests
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Server\Tests;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Object\Infrastructure\Repository\FileAdapterStrategy;
41
use Apparat\Object\Ports\Facades\RepositoryFacade;
42
use Apparat\Server\Domain\Model\Server;
43
use Apparat\Server\Ports\Facade\ServerFacade;
44
use Apparat\Server\Ports\Route\Route;
45
use Apparat\Server\Ports\View\TYPO3FluidView;
46
use Apparat\Server\Tests\Adr\TestAction;
47
use Apparat\Server\Tests\Adr\TestModule;
48
use Zend\Diactoros\ServerRequest;
49
use Zend\Diactoros\Uri;
50
51
/**
52
 * Basic server test
53
 *
54
 * @package Apparat\Server
55
 * @subpackage Apparat\Server\Tests
56
 */
57
class BasicServerTest extends AbstractServerTest
58
{
59
    /**
60
     * This method is called before the first test of this test class is run.
61
     *
62
     * @since Method available since Release 3.4.0
63
     */
64
    public static function setUpBeforeClass()
65
    {
66
        TestModule::autorun();
67
68
        // Register a repositoryc
69
        RepositoryFacade::register(
70
            'repo',
71
            [
72
                'type' => FileAdapterStrategy::TYPE,
73
                'root' => __DIR__.DIRECTORY_SEPARATOR.'Fixture',
74
            ]
75
        );
76
    }
77
78
    /**
79
     * Test the server instantiation
80
     */
81
    public function testServerInstance()
82
    {
83
        $server = Kernel::create(Server::class);
84
        $this->assertInstanceOf(Server::class, $server);
85
    }
86
87
    /**
88
     * Test registering and dispatching a route
89
     */
90
    public function testRegisterDispatchRoute()
91
    {
92
        $route = new Route('GET', 'default', '/default/{id}{format}', TestAction::class);
93
        $route->setTokens([
94
            'id' => '\d+',
95
            'format' => '(\.[^/]+)?',
96
        ]);
97
        ServerFacade::registerRoute($route);
98
99
        $uri = new Uri('http://apparat/blog/default/1.html');
100
        $request = new ServerRequest();
101
        $request = $request->withUri($uri);
102
        ServerFacade::dispatchRequest($request);
103
//        print_r($response);
104
    }
105
106
    /**
107
     * Test registering the default routes
108
     */
109
    public function testObjectRoutes()
110
    {
111
        ServerFacade::enableObjectRoute();
112
        $uri = new Uri('http://apparat/blog/repo/2016/06/20/2');
113
        $request = new ServerRequest();
114
        $request = $request->withUri($uri);
115
        $response = ServerFacade::dispatchRequest($request);
116
        echo $response->getBody();
117
    }
118
119
    /**
120
     * Test custom template resources
121
     */
122
    public function testCustomTemplateResources()
123
    {
124
        $noneRepoPath = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'non-repo'.DIRECTORY_SEPARATOR;
125
        ServerFacade::enableObjectRoute();
126
        ServerFacade::setViewResources([
127
            TYPO3FluidView::LAYOUTS => $noneRepoPath.'Layouts'.DIRECTORY_SEPARATOR,
128
            TYPO3FluidView::TEMPLATES => $noneRepoPath.'Templates'.DIRECTORY_SEPARATOR,
129
            TYPO3FluidView::PARTIALS => $noneRepoPath.'Partials'.DIRECTORY_SEPARATOR,
130
        ]);
131
        $uri = new Uri('http://apparat/blog/repo/2016/06/20/2');
132
        $request = new ServerRequest();
133
        $request = $request->withUri($uri);
134
        $response = ServerFacade::dispatchRequest($request);
135
        echo $response->getBody();
136
    }
137
138
}
139