{"id":15022855,"url":"https://github.com/contao/test-case","last_synced_at":"2025-05-02T12:31:29.415Z","repository":{"id":26588005,"uuid":"109284318","full_name":"contao/test-case","owner":"contao","description":"[READ-ONLY] Contao Test Case","archived":false,"fork":false,"pushed_at":"2024-09-17T06:23:02.000Z","size":81,"stargazers_count":6,"open_issues_count":0,"forks_count":4,"subscribers_count":5,"default_branch":"5.x","last_synced_at":"2024-09-27T21:41:09.140Z","etag":null,"topics":["cms","contao","php","phpunit","symfony","testcase"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/contao.png","metadata":{"funding":{"custom":"https://to.contao.org/donate"},"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-02T15:37:49.000Z","updated_at":"2024-08-06T14:11:02.000Z","dependencies_parsed_at":"2023-12-15T13:37:44.769Z","dependency_job_id":"a54cae66-f34c-47ec-8d41-59bd0fb8ba46","html_url":"https://github.com/contao/test-case","commit_stats":{"total_commits":41,"total_committers":6,"mean_commits":6.833333333333333,"dds":"0.29268292682926833","last_synced_commit":"73cd4beb3f1aae2ebc6c2bd508e1ab9101394b05"},"previous_names":[],"tags_count":166,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/contao%2Ftest-case","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/contao%2Ftest-case/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/contao%2Ftest-case/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/contao%2Ftest-case/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/contao","download_url":"https://codeload.github.com/contao/test-case/tar.gz/refs/heads/5.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219868330,"owners_count":16555873,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cms","contao","php","phpunit","symfony","testcase"],"created_at":"2024-09-24T19:58:27.562Z","updated_at":"2024-10-10T09:20:30.005Z","avatar_url":"https://github.com/contao.png","language":"PHP","readme":"# Contao test case\n\nContao is an open source PHP content management system for people who want a professional website that is easy to\nmaintain. Visit the [project website][1] for more information.\n\nThe Contao test case provides a PHPUnit test case with some useful methods for testing Contao. Run\n`php composer.phar require --dev contao/test-case` to install the package and then use it in your test classes:\n\n```php\nuse Contao\\TestCase\\ContaoTestCase;\n\nclass MyTest extends ContaoTestCase\n{\n}\n```\n\n## Mocking the Symfony container\n\nThe `getContainerWithContaoConfiguration()` method mocks a Symfony container with the default configuration of the\nContao core extension.\n\n```php\n$container = $this-\u003egetContainerWithContaoConfiguration();\n\necho $container-\u003egetParameter('contao.upload_path'); // will output \"files\"\n```\n\nYou can also set a project directory:\n\n```php\n$container = $this-\u003egetContainerWithContaoConfiguration('/tmp');\n\necho $container-\u003egetParameter('kernel.project_dir'); // will output \"/tmp\"\necho $container-\u003egetParameter('kernel.root_dir'); // will output \"/tmp/app\"\necho $container-\u003egetParameter('kernel.cache_dir'); // will output \"/tmp/var/cache\"\n```\n\n## Mocking the Contao framework\n\nThe `mockContaoFramework)` method mocks an initialized Contao framework.\n\n```php\n$framework = $this-\u003emockContaoFramework();\n$framework\n    -\u003eexpect($this-\u003eatLeastOnce())\n    -\u003emethod('initialize')\n;\n```\n\nThe method automatically adds a Config adapter with the Contao settings:\n\n```php\n$framework = $this-\u003emockContaoFramework();\n$config = $framework-\u003egetAdapter(Contao\\Config::class);\n\necho $config-\u003eget('datimFormat'); // will output \"'Y-m-d H:i'\"\n```\n\nYou can optionally add more adapters as argument:\n\n```php\n$adapters = [\n    Contao\\Config::class =\u003e $configAdapter,\n    Contao\\Encryption::class =\u003e $encryptionAdapter,\n];\n\n$framework = $this-\u003emockContaoFramework($adapters);\n```\n\nThe given Config adapter will overwrite the default Config adapter.\n\n## Mocking an adapter\n\nThe `mockAdapter()` method will mock an adapter with the given methods.\n\n```php\n$adapter = $this-\u003emockAdapter(['findById']);\n$adapter\n    -\u003emethod('findById')\n    -\u003ewillReturn($model)\n;\n\n$framework = $this-\u003emockContaoFramework([Contao\\FilesModel::class =\u003e $adapter]);\n```\n\nAdapters with a simple return value like above can be further simplified:\n\n```php\n$adapter = $this-\u003emockConfiguredAdapter(['findById' =\u003e $model]);\n```\n\nThis code does exactly the same as the code above.\n\n## Mocking a class with magic properties\n\nThe `mockClassWithProperties()` method mocks a class that uses magic `__set()` and `__get()` methods to manage\nproperties.\n\n```php\n$mock = $this-\u003emockClassWithProperties(Contao\\PageModel::class);\n$mock-\u003eid = 2;\n$mock-\u003etitle = 'Home';\n\necho $mock-\u003etitle; // will output \"Home\"\n```\n\nIf the class to be mocked is read-only, you can optionally pass the properties as constructor argument:\n\n```php\n$properties = [\n    'id' =\u003e 2,\n    'title' =\u003e 'Home',\n];\n\n$mock = $this-\u003emockClassWithProperties(Contao\\PageModel::class, $properties);\n\necho $mock-\u003etitle; // will output \"Home\"\n```\n\nIf you need to call a method of the original class, you can pass the method name as third argument. The resulting mock\nobject will be a partial mock object without the given method(s).\n\n```php\n$mock = $this-\u003emockClassWithProperties(Contao\\PageModel::class, [], ['getTable']);\n$mock-\u003eid = 2;\n\necho $mock-\u003egetTable(); // will call the original method\n```\n\n## Mocking a token storage\n\nThe `mockTokenStorage()` mocks a token storage with a token returning either a Contao back end or front end user.\n\n```php\n$tokenStorage = $this-\u003emockTokenStorage(Contao\\BackendUser::class);\n$user = $tokenStorage-\u003egetToken()-\u003egetUser();\n```\n\n## Using a temporary directory\n\nThe `getTempDir()` method creates a temporary directory based on the test class name and returns its path.\n\n```php\n$fs = new Filesystem();\n$fs-\u003emkdir($this-\u003egetTempDir().'/var/cache');\n```\n\nThe directory will be removed automatically after the tests have been run. For this to work, please make sure to always\ncall the parent `tearDownAfterClass()` method if you define the method in your test class!\n\n```php\nuse Contao\\TestCase\\ContaoTestCase;\n\nclass MyTest extends ContaoTestCase\n{\n    public static function tearDownAfterClass()\n    {\n        // The temporary directory would not be removed without this call!\n        parent::tearDownAfterClass();\n    }\n}\n```\n\n[1]: https://contao.org\n","funding_links":["https://to.contao.org/donate"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcontao%2Ftest-case","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcontao%2Ftest-case","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcontao%2Ftest-case/lists"}