Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sfComponent phpunit test #326

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add(test) port of sfComponentText unit test
  • Loading branch information
connorhu authored and thePanz committed Feb 16, 2024
commit c490f3551d202a8e5d5b4ebf2ce7e42d0b9b665b
77 changes: 77 additions & 0 deletions tests/action/sfComponentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the symfony package.
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

require_once __DIR__.'/../fixtures/myComponent.php';
require_once __DIR__.'/../sfContext.class.php';
require_once __DIR__.'/../sfNoRouting.class.php';
require_once __DIR__.'/../sfEventDispatcherTestCase.php';

/**
* @internal
*
* @coversNothing
*/
class sfComponentTest extends sfEventDispatcherTestCase
{
private sfContext $context;

public function setUp(): void
{
$this->context = sfContext::getInstance(array(
'routing' => 'sfNoRouting',
'request' => 'sfWebRequest',
));

$this->testObject = new myComponent($this->context, 'module', 'action');
$this->dispatcher = $this->context->getEventDispatcher();
$this->class = 'component';
}

public function testInitialize()
{
$component = new myComponent($this->context, 'module', 'action');
$this->assertSame($this->context, $component->getContext(), '->initialize() takes a sfContext object as its first argument');
$component->initialize($this->context, 'module', 'action');
$this->assertSame($this->context, $component->getContext(), '->initialize() takes a sfContext object as its first argument');
}

public function testGetContext()
{
$component = new myComponent($this->context, 'module', 'action');

$component->initialize($this->context, 'module', 'action');
$this->assertSame($this->context, $component->getContext(), '->getContext() returns the current context');
}

public function testGetRequest()
{
$component = new myComponent($this->context, 'module', 'action');

$component->initialize($this->context, 'module', 'action');
$this->assertSame($this->context->getRequest(), $component->getRequest(), '->getRequest() returns the current request');
}

public function testGetResponse()
{
$component = new myComponent($this->context, 'module', 'action');

$component->initialize($this->context, 'module', 'action');
$this->assertSame($this->context->getResponse(), $component->getResponse(), '->getResponse() returns the current response');
}

public function testSetter()
{
$component = new myComponent($this->context, 'module', 'action');

$component->foo = array();
$component->foo[] = 'bar';
$this->assertSame(array('bar'), $component->foo, '__set() populates component variables');
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/myComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

class myComponent extends sfComponent
{
public function execute($request)
{
}
}
14 changes: 14 additions & 0 deletions tests/fixtures/myEventDispatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

class myEventDispatcherTest
{
public static function newMethod(sfEvent $event)
{
if ('newMethod' == $event['method']) {
$arguments = $event['arguments'];
$event->setReturnValue($arguments[0]);

return true;
}
}
}
123 changes: 123 additions & 0 deletions tests/sfContext.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

/*
* This file is part of the symfony package.
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class sfContext
{
protected static ?sfContext $instance = null;

public $configuration;
public $request;
public $response;
public $controller;
public $routing;
public $user;
public $storage;

protected string $sessionPath = '';

protected sfEventDispatcher $dispatcher;

public static function getInstance($factories = array(), $force = false): self
{
if (!isset(self::$instance) || $force) {
self::$instance = new sfContext();

self::$instance->sessionPath = sys_get_temp_dir().'/sessions_'.rand(11111, 99999);
self::$instance->storage = new sfSessionTestStorage(array('session_path' => self::$instance->sessionPath));

self::$instance->dispatcher = new sfEventDispatcher();

foreach ($factories as $type => $class) {
self::$instance->inject($type, $class);
}
}

return self::$instance;
}

public function __destruct()
{
sfToolkit::clearDirectory($this->sessionPath);
}

public static function hasInstance()/*: true*/
{
return true;
}

public function getEventDispatcher(): sfEventDispatcher
{
return self::$instance->dispatcher;
}

public function getModuleName(): string
{
return 'module';
}

public function getActionName(): string
{
return 'action';
}

public function getConfiguration()
{
return $this->configuration;
}

public function getRequest()
{
return $this->request;
}

public function getResponse()
{
return $this->response;
}

public function getRouting()
{
return $this->routing;
}

public function getStorage()
{
return $this->storage;
}

public function getUser()
{
return $this->user;
}

public function getController()
{
return $this->controller;
}

public function inject($type, $class, $parameters = array())
{
switch ($type) {
case 'routing':
$object = new $class($this->dispatcher, null, $parameters);
break;
case 'response':
$object = new $class($this->dispatcher, $parameters);
break;
case 'request':
$object = new $class($this->dispatcher, $this->routing, $parameters);
break;
default:
$object = new $class($this, $parameters);
}

$this->{$type} = $object;
}
}
39 changes: 39 additions & 0 deletions tests/sfEventDispatcherTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the symfony package.
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use PHPUnit\Framework\TestCase;

require_once __DIR__.'/fixtures/myEventDispatcherTest.php';

/**
* @internal
*
* @coversNothing
*/
class sfEventDispatcherTestCase extends TestCase
{
protected $testObject;
protected $dispatcher;
protected $class;

public function testExistedMethod()
{
$this->dispatcher->connect($this->class.'.method_not_found', array(myEventDispatcherTest::class, 'newMethod'));

$this->assertSame('ok', $this->testObject->newMethod('ok'), '__call() accepts new methods via sfEventDispatcher');
}

public function testNonExistantMethod()
{
$this->expectException(sfException::class);

$this->testObject->nonexistantmethodname();
}
}
107 changes: 107 additions & 0 deletions tests/sfNoRouting.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/*
* This file is part of the symfony package.
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* sfNoRouting class is a very simple routing class that uses GET parameters.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*
* @version SVN: $Id: sfNoRouting.class.php 20566 2009-07-29 07:04:01Z fabien $
*/
class sfNoRouting extends sfRouting
{
/**
* @see sfRouting
*/
public function getCurrentInternalUri($with_route_name = false)
{
$parameters = $this->mergeArrays($this->defaultParameters, $_GET);
$action = sprintf('%s/%s', $parameters['module'], $parameters['action']);

// other parameters
unset($parameters['module'], $parameters['action']);
ksort($parameters);
$parameters = count($parameters) ? '?'.http_build_query($parameters, '', '&') : '';

return sprintf('%s%s', $action, $parameters);
}

/**
* @see sfRouting
*/
public function generate($name, $params = array(), $absolute = false)
{
$parameters = $this->mergeArrays($this->defaultParameters, $params);
if ($this->getDefaultParameter('module') == $parameters['module']) {
unset($parameters['module']);
}
if ($this->getDefaultParameter('action') == $parameters['action']) {
unset($parameters['action']);
}

$parameters = http_build_query($parameters, '', '&');

return $this->fixGeneratedUrl('/'.($parameters ? '?'.$parameters : ''), $absolute);
}

/**
* @see sfRouting
*/
public function parse($url)
{
return array();
}

/**
* @see sfRouting
*/
public function getRoutes()
{
return array();
}

/**
* @see sfRouting
*/
public function getRoute($name)
{
return null;
}

/**
* @see sfRouting
*/
public function setRoutes($routes)
{
return array();
}

/**
* @see sfRouting
*/
public function hasRoutes()
{
return false;
}

/**
* @see sfRouting
*/
public function clearRoutes() {}

protected function mergeArrays($arr1, $arr2)
{
foreach ($arr2 as $key => $value) {
$arr1[$key] = $value;
}

return $arr1;
}
}
Loading