Is there any way to use pest or phpunit assertion in application?
How can I sue pest or phpunit asserts inside of my application(not in test)? And when they fail, throw exception.
currently what I've tried:
expect(false)->toBeTrue(); // error: PHPUnit\TextUI\Configuration\Registry::get(): Return value must be of type PHPUnit\TextUI\Configuration\Configuration, null returned
Assert::isTrue(false); // phpunit: won't throw any exception
BTW (without knowing the purpose), but YES, you can use it this way:
For PHP-UNIT.
use Exception;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\ExpectationFailedException;
try {
Assert::assertTrue(false); // This will fail and throw an exception
} catch (ExpectationFailedException $e) {
throw new Exception('Assertion failed: ' . $e->getMessage());
}
For PEST.
use Pest\Expectation;
use PHPUnit\Framework\ExpectationFailedException;
try {
// Boot Pest expectation and use `expect()`
expect(false)->toBeTrue(); // This will fail
} catch (ExpectationFailedException $e) {
throw new Exception('Pest expectation failed: ' . $e->getMessage());
}
@tisuchi both of them fail: PHPUnit\TextUI\Configuration\Registry::get(): Return value must be of type PHPUnit\TextUI\Configuration\Configuration, null returned
@tykus I want to have them in code to for example assert given data is valid(to a function). I can do them in if statements and then throw exception, but testing libraries provide lots of methods.
That sound like a bad idea to me. Those tools should not be used for other things than testing.
There is a reason for them to be require dev, and not require.