Title: Fully automate the module without requiring changes to tests · Issue #23 · Codeception/MockeryModule · GitHub
Open Graph Title: Fully automate the module without requiring changes to tests · Issue #23 · Codeception/MockeryModule
X Title: Fully automate the module without requiring changes to tests · Issue #23 · Codeception/MockeryModule
Description: Currently using this module requires changes in each test. I have found an ugly way to alleviate that problem. The code below was tested for Cest type tests only. It should not break for any other test type but might simply not work. use...
Open Graph Description: Currently using this module requires changes in each test. I have found an ugly way to alleviate that problem. The code below was tested for Cest type tests only. It should not break for any other ...
X Description: Currently using this module requires changes in each test. I have found an ugly way to alleviate that problem. The code below was tested for Cest type tests only. It should not break for any other ...
Opengraph URL: https://github.com/Codeception/MockeryModule/issues/23
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Fully automate the module without requiring changes to tests","articleBody":"Currently using this module requires changes in each test.\r\nI have found an ugly way to alleviate that problem.\r\n\r\nThe code below was tested for `Cest` type tests only. It should not break for any other test type but might simply not work.\r\n\r\n```php\r\n\r\nuse Codeception\\Lib\\Di;\r\nuse Codeception\\Module;\r\nuse Codeception\\TestInterface;\r\n\r\nclass Mockery extends Module\r\n{\r\n\r\n\r\n public function _before(TestInterface $test)\r\n {\r\n $meta = $test-\u003egetMetadata();\r\n $rc = new \\ReflectionClass($meta);\r\n $prop = $rc-\u003egetProperty('services');\r\n $prop-\u003esetAccessible(true);\r\n $services = $prop-\u003egetValue($meta);\r\n /** @var Di $di */\r\n $di = $services['di'];\r\n\r\n $services['di'] = new class($di, $test) extends Di {\r\n public function __construct(Di $fallback, private readonly TestInterface $test)\r\n {\r\n parent::__construct($fallback);\r\n\r\n }\r\n\r\n\r\n public function injectDependencies(object $object, string $injectMethodName = self::DEFAULT_INJECT_METHOD_NAME, array $defaults = []): void\r\n {\r\n if ($object === $this-\u003etest) {\r\n parent::injectDependencies(...func_get_args());\r\n return;\r\n }\r\n // We check if the object is our test, if so we close mockery after.\r\n\r\n codecept_debug(['CALLING',\r\n get_class($object),\r\n $injectMethodName\r\n ]);\r\n try {\r\n parent::injectDependencies(...func_get_args());\r\n \\Mockery::close();\r\n } catch (\\Throwable $t) {\r\n \\Mockery::resetContainer();\r\n throw $t;\r\n }\r\n \\Mockery::close();\r\n }\r\n };\r\n $prop-\u003esetValue($meta, $services);\r\n }\r\n}\r\n```\r\n\r\nEssentially we hijack the `Di` container and replace it with a custom implementation. Based on the fact that for `Cest` files the actual test is ran like this:\r\n```php\r\n$this-\u003egetMetadata()-\u003egetService('di')-\u003einjectDependencies($this-\u003etestInstance, $methodName, $context);\r\n```\r\nWe abuse this to insert a mockery closure after calling the test method.\r\n\r\n\r\nWhile I'm happy to make a PR for this, I'm not sure it should be in an \"official\" repository. I probably don't have time to implement a proper fix where we add an event to allow modules to fail a test before it is finished.\r\n","author":{"url":"https://github.com/SamMousa","@type":"Person","name":"SamMousa"},"datePublished":"2023-11-13T16:01:02.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/23/MockeryModule/issues/23"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ff4391b8-5f2e-b1e0-7c81-8a9c16f208f5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DC62:16ACDA:35E301:449194:69916010 |
| html-safe-nonce | 325a19903e4d1dd03e76acba43d8657fa1d0e6d8f5594f8ee6db1bd2b8700802 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQzYyOjE2QUNEQTozNUUzMDE6NDQ5MTk0OjY5OTE2MDEwIiwidmlzaXRvcl9pZCI6IjMwNTI0OTgzMTkyMDI5MzQ4MDAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8c3adf4abf8db4f0c54e0d25b9e9a525a26f9851c4b4d9b2232e88d76dc043d1 |
| hovercard-subject-tag | issue:1990960177 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/Codeception/MockeryModule/23/issue_layout |
| twitter:image | https://opengraph.githubassets.com/62cc78aa49db7b710b1de41cf3c615a98ca1819ed51b19ffa82eaba4a09cc747/Codeception/MockeryModule/issues/23 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/62cc78aa49db7b710b1de41cf3c615a98ca1819ed51b19ffa82eaba4a09cc747/Codeception/MockeryModule/issues/23 |
| og:image:alt | Currently using this module requires changes in each test. I have found an ugly way to alleviate that problem. The code below was tested for Cest type tests only. It should not break for any other ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | SamMousa |
| hostname | github.com |
| expected-hostname | github.com |
| None | 42c603b9d642c4a9065a51770f75e5e27132fef0e858607f5c9cb7e422831a7b |
| turbo-cache-control | no-preview |
| go-import | github.com/Codeception/MockeryModule git https://github.com/Codeception/MockeryModule.git |
| octolytics-dimension-user_id | 1288753 |
| octolytics-dimension-user_login | Codeception |
| octolytics-dimension-repository_id | 16224303 |
| octolytics-dimension-repository_nwo | Codeception/MockeryModule |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 16224303 |
| octolytics-dimension-repository_network_root_nwo | Codeception/MockeryModule |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 848bc6032dcc93a9a7301dcc3f379a72ba13b96e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width