{"id":28399444,"url":"https://github.com/codeception/domainassert","last_synced_at":"2025-06-28T21:31:43.973Z","repository":{"id":56954956,"uuid":"110441779","full_name":"Codeception/DomainAssert","owner":"Codeception","description":"Domain-specific assertions for PHPUnit and Codeception","archived":false,"fork":false,"pushed_at":"2023-07-26T08:37:12.000Z","size":13,"stargazers_count":22,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-27T11:38:57.908Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Codeception.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2017-11-12T15:00:50.000Z","updated_at":"2023-04-01T13:58:52.000Z","dependencies_parsed_at":"2022-08-21T04:10:20.140Z","dependency_job_id":"4a430fec-0a72-4c53-8291-42018dd7b9c1","html_url":"https://github.com/Codeception/DomainAssert","commit_stats":{"total_commits":9,"total_committers":5,"mean_commits":1.8,"dds":0.7777777777777778,"last_synced_commit":"b9e61d668694003d12cdfed3bb4739acf4f111cf"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/Codeception/DomainAssert","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Codeception%2FDomainAssert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Codeception%2FDomainAssert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Codeception%2FDomainAssert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Codeception%2FDomainAssert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Codeception","download_url":"https://codeload.github.com/Codeception/DomainAssert/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Codeception%2FDomainAssert/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262502310,"owners_count":23321124,"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":[],"created_at":"2025-06-01T08:09:07.781Z","updated_at":"2025-06-28T21:31:43.968Z","avatar_url":"https://github.com/Codeception.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Domain Assertions\n\nAssertion library for [PHPUnit][1] or [Codeception][2] powered by [Symfony Expression Language][3].\n\n## Pitch\n\nThis tiny library helps you to create domain-specific assertions in tests:\n\nInstead of:\n\n```php\n$this-\u003eassertTrue($user-\u003eisValid(), 'user is valid');\n```\nuse\n\n```php\n$this-\u003eassertUserIsValid($user);\n```\n\n### Why? \n\nSee how test fails in first example:\n\n```bash\nuser is valid\nFailed asserting that false is true.\n```\n\nAnd how test fails in second example:\n\n```bash\nFailed asserting that `user.isValid()`.\n[user]: User Object \u0026000000005689696e000000004066036e (\n    'role' =\u003e 'guest'\n)\n```\n\nWhat makes more sense to you? In second example you get the business logic behind the assertion as well as values passed into it.\nThat's why if you have business logic in your project `domain-assert` is your choice.\n \n## How To Use It\n\nInstall this package:\n\n```bash\ncomposer require codeception/domain-assert --dev\n```\nCreate a **trait** with a custom assertion. *We recommend using traits as you can reuse them accross different test cases.*\n \n```php\nuse Codeception\\DomainRule;\n\ntrait CustomAssertion\n{\n    public function assertValidUser(User $user)\n    {\n        $this-\u003eassertThat(\n            ['user' =\u003e $user], \n            new DomainRule('user and user.isValid()')\n        );\n    }\n}\n```\n\n*In this example we check that `$user` exists and `$user-\u003eisValid()` return true;* \n\nThat's all! Now inject this trait to TestCases and use it.\n\n```php\nclass UserTest extends \\PHPUnit\\Framework\\TestCase\n{\n    use CustomAssertion;\n}\n\n```\n\n## Defining Business Rules\n\nThis library uses [Expression Language][3] to define domain rules for assertions. \n\nLet's define a rule to check if we have enough products in the stock:\n\n```\nstock and product.getStock() == stock and product.getAmount() \u003e amount\n```\n\nWe have 3 items here: `product`, `stock`, and `amount` which is a number of items we need.\nLet's create assertion according to this rule:\n\n```php\npublic function assertEnoughProductsInStock(Stock stock, Product product, amount)\n{\n    $this-\u003eassertThat([\n            'product' =\u003e $product,\n            'stock' =\u003e $stock, \n            'amount' =\u003e $amount\n        ], \n        new DomainRule('stock and product.getStock() == stock and product.getAmount() \u003e amount')\n    );\n}\n```\n\nNow it can be used inside your tests:\n\n```php\n$product = new Product('iPhone');\n$stock-\u003eaddProduct($product);\n$stock-\u003eaddProduct($product);\n$stock-\u003eaddProduct($product);\n$this-\u003eassertEnoughProductsInStock($stock, $product, 2);\n```\n\n## Advanced Concepts\n\n* Instead of `$this-\u003eassertThat` you can call static version of this method: `PHPUnit\\Framework\\Assert::assertThat`\n* `Codeception\\DomainRule` extends `PHPUnit\\Framework\\Constraint`.\n* `Codeception\\DomainRule` uses `Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage`\n* [Expression Language can be extended](https://symfony.com/doc/current/components/expression_language/extending.html) by calling `$domainRule-\u003egetLanguage()` \n* `assertThat` can receive first parameter as scalar value. In this case it will be treated as `expected` inside an expression:\n\n```php\npublic function assertIsGreaterThanMinimal()\n{\n    $this-\u003eassertThat(\n        $minimalPrice,\n        new DomainRule('expected \u003e 1000')\n    );    \n}\n```\n\n## License\n\nOpen-source software licensed under the [MIT][4] License. © Codeception PHP Testing Framework\n\n[1]: https://phpunit.de/\n[2]: http://codeception.com/\n[3]: https://symfony.com/doc/current/components/expression_language.html\n[4]: https://github.com/Codeception/DomainAssert/blob/master/LICENSE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeception%2Fdomainassert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeception%2Fdomainassert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeception%2Fdomainassert/lists"}