diff --git a/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php b/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php index ecd7e42733..c5b1c0b9d5 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php +++ b/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php @@ -58,6 +58,8 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract abstract public function getAcceptMimeType(); + private static $apiInitialized = false; + public function tearDown() { $this->unsetToken(); @@ -68,23 +70,23 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract { parent::setUp(); - self::$DI['app'] = self::$DI->share(function () { - - $environment = 'test'; - $app = require __DIR__ . '/../../../../../lib/Alchemy/Phrasea/Application/Api.php'; - - $app['debug'] = true; - - $app['EM'] = $app->share($app->extend('EM', function ($em) { - @unlink('/tmp/db.sqlite'); - copy(__DIR__ . '/../../../../db-ref.sqlite', '/tmp/db.sqlite'); - - return $em; - })); - - return $app; + self::$DI['app'] = self::$DI->share(function ($DI) { + return $this->loadApp('lib/Alchemy/Phrasea/Application/Api.php'); }); + if (!self::$apiInitialized) { + self::$oauthApplication = \API_OAuth2_Application::create(self::$DI['app'], self::$DI['user_notAdmin'], 'test API v1'); + self::$account = \API_OAuth2_Account::load_with_user(self::$DI['app'], self::$oauthApplication, self::$DI['user_notAdmin']); + self::$token = self::$account->get_token()->get_value(); + + self::$adminToken = null; + + self::$adminApplication = \API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test2 API v1'); + self::$adminAccount = \API_OAuth2_Account::load_with_user(self::$DI['app'], self::$adminApplication, self::$DI['user']); + self::$adminToken = self::$adminAccount->get_token()->get_value(); + self::$apiInitialized = true; + } + if (!static::$APIrecord) { $file = new File(self::$DI['app'], self::$DI['app']['mediavorus']->guess(__DIR__ . '/../../../../files/test024.jpg'), self::$DI['collection']); static::$APIrecord = \record_adapter::createFromFile($file, self::$DI['app']); @@ -94,24 +96,6 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract $this->record = static::$APIrecord; } - public static function setUpBeforeClass() - { - parent::setUpBeforeClass(); - - $environment = 'test'; - $application = require __DIR__ . '/../../../../../lib/Alchemy/Phrasea/Application/Api.php'; - - self::$oauthApplication = \API_OAuth2_Application::create($application, self::$DI['user_notAdmin'], 'test API v1'); - self::$account = \API_OAuth2_Account::load_with_user($application, self::$oauthApplication, self::$DI['user_notAdmin']); - self::$token = self::$account->get_token()->get_value(); - - self::$adminToken = null; - - self::$adminApplication = \API_OAuth2_Application::create($application, self::$DI['user'], 'test2 API v1'); - self::$adminAccount = \API_OAuth2_Account::load_with_user($application, self::$adminApplication, self::$DI['user']); - self::$adminToken = self::$adminAccount->get_token()->get_value(); - } - public static function tearDownAfterClass() { //delete database entry @@ -126,6 +110,8 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract static::$APIrecord->delete(); static::$APIrecord = null; + self::$apiInitialized = false; + parent::tearDownAfterClass(); } @@ -135,12 +121,11 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract public function testThatEventsAreDispatched($eventName, $className, $route, $context) { $preEvent = 0; - $phpunit = $this; - self::$DI['app']['dispatcher']->addListener($eventName, function ($event) use ($phpunit, &$preEvent, $className, $context) { + self::$DI['app']['dispatcher']->addListener($eventName, function ($event) use (&$preEvent, $className, $context) { $preEvent++; - $phpunit->assertInstanceOf($className, $event); + $this->assertInstanceOf($className, $event); if (null !== $context) { - $phpunit->assertEquals($context, $event->getContext()->getContext()); + $this->assertEquals($context, $event->getContext()->getContext()); } }); diff --git a/tests/Alchemy/Tests/Phrasea/Application/OAuth2Test.php b/tests/Alchemy/Tests/Phrasea/Application/OAuth2Test.php index 26e71c9daa..77cd6d9b02 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/OAuth2Test.php +++ b/tests/Alchemy/Tests/Phrasea/Application/OAuth2Test.php @@ -89,11 +89,10 @@ class oauthv2_application_test extends \PhraseanetWebTestCaseAuthenticatedAbstra $acc->set_revoked($revoked); // revoked to show form $preEvent = 0; - $phpunit = $this; - self::$DI['app']['dispatcher']->addListener($eventName, function ($event) use ($phpunit, &$preEvent, $className) { + self::$DI['app']['dispatcher']->addListener($eventName, function ($event) use (&$preEvent, $className) { $preEvent++; - $phpunit->assertInstanceOf($className, $event); - $phpunit->assertEquals(Context::CONTEXT_OAUTH2_NATIVE, $event->getContext()->getContext()); + $this->assertInstanceOf($className, $event); + $this->assertEquals(Context::CONTEXT_OAUTH2_NATIVE, $event->getContext()->getContext()); }); self::$DI['client']->request($method, '/api/oauthv2/authorize', $this->queryParameters); diff --git a/tests/Alchemy/Tests/Phrasea/Authentication/Phrasea/FailureManagerTest.php b/tests/Alchemy/Tests/Phrasea/Authentication/Phrasea/FailureManagerTest.php index 7177bfcd55..5fd09de362 100644 --- a/tests/Alchemy/Tests/Phrasea/Authentication/Phrasea/FailureManagerTest.php +++ b/tests/Alchemy/Tests/Phrasea/Authentication/Phrasea/FailureManagerTest.php @@ -89,9 +89,8 @@ class FailureManagerTest extends \PhraseanetPHPUnitAbstract $username = 'romainneutron'; - $phpunit = $this; - $oldFailures = $this->ArrayIze(function () use ($phpunit) { - return $phpunit->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); + $oldFailures = $this->ArrayIze(function () { + return $this->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); }, 8); $repo->expects($this->once()) @@ -114,9 +113,8 @@ class FailureManagerTest extends \PhraseanetPHPUnitAbstract $username = 'romainneutron'; - $phpunit = $this; - $oldFailures = $this->ArrayIze(function () use ($phpunit) { - return $phpunit->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); + $oldFailures = $this->ArrayIze(function () { + return $this->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); }, 10); $repo->expects($this->once()) @@ -139,12 +137,11 @@ class FailureManagerTest extends \PhraseanetPHPUnitAbstract $username = 'romainneutron'; - $phpunit = $this; - $oldFailures = $this->ArrayIze(function () use ($phpunit) { - $failure = $phpunit->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); - $failure->expects($phpunit->once()) + $oldFailures = $this->ArrayIze(function () { + $failure = $this->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); + $failure->expects($this->once()) ->method('setLocked') - ->with($phpunit->equalTo(false)); + ->with($this->equalTo(false)); return $failure; }, 10); @@ -170,9 +167,8 @@ class FailureManagerTest extends \PhraseanetPHPUnitAbstract $username = 'romainneutron'; - $phpunit = $this; - $oldFailures = $this->ArrayIze(function () use ($phpunit) { - return $phpunit->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); + $oldFailures = $this->ArrayIze(function () { + return $this->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); }, 10); $repo->expects($this->once()) @@ -192,9 +188,8 @@ class FailureManagerTest extends \PhraseanetPHPUnitAbstract $username = 'romainneutron'; - $phpunit = $this; - $oldFailures = $this->ArrayIze(function () use ($phpunit) { - return $phpunit->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); + $oldFailures = $this->ArrayIze(function () { + return $this->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); }, 2); $repo->expects($this->once()) @@ -230,9 +225,8 @@ class FailureManagerTest extends \PhraseanetPHPUnitAbstract $username = 'romainneutron'; - $phpunit = $this; - $oldFailures = $this->ArrayIze(function () use ($phpunit) { - return $phpunit->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); + $oldFailures = $this->ArrayIze(function () { + return $this->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); }, 3); $repo->expects($this->once()) diff --git a/tests/Alchemy/Tests/Phrasea/Authentication/Provider/TwitterTest.php b/tests/Alchemy/Tests/Phrasea/Authentication/Provider/TwitterTest.php index ec36534746..5f15f246ae 100644 --- a/tests/Alchemy/Tests/Phrasea/Authentication/Provider/TwitterTest.php +++ b/tests/Alchemy/Tests/Phrasea/Authentication/Provider/TwitterTest.php @@ -110,10 +110,9 @@ class TwitterTest extends ProviderTestCase $request = $this->getRequestMock(); $this->addQueryParameter($request, ['state' => $state]); - $phpunit = $this; $provider->getTwitterClient()->expects($this->any()) ->method('request') - ->will($this->returnCallback(function ($method) use ($provider, $phpunit) { + ->will($this->returnCallback(function ($method) use ($provider) { switch ($method) { case 'POST': $provider->getTwitterClient()->response = [ @@ -125,7 +124,7 @@ class TwitterTest extends ProviderTestCase case 'GET': $provider->getTwitterClient()->response = [ 'response' => json_encode([ - 'id' => $phpunit::ID, + 'id' => self::ID, ]) ]; break; @@ -165,15 +164,14 @@ class TwitterTest extends ProviderTestCase 'oauth_token_secret' => 'token secret', ]); - $phpunit = $this; $provider->getTwitterClient()->expects($this->once()) ->method('request') - ->will($this->returncallback(function () use ($provider, $phpunit) { + ->will($this->returncallback(function () use ($provider) { $provider->getTwitterClient()->response = [ 'response' => json_encode([ - 'screen_name' => $phpunit::USERNAME, - 'profile_image_url_https' => $phpunit::IMAGEURL, - 'id' => $phpunit::ID, + 'screen_name' => self::USERNAME, + 'profile_image_url_https' => self::IMAGEURL, + 'id' => self::ID, ]) ]; diff --git a/tests/Alchemy/Tests/Phrasea/Border/ManagerTest.php b/tests/Alchemy/Tests/Phrasea/Border/ManagerTest.php index 2b7a52c373..4628d15dc9 100644 --- a/tests/Alchemy/Tests/Phrasea/Border/ManagerTest.php +++ b/tests/Alchemy/Tests/Phrasea/Border/ManagerTest.php @@ -85,23 +85,21 @@ class ManagerTest extends \PhraseanetWebTestCaseAuthenticatedAbstract $shaChecker = new Sha256(self::$DI['app']); $this->object->registerChecker($shaChecker); - $phpunit = $this; - - $postProcess = function ($element, $visa, $code) use ($phpunit, &$records) { - $phpunit->assertInstanceOf('\\Alchemy\Phrasea\Model\Entities\\LazaretFile', $element); - $phpunit->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Visa', $visa); - $phpunit->assertEquals(Manager::LAZARET_CREATED, $code); - $records[] = $element; - }; + $postProcess = function ($element, $visa, $code) use (&$records) { + $this->assertInstanceOf('\\Alchemy\Phrasea\Model\Entities\\LazaretFile', $element); + $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Visa', $visa); + $this->assertEquals(Manager::LAZARET_CREATED, $code); + $records[] = $element; + }; $this->assertEquals(Manager::LAZARET_CREATED, $this->object->process($this->session, File::buildFromPathfile(self::$file1, self::$DI['collection'], self::$DI['app']), $postProcess)); - $postProcess = function ($element, $visa, $code) use ($phpunit, &$records) { - $phpunit->assertInstanceOf('\\record_adapter', $element); - $phpunit->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Visa', $visa); - $phpunit->assertEquals(Manager::RECORD_CREATED, $code); - $records[] = $element; - }; + $postProcess = function ($element, $visa, $code) use (&$records) { + $this->assertInstanceOf('\\record_adapter', $element); + $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Visa', $visa); + $this->assertEquals(Manager::RECORD_CREATED, $code); + $records[] = $element; + }; $this->assertEquals(Manager::RECORD_CREATED, $this->object->process($this->session, File::buildFromPathfile(self::$file1, self::$DI['collection'], self::$DI['app']), $postProcess, Manager::FORCE_RECORD)); @@ -381,19 +379,18 @@ class ManagerTest extends \PhraseanetWebTestCaseAuthenticatedAbstract $file->addAttribute(new Metadata($monoData)); $file->addAttribute(new Metadata($multiData)); - $phpunit = $this; $application = self::$DI['app']; - $postProcess = function ($element, $visa, $code) use ($phpunit, $application) { - $phpunit->assertInstanceOf('\\Alchemy\Phrasea\Model\Entities\\LazaretFile', $element); + $postProcess = function ($element, $visa, $code) use ($application) { + $this->assertInstanceOf('\\Alchemy\Phrasea\Model\Entities\\LazaretFile', $element); - /* @var $element \Alchemy\Phrasea\Model\Entities\LazaretFile */ - foreach ($element->getAttributes() as $attribute) { - $phpunit->assertEquals('metadata', $attribute->getName()); - $value = Factory::getFileAttribute($application, $attribute->getName(), $attribute->getValue()); - $phpunit->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Attribute\\Metadata', $value); - } - }; + /* @var $element \Alchemy\Phrasea\Model\Entities\LazaretFile */ + foreach ($element->getAttributes() as $attribute) { + $this->assertEquals('metadata', $attribute->getName()); + $value = Factory::getFileAttribute($application, $attribute->getName(), $attribute->getValue()); + $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Attribute\\Metadata', $value); + } + }; $this->assertEquals(Manager::LAZARET_CREATED, $this->object->process($this->session, $file, $postProcess, Manager::FORCE_LAZARET)); } diff --git a/tests/Alchemy/Tests/Phrasea/Command/Setup/XSendFileMappingGeneratorTest.php b/tests/Alchemy/Tests/Phrasea/Command/Setup/XSendFileMappingGeneratorTest.php index ec4f0dfb49..8e2057f115 100644 --- a/tests/Alchemy/Tests/Phrasea/Command/Setup/XSendFileMappingGeneratorTest.php +++ b/tests/Alchemy/Tests/Phrasea/Command/Setup/XSendFileMappingGeneratorTest.php @@ -25,10 +25,9 @@ class XSendFileMappingGeneratorTest extends \PhraseanetPHPUnitAbstract ->will($this->returnValue($option)); $command = new XSendFileMappingGenerator(); - $phpunit = $this; - self::$DI['cli']['monolog'] = self::$DI['cli']->share(function () use ($phpunit) { - return $phpunit->getMockBuilder('Monolog\Logger')->disableOriginalConstructor()->getMock(); + self::$DI['cli']['monolog'] = self::$DI['cli']->share(function () { + return $this->getMockBuilder('Monolog\Logger')->disableOriginalConstructor()->getMock(); }); self::$DI['cli']['conf'] = $this->getMockBuilder('Alchemy\Phrasea\Core\Configuration\PropertyAccess') ->disableOriginalConstructor() diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Prod/BridgeTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Prod/BridgeTest.php index a404da3c10..607846fe54 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Prod/BridgeTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Prod/BridgeTest.php @@ -72,14 +72,13 @@ class BridgeApplication extends \PhraseanetWebTestCaseAuthenticatedAbstract $this->assertTrue(self::$DI['client']->getResponse()->isOk()); $pageContent = self::$DI['client']->getResponse()->getContent(); //check for errors in the crawler - $phpunit = $this; $crawler ->filter('div') - ->reduce(function ($crawler, $i) use ($phpunit) { + ->reduce(function ($crawler, $i) { if (!$crawler->attr('class')) { return false; } elseif ($node->getAttribute('class') == 'error_auth') { - $phpunit->fail("Erreur callback"); + $this->fail("Erreur callback"); } }); $settings = self::$account->get_settings(); @@ -97,14 +96,13 @@ class BridgeApplication extends \PhraseanetWebTestCaseAuthenticatedAbstract self::$account->delete(); $crawler = self::$DI['client']->request('GET', '/prod/bridge/callback/apitest/'); $this->assertTrue(self::$DI['client']->getResponse()->isOk()); - $phpunit = $this; $crawler ->filter('div') - ->reduce(function ($crawler, $i) use ($phpunit) { + ->reduce(function ($crawler, $i) { if (!$crawler->attr('class')) { return false; } elseif ($node->getAttribute('class') == 'error_auth') { - $phpunit->fail("Erreur callback"); + $this->fail("Erreur callback"); } }); try { diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php index 88b7aafc79..392486b60f 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php @@ -443,11 +443,10 @@ class LazaretTest extends \PhraseanetWebTestCaseAuthenticatedAbstract ->method('flush'); $called = false; - $phpunit = $this; - self::$DI['app']['phraseanet.logger'] = self::$DI['app']->protect(function () use (&$called, $phpunit) { + self::$DI['app']['phraseanet.logger'] = self::$DI['app']->protect(function () use (&$called) { $called = true; - return $phpunit->getMockBuilder('\Session_Logger') + return $this->getMockBuilder('\Session_Logger') ->disableOriginalConstructor() ->getMock(); }); diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php index d9f15f6a3b..18d8ce4604 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php @@ -1168,11 +1168,10 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract $this->logout(self::$DI['app']); $preEvent = 0; - $phpunit = $this; - self::$DI['app']['dispatcher']->addListener($eventName, function ($event) use ($phpunit, &$preEvent, $className, $context) { + self::$DI['app']['dispatcher']->addListener($eventName, function ($event) use (&$preEvent, $className, $context) { $preEvent++; - $phpunit->assertInstanceOf($className, $event); - $phpunit->assertEquals($context, $event->getContext()->getContext()); + $this->assertInstanceOf($className, $event); + $this->assertEquals($context, $event->getContext()->getContext()); }); self::$DI['client'] = new Client(self::$DI['app'], []); @@ -1259,11 +1258,10 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testGuestAuthenticateTriggersEvents($eventName, $className, $context) { $preEvent = 0; - $phpunit = $this; - self::$DI['app']['dispatcher']->addListener($eventName, function ($event) use ($phpunit, &$preEvent, $className, $context) { + self::$DI['app']['dispatcher']->addListener($eventName, function ($event) use (&$preEvent, $className, $context) { $preEvent++; - $phpunit->assertInstanceOf($className, $event); - $phpunit->assertEquals($context, $event->getContext()->getContext()); + $this->assertInstanceOf($className, $event); + $this->assertEquals($context, $event->getContext()->getContext()); }); $usr_id = \User_Adapter::get_usr_id_from_login(self::$DI['app'], 'invite'); diff --git a/tests/Alchemy/Tests/Phrasea/Controller/SetupTest.php b/tests/Alchemy/Tests/Phrasea/Controller/SetupTest.php index 79cb1d5bb5..0ccfba53f6 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/SetupTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/SetupTest.php @@ -8,9 +8,10 @@ class SetupTest extends \PhraseanetWebTestCaseAbstract { public function setUp() { - // set test environment - $environment = 'test'; - $this->app = require __DIR__ . '/../../../../../lib/Alchemy/Phrasea/Application/Root.php'; + $this->app = $this->loadApp('lib/Alchemy/Phrasea/Application/Root.php'); +// // set test environment +// $environment = 'test'; +// $this->app = require __DIR__ . '/../../../../../lib/Alchemy/Phrasea/Application/Root.php'; $this->app['phraseanet.configuration-tester'] = $this->getMockBuilder('Alchemy\Phrasea\Setup\ConfigurationTester') ->disableOriginalConstructor() diff --git a/tests/Alchemy/Tests/Phrasea/Core/Provider/AuthenticationManagerServiceProviderTest.php b/tests/Alchemy/Tests/Phrasea/Core/Provider/AuthenticationManagerServiceProviderTest.php index e05d71ceb8..e7ae159ab8 100644 --- a/tests/Alchemy/Tests/Phrasea/Core/Provider/AuthenticationManagerServiceProviderTest.php +++ b/tests/Alchemy/Tests/Phrasea/Core/Provider/AuthenticationManagerServiceProviderTest.php @@ -119,15 +119,14 @@ class AuthenticationManagerServiceProvidertest extends ServiceProviderTestCase $app['phraseanet.registry'] = $this->getMockBuilder('registry') ->disableOriginalConstructor() ->getMock(); - $phpunit = $this; $app['phraseanet.registry']->expects($this->any()) ->method('get') - ->will($this->returnCallback(function ($key) use ($phpunit) { + ->will($this->returnCallback(function ($key) { switch ($key) { case 'GV_sit': return mt_rand(); default: - $phpunit->fail(sprintf('Unknown key %s', $key)); + $this->fail(sprintf('Unknown key %s', $key)); } })); $app['phraseanet.appbox'] = self::$DI['app']['phraseanet.appbox']; @@ -153,15 +152,14 @@ class AuthenticationManagerServiceProvidertest extends ServiceProviderTestCase $app['phraseanet.registry'] = $this->getMockBuilder('registry') ->disableOriginalConstructor() ->getMock(); - $phpunit = $this; $app['phraseanet.registry']->expects($this->any()) ->method('get') - ->will($this->returnCallback(function ($key) use ($phpunit) { + ->will($this->returnCallback(function ($key) { switch ($key) { case 'GV_sit': return mt_rand(); default: - $phpunit->fail(sprintf('Unknown key %s', $key)); + $this->fail(sprintf('Unknown key %s', $key)); } })); $app['phraseanet.appbox'] = self::$DI['app']['phraseanet.appbox']; diff --git a/tests/classes/PhraseanetPHPUnitAbstract.php b/tests/classes/PhraseanetPHPUnitAbstract.php index 9775c932ab..3e28901ff1 100644 --- a/tests/classes/PhraseanetPHPUnitAbstract.php +++ b/tests/classes/PhraseanetPHPUnitAbstract.php @@ -132,7 +132,7 @@ abstract class PhraseanetPHPUnitAbstract extends WebTestCase public function setUp() { - ini_set('memory_limit', '2048M'); + ini_set('memory_limit', '4096M'); $this->start = $start = microtime(true); @@ -141,94 +141,12 @@ abstract class PhraseanetPHPUnitAbstract extends WebTestCase \PHPUnit_Framework_Error_Warning::$enabled = true; \PHPUnit_Framework_Error_Notice::$enabled = true; - $phpunit = $this; - - self::$DI['app'] = self::$DI->share(function ($DI) use ($phpunit) { - $environment = 'test'; - $app = require __DIR__ . '/../../lib/Alchemy/Phrasea/Application/Root.php'; - - $app['form.csrf_provider'] = $app->share(function () { - return new CsrfTestProvider(); - }); - - $app['url_generator'] = $app->share($app->extend('url_generator', function ($generator, $app) { - $host = parse_url($app['conf']->get(['main', 'servername']), PHP_URL_HOST); - $generator->setContext(new RequestContext('', 'GET', $host)); - - return $generator; - })); - $app['translator'] = $this->createTranslatorMock(); - $app['phraseanet.SE.subscriber'] = $this->getMock('Symfony\Component\EventDispatcher\EventSubscriberInterface'); - $app['phraseanet.SE.subscriber']::staticExpects($this->any()) - ->method('getSubscribedEvents') - ->will($this->returnValue([])); - - $app['debug'] = true; - - $app['EM'] = $app->share($app->extend('EM', function ($em) use ($phpunit) { - $phpunit::initializeSqliteDB(); - - return $em; - })); - - $app['browser'] = $app->share($app->extend('browser', function ($browser) { - - $browser->setUserAgent(PhraseanetPHPUnitAbstract::USER_AGENT_FIREFOX8MAC); - - return $browser; - })); - - $app['notification.deliverer'] = $phpunit->getMockBuilder('Alchemy\Phrasea\Notification\Deliverer') - ->disableOriginalConstructor() - ->getMock(); - $app['notification.deliverer']->expects($phpunit->any()) - ->method('deliver') - ->will($phpunit->returnCallback(function () use ($phpunit) { - $phpunit->fail('Notification deliverer must be mocked'); - })); - - return $app; + self::$DI['app'] = self::$DI->share(function ($DI) { + return $this->loadApp('/lib/Alchemy/Phrasea/Application/Root.php'); }); - self::$DI['cli'] = self::$DI->share(function ($DI) use ($phpunit) { - $app = new CLI('cli test', null, 'test'); - - $app['form.csrf_provider'] = $app->share(function () { - return new CsrfTestProvider(); - }); - - $app['url_generator'] = $app->share($app->extend('url_generator', function ($generator, $app) { - $host = parse_url($app['conf']->get(['main', 'servername']), PHP_URL_HOST); - $generator->setContext(new RequestContext('', 'GET', $host)); - - return $generator; - })); - - $app['debug'] = true; - - $app['EM'] = $app->share($app->extend('EM', function ($em) use ($phpunit) { - $phpunit::initializeSqliteDb(); - - return $em; - })); - - $app['browser'] = $app->share($app->extend('browser', function ($browser) { - - $browser->setUserAgent(PhraseanetPHPUnitAbstract::USER_AGENT_FIREFOX8MAC); - - return $browser; - })); - - $app['notification.deliverer'] = $phpunit->getMockBuilder('Alchemy\Phrasea\Notification\Deliverer') - ->disableOriginalConstructor() - ->getMock(); - $app['notification.deliverer']->expects($phpunit->any()) - ->method('deliver') - ->will($phpunit->returnCallback(function () use ($phpunit) { - $phpunit->fail('Notification deliverer must be mocked'); - })); - - return $app; + self::$DI['cli'] = self::$DI->share(function ($DI) { + return $this->loadCLI(); }); self::$DI['client'] = self::$DI->share(function ($DI) { @@ -238,6 +156,69 @@ abstract class PhraseanetPHPUnitAbstract extends WebTestCase self::$DI['user']->purgePreferences(); } + protected function loadCLI($environment = Application::ENV_TEST) + { + $cli = new CLI('cli test', null, $environment); + $this->addMocks($cli); + + return $cli; + } + + protected function loadApp($path, $environment = Application::ENV_TEST) + { + $app = require __DIR__ . '/../../' . $path; + $this->addMocks($app); + + return $app; + } + + protected function addMocks(Application $app) + { + $app['debug'] = true; + + $app['form.csrf_provider'] = $app->share(function () { + return new CsrfTestProvider(); + }); + + $app['url_generator'] = $app->share($app->extend('url_generator', function ($generator, $app) { + $host = parse_url($app['conf']->get(['main', 'servername']), PHP_URL_HOST); + $generator->setContext(new RequestContext('', 'GET', $host)); + + return $generator; + })); + + $app['translator'] = $this->createTranslatorMock(); + + $app['phraseanet.SE.subscriber'] = $this->getMock('Symfony\Component\EventDispatcher\EventSubscriberInterface'); + $app['phraseanet.SE.subscriber']::staticExpects($this->any()) + ->method('getSubscribedEvents') + ->will($this->returnValue([])); + + $app['translator'] = $this->createTranslatorMock(); + + $app['EM'] = $app->share($app->extend('EM', function ($em) { + $this->initializeSqliteDB(); + + return $em; + })); + + $app['browser'] = $app->share($app->extend('browser', function ($browser) { + $browser->setUserAgent(PhraseanetPHPUnitAbstract::USER_AGENT_FIREFOX8MAC); + + return $browser; + })); + + $app['notification.deliverer'] = $this->getMockBuilder('Alchemy\Phrasea\Notification\Deliverer') + ->disableOriginalConstructor() + ->getMock(); + + $app['notification.deliverer']->expects($this->any()) + ->method('deliver') + ->will($this->returnCallback(function () { + $this->fail('Notification deliverer must be mocked'); + })); + } + public function tearDown() { /** @@ -1213,12 +1194,10 @@ abstract class PhraseanetPHPUnitAbstract extends WebTestCase ->disableOriginalConstructor() ->getMock(); - $phpunit = $this; - self::$DI['app']['notification.deliverer']->expects($this->any()) ->method('deliver') - ->will($this->returnCallback(function ($email, $receipt) use ($phpunit, &$expectedMails) { - $phpunit->assertTrue(isset($expectedMails[get_class($email)])); + ->will($this->returnCallback(function ($email, $receipt) use (&$expectedMails) { + $this->assertTrue(isset($expectedMails[get_class($email)])); $expectedMails[get_class($email)]++; })); }