Skip to content

Commit c490f35

Browse files
connorhuthePanz
authored andcommitted
add(test) port of sfComponentText unit test
1 parent 240edde commit c490f35

7 files changed

+422
-0
lines changed

tests/action/sfComponentTest.php

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the symfony package.
5+
* (c) Fabien Potencier <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
require_once __DIR__.'/../fixtures/myComponent.php';
12+
require_once __DIR__.'/../sfContext.class.php';
13+
require_once __DIR__.'/../sfNoRouting.class.php';
14+
require_once __DIR__.'/../sfEventDispatcherTestCase.php';
15+
16+
/**
17+
* @internal
18+
*
19+
* @coversNothing
20+
*/
21+
class sfComponentTest extends sfEventDispatcherTestCase
22+
{
23+
private sfContext $context;
24+
25+
public function setUp(): void
26+
{
27+
$this->context = sfContext::getInstance(array(
28+
'routing' => 'sfNoRouting',
29+
'request' => 'sfWebRequest',
30+
));
31+
32+
$this->testObject = new myComponent($this->context, 'module', 'action');
33+
$this->dispatcher = $this->context->getEventDispatcher();
34+
$this->class = 'component';
35+
}
36+
37+
public function testInitialize()
38+
{
39+
$component = new myComponent($this->context, 'module', 'action');
40+
$this->assertSame($this->context, $component->getContext(), '->initialize() takes a sfContext object as its first argument');
41+
$component->initialize($this->context, 'module', 'action');
42+
$this->assertSame($this->context, $component->getContext(), '->initialize() takes a sfContext object as its first argument');
43+
}
44+
45+
public function testGetContext()
46+
{
47+
$component = new myComponent($this->context, 'module', 'action');
48+
49+
$component->initialize($this->context, 'module', 'action');
50+
$this->assertSame($this->context, $component->getContext(), '->getContext() returns the current context');
51+
}
52+
53+
public function testGetRequest()
54+
{
55+
$component = new myComponent($this->context, 'module', 'action');
56+
57+
$component->initialize($this->context, 'module', 'action');
58+
$this->assertSame($this->context->getRequest(), $component->getRequest(), '->getRequest() returns the current request');
59+
}
60+
61+
public function testGetResponse()
62+
{
63+
$component = new myComponent($this->context, 'module', 'action');
64+
65+
$component->initialize($this->context, 'module', 'action');
66+
$this->assertSame($this->context->getResponse(), $component->getResponse(), '->getResponse() returns the current response');
67+
}
68+
69+
public function testSetter()
70+
{
71+
$component = new myComponent($this->context, 'module', 'action');
72+
73+
$component->foo = array();
74+
$component->foo[] = 'bar';
75+
$this->assertSame(array('bar'), $component->foo, '__set() populates component variables');
76+
}
77+
}

tests/fixtures/myComponent.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
class myComponent extends sfComponent
4+
{
5+
public function execute($request)
6+
{
7+
}
8+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
class myEventDispatcherTest
4+
{
5+
public static function newMethod(sfEvent $event)
6+
{
7+
if ('newMethod' == $event['method']) {
8+
$arguments = $event['arguments'];
9+
$event->setReturnValue($arguments[0]);
10+
11+
return true;
12+
}
13+
}
14+
}

tests/sfContext.class.php

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the symfony package.
5+
* (c) Fabien Potencier <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
class sfContext
12+
{
13+
protected static ?sfContext $instance = null;
14+
15+
public $configuration;
16+
public $request;
17+
public $response;
18+
public $controller;
19+
public $routing;
20+
public $user;
21+
public $storage;
22+
23+
protected string $sessionPath = '';
24+
25+
protected sfEventDispatcher $dispatcher;
26+
27+
public static function getInstance($factories = array(), $force = false): self
28+
{
29+
if (!isset(self::$instance) || $force) {
30+
self::$instance = new sfContext();
31+
32+
self::$instance->sessionPath = sys_get_temp_dir().'/sessions_'.rand(11111, 99999);
33+
self::$instance->storage = new sfSessionTestStorage(array('session_path' => self::$instance->sessionPath));
34+
35+
self::$instance->dispatcher = new sfEventDispatcher();
36+
37+
foreach ($factories as $type => $class) {
38+
self::$instance->inject($type, $class);
39+
}
40+
}
41+
42+
return self::$instance;
43+
}
44+
45+
public function __destruct()
46+
{
47+
sfToolkit::clearDirectory($this->sessionPath);
48+
}
49+
50+
public static function hasInstance()/*: true*/
51+
{
52+
return true;
53+
}
54+
55+
public function getEventDispatcher(): sfEventDispatcher
56+
{
57+
return self::$instance->dispatcher;
58+
}
59+
60+
public function getModuleName(): string
61+
{
62+
return 'module';
63+
}
64+
65+
public function getActionName(): string
66+
{
67+
return 'action';
68+
}
69+
70+
public function getConfiguration()
71+
{
72+
return $this->configuration;
73+
}
74+
75+
public function getRequest()
76+
{
77+
return $this->request;
78+
}
79+
80+
public function getResponse()
81+
{
82+
return $this->response;
83+
}
84+
85+
public function getRouting()
86+
{
87+
return $this->routing;
88+
}
89+
90+
public function getStorage()
91+
{
92+
return $this->storage;
93+
}
94+
95+
public function getUser()
96+
{
97+
return $this->user;
98+
}
99+
100+
public function getController()
101+
{
102+
return $this->controller;
103+
}
104+
105+
public function inject($type, $class, $parameters = array())
106+
{
107+
switch ($type) {
108+
case 'routing':
109+
$object = new $class($this->dispatcher, null, $parameters);
110+
break;
111+
case 'response':
112+
$object = new $class($this->dispatcher, $parameters);
113+
break;
114+
case 'request':
115+
$object = new $class($this->dispatcher, $this->routing, $parameters);
116+
break;
117+
default:
118+
$object = new $class($this, $parameters);
119+
}
120+
121+
$this->{$type} = $object;
122+
}
123+
}

tests/sfEventDispatcherTestCase.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the symfony package.
5+
* (c) Fabien Potencier <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
use PHPUnit\Framework\TestCase;
12+
13+
require_once __DIR__.'/fixtures/myEventDispatcherTest.php';
14+
15+
/**
16+
* @internal
17+
*
18+
* @coversNothing
19+
*/
20+
class sfEventDispatcherTestCase extends TestCase
21+
{
22+
protected $testObject;
23+
protected $dispatcher;
24+
protected $class;
25+
26+
public function testExistedMethod()
27+
{
28+
$this->dispatcher->connect($this->class.'.method_not_found', array(myEventDispatcherTest::class, 'newMethod'));
29+
30+
$this->assertSame('ok', $this->testObject->newMethod('ok'), '__call() accepts new methods via sfEventDispatcher');
31+
}
32+
33+
public function testNonExistantMethod()
34+
{
35+
$this->expectException(sfException::class);
36+
37+
$this->testObject->nonexistantmethodname();
38+
}
39+
}

tests/sfNoRouting.class.php

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the symfony package.
5+
* (c) Fabien Potencier <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
/**
12+
* sfNoRouting class is a very simple routing class that uses GET parameters.
13+
*
14+
* @author Fabien Potencier <[email protected]>
15+
*
16+
* @version SVN: $Id: sfNoRouting.class.php 20566 2009-07-29 07:04:01Z fabien $
17+
*/
18+
class sfNoRouting extends sfRouting
19+
{
20+
/**
21+
* @see sfRouting
22+
*/
23+
public function getCurrentInternalUri($with_route_name = false)
24+
{
25+
$parameters = $this->mergeArrays($this->defaultParameters, $_GET);
26+
$action = sprintf('%s/%s', $parameters['module'], $parameters['action']);
27+
28+
// other parameters
29+
unset($parameters['module'], $parameters['action']);
30+
ksort($parameters);
31+
$parameters = count($parameters) ? '?'.http_build_query($parameters, '', '&') : '';
32+
33+
return sprintf('%s%s', $action, $parameters);
34+
}
35+
36+
/**
37+
* @see sfRouting
38+
*/
39+
public function generate($name, $params = array(), $absolute = false)
40+
{
41+
$parameters = $this->mergeArrays($this->defaultParameters, $params);
42+
if ($this->getDefaultParameter('module') == $parameters['module']) {
43+
unset($parameters['module']);
44+
}
45+
if ($this->getDefaultParameter('action') == $parameters['action']) {
46+
unset($parameters['action']);
47+
}
48+
49+
$parameters = http_build_query($parameters, '', '&');
50+
51+
return $this->fixGeneratedUrl('/'.($parameters ? '?'.$parameters : ''), $absolute);
52+
}
53+
54+
/**
55+
* @see sfRouting
56+
*/
57+
public function parse($url)
58+
{
59+
return array();
60+
}
61+
62+
/**
63+
* @see sfRouting
64+
*/
65+
public function getRoutes()
66+
{
67+
return array();
68+
}
69+
70+
/**
71+
* @see sfRouting
72+
*/
73+
public function getRoute($name)
74+
{
75+
return null;
76+
}
77+
78+
/**
79+
* @see sfRouting
80+
*/
81+
public function setRoutes($routes)
82+
{
83+
return array();
84+
}
85+
86+
/**
87+
* @see sfRouting
88+
*/
89+
public function hasRoutes()
90+
{
91+
return false;
92+
}
93+
94+
/**
95+
* @see sfRouting
96+
*/
97+
public function clearRoutes() {}
98+
99+
protected function mergeArrays($arr1, $arr2)
100+
{
101+
foreach ($arr2 as $key => $value) {
102+
$arr1[$key] = $value;
103+
}
104+
105+
return $arr1;
106+
}
107+
}

0 commit comments

Comments
 (0)