Silex.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Codeception\Module;
  3. use Codeception\Configuration;
  4. use Codeception\Exception\ModuleConfigException;
  5. use Codeception\Lib\Framework;
  6. use Codeception\Lib\Interfaces\DoctrineProvider;
  7. use Codeception\TestInterface;
  8. use Symfony\Component\HttpKernel\Client;
  9. /**
  10. * Module for testing Silex applications like you would regularly do with Silex\WebTestCase.
  11. * This module uses Symfony2 Crawler and HttpKernel to emulate requests and get response.
  12. *
  13. * This module may be considered experimental and require feedback and pull requests from you.
  14. *
  15. * ## Status
  16. *
  17. * * Maintainer: **davert**
  18. * * Stability: **alpha**
  19. * * Contact: davert.codecept@resend.cc
  20. *
  21. * ## Config
  22. *
  23. * * app: **required** - path to Silex bootstrap file.
  24. * * em_service: 'db.orm.em' - use the stated EntityManager to pair with Doctrine Module.
  25. *
  26. * ### Bootstrap File
  27. *
  28. * Bootstrap is the same as [WebTestCase.createApplication](http://silex.sensiolabs.org/doc/testing.html#webtestcase).
  29. *
  30. * ``` php
  31. * <?php
  32. * $app = require __DIR__.'/path/to/app.php';
  33. * $app['debug'] = true;
  34. * unset($app['exception_handler']);
  35. *
  36. * return $app; // optionally
  37. * ?>
  38. * ```
  39. *
  40. * ### Example (`functional.suite.yml`)
  41. *
  42. * modules:
  43. * enabled:
  44. * - Silex:
  45. * app: 'app/bootstrap.php'
  46. *
  47. * ## Public Properties
  48. *
  49. * * app - `Silex\Application` instance received from bootstrap file
  50. *
  51. * Class Silex
  52. * @package Codeception\Module
  53. */
  54. class Silex extends Framework implements DoctrineProvider
  55. {
  56. /**
  57. * @var \Silex\Application
  58. */
  59. public $app;
  60. protected $requiredFields = ['app'];
  61. protected $config = [
  62. 'em_service' => 'db.orm.em'
  63. ];
  64. public function _initialize()
  65. {
  66. if (!file_exists(Configuration::projectDir() . $this->config['app'])) {
  67. throw new ModuleConfigException(__CLASS__, "Bootstrap file {$this->config['app']} not found");
  68. }
  69. $this->loadApp();
  70. }
  71. public function _before(TestInterface $test)
  72. {
  73. $this->loadApp();
  74. $this->client = new Client($this->app);
  75. }
  76. public function _getEntityManager()
  77. {
  78. if (!isset($this->app[$this->config['em_service']])) {
  79. return null;
  80. }
  81. return $this->app[$this->config['em_service']];
  82. }
  83. protected function loadApp()
  84. {
  85. $this->app = require Configuration::projectDir() . $this->config['app'];
  86. // if $app is not returned but exists
  87. if (isset($app)) {
  88. $this->app = $app;
  89. }
  90. if (!isset($this->app)) {
  91. throw new ModuleConfigException(__CLASS__, "\$app instance was not received from bootstrap file");
  92. }
  93. // make doctrine persistent
  94. $db_orm_em = $this->_getEntityManager();
  95. if ($db_orm_em) {
  96. $this->app->extend($this->config['em_service'], function () use ($db_orm_em) {
  97. return $db_orm_em;
  98. });
  99. }
  100. // some Silex apps (like Bolt) may rely on global $app variable
  101. $GLOBALS['app'] = $this->app;
  102. }
  103. /**
  104. * Return an instance of a class from the Container.
  105. *
  106. * Example
  107. * ``` php
  108. * <?php
  109. * $I->grabService('session');
  110. * ?>
  111. * ```
  112. *
  113. * @param string $service
  114. * @return mixed
  115. */
  116. public function grabService($service)
  117. {
  118. return $this->app[$service];
  119. }
  120. /**
  121. * Returns a list of recognized domain names
  122. *
  123. * @return array
  124. */
  125. public function getInternalDomains()
  126. {
  127. $internalDomains = [];
  128. foreach ($this->app['routes'] as $route) {
  129. if ($domain = $route->getHost()) {
  130. $internalDomains[] = '/^' . preg_quote($domain, '/') . '$/';
  131. }
  132. }
  133. return $internalDomains;
  134. }
  135. }