98 lines
3.1 KiB
YAML
98 lines
3.1 KiB
YAML
uuid:
|
|
- value: 2f503fac-0d18-436b-a5d1-64263c5fc616
|
|
langcode:
|
|
- value: en
|
|
type:
|
|
- target_id: daily_email
|
|
target_type: node_type
|
|
target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7
|
|
revision_timestamp:
|
|
- value: '2025-05-11T09:00:14+00:00'
|
|
revision_uid:
|
|
- target_type: user
|
|
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
|
|
revision_log: { }
|
|
status:
|
|
- value: true
|
|
uid:
|
|
- target_type: user
|
|
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
|
|
title:
|
|
- value: "Assertions aren't just for tests"
|
|
created:
|
|
- value: '2024-05-08T00:00:00+00:00'
|
|
changed:
|
|
- value: '2025-05-11T09:00:14+00:00'
|
|
promote:
|
|
- value: false
|
|
sticky:
|
|
- value: false
|
|
default_langcode:
|
|
- value: true
|
|
revision_translation_affected:
|
|
- value: true
|
|
path:
|
|
- alias: /daily/2024/05/08/assertions-arent-just-for-tests
|
|
langcode: en
|
|
body:
|
|
- value: |
|
|
<p>If you've written or seen automated tests in PHP, you'll have seen lines like this:</p>
|
|
|
|
<pre><code class="php">self::assertTrue(FALSE);
|
|
</code></pre>
|
|
|
|
<p>But, did you know assertions can be used outside of tests.</p>
|
|
|
|
<p>PHP has an <code>assert()</code> function that can be used anywhere.</p>
|
|
|
|
<p>For example, if I had this code:</p>
|
|
|
|
<pre><code class="php">$node = Node::load(1);
|
|
|
|
assert($node instanceof NodeInterface);
|
|
assert($node->bundle() === 'page');
|
|
</code></pre>
|
|
|
|
<p>I know <code>$node</code> is a node with the correct bundle type and I can continue.</p>
|
|
|
|
<p>I've made my assumptions explicit.</p>
|
|
|
|
<p>If <code>$node</code> is not the correct type or returns an unexpected bundle, the assertion will fail and an Exception will be thrown.</p>
|
|
|
|
<p>I think this is better than assuming or hoping the values are as you expect, and it also makes the intent of the code much easier to see and understand.</p>
|
|
|
|
<p>If you haven't tried <code>assert()</code> before, give it a try.</p>
|
|
|
|
|
|
format: full_html
|
|
processed: |
|
|
<p>If you've written or seen automated tests in PHP, you'll have seen lines like this:</p>
|
|
|
|
<pre><code class="php">self::assertTrue(FALSE);
|
|
</code></pre>
|
|
|
|
<p>But, did you know assertions can be used outside of tests.</p>
|
|
|
|
<p>PHP has an <code>assert()</code> function that can be used anywhere.</p>
|
|
|
|
<p>For example, if I had this code:</p>
|
|
|
|
<pre><code class="php">$node = Node::load(1);
|
|
|
|
assert($node instanceof NodeInterface);
|
|
assert($node->bundle() === 'page');
|
|
</code></pre>
|
|
|
|
<p>I know <code>$node</code> is a node with the correct bundle type and I can continue.</p>
|
|
|
|
<p>I've made my assumptions explicit.</p>
|
|
|
|
<p>If <code>$node</code> is not the correct type or returns an unexpected bundle, the assertion will fail and an Exception will be thrown.</p>
|
|
|
|
<p>I think this is better than assuming or hoping the values are as you expect, and it also makes the intent of the code much easier to see and understand.</p>
|
|
|
|
<p>If you haven't tried <code>assert()</code> before, give it a try.</p>
|
|
|
|
|
|
summary: null
|
|
field_daily_email_cta: { }
|