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 all commits
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
5 changes: 4 additions & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,8 @@ jobs:
- name: Run Check configuration
run: php data/bin/check_configuration.php

- name: Run Tests
- name: Run Lime Tests
run: php data/bin/symfony symfony:test --trace

- name: Run PHPUnit Tests
run: php vendor/bin/phpunit
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ lib/plugins/sfDoctrinePlugin/test/functional/fixtures/log/
/vendor
/composer.lock
.php-cs-fixer.cache
.phpunit.result.cache
phpunit.xml
/tests/fixtures/symfony/log
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"swiftmailer/swiftmailer": "^5.2 || ^6.0"
},
"require-dev": {
"psr/log": "*"
"psr/log": "*",
"phpunit/phpunit": "^9.6",
"symfony/phpunit-bridge": "^7.0"
},
"autoload": {
"files": ["autoload.php"]
Expand All @@ -26,5 +28,13 @@
"replace": {
"lexpress/symfony1": "^1.5"
},
"scripts": {
"test": [
"@test:lime",
"@test:phpunit"
],
"test:lime": "php data/bin/symfony symfony:test --trace",
"test:phpunit": "phpunit"
},
"bin": ["data/bin/symfony"]
}
27 changes: 27 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
bootstrap="tests/bootstrap.php"
failOnRisky="true"
failOnWarning="true"
stderr="true">
<php>
<ini name="error_reporting" value="32767" />
<ini name="intl.default_locale" value="en" />
<ini name="intl.error_level" value="0" />
<ini name="memory_limit" value="-1" />
</php>

<testsuites>
<testsuite name="Symfony Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory>./lib/</directory>
</include>
</coverage>
</phpunit>
75 changes: 75 additions & 0 deletions tests/action/sfComponentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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
*/
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');
}
}
15 changes: 15 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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 Symfony\Bridge\PhpUnit\DeprecationErrorHandler;

if ('disabled' !== getenv('SYMFONY_DEPRECATIONS_HELPER')) {
DeprecationErrorHandler::register(getenv('SYMFONY_DEPRECATIONS_HELPER'));
}
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();
}
}
Loading
Loading