CodeIgniter4/testing/response.html
2023-01-29 00:01:55 +00:00

742 lines
55 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Testing Responses &mdash; CodeIgniter 4.3.1 documentation</title>
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
<link rel="shortcut icon" href="../_static/favicon.ico"/>
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/js/citheme.js"></script>
<script src="../_static/js/carbon.js"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Benchmarking" href="benchmark.html" />
<link rel="prev" title="HTTP Feature Testing" href="feature.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" style="background: #DD4814" >
<a href="../index.html">
<img src="../_static/ci-logo-text.png" class="logo" alt="Logo"/>
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/requirements.html">Server Requirements</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/credits.html">Credits</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/psr.html">PSR Compliance</a></li>
<li class="toctree-l2"><a class="reference internal" href="../license.html">License Agreement</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_composer.html">Composer Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_manual.html">Manual Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/running.html">Running Your App</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
<li class="toctree-l2"><a class="reference internal" href="../changelogs/index.html">Change Logs</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/repositories.html">CodeIgniter Repositories</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Build Your First Application</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static Pages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News Section</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create News Items</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../concepts/structure.html">Application Structure</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/mvc.html">Models, Views, and Controllers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/autoloader.html">Autoloading Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/services.html">Services</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/factories.html">Factories</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/http.html">Working with HTTP Requests</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/security.html">Security Guidelines</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../general/configuration.html">Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helper Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Global Functions and Constants</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/logging.html">Logging Information</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Web Page Caching</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/ajax.html">AJAX Requests</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/modules.html">Code Modules</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../incoming/index.html">Controllers and Routing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../incoming/routing.html">URI Routing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/controllers.html">Controllers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/filters.html">Controller Filters</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/message.html">HTTP Messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/request.html">Request Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/incomingrequest.html">IncomingRequest Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/content_negotiation.html">Content Negotiation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/methodspoofing.html">HTTP Method Spoofing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/restful.html">RESTful Resource Handling</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/views.html">Views</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_cells.html">View Cells</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_renderer.html">View Renderer</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_layouts.html">View Layouts</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_parser.html">View Parser</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_decorators.html">View Decorators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/table.html">HTML Table Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/response.html">HTTP Responses</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/api_responses.html">API Response Trait</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/localization.html">Localization</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/alternative_php.html">Alternate PHP Syntax for View Files</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working With Databases</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/events.html">Database Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../models/model.html">Using CodeIgniter's Model</a></li>
<li class="toctree-l2"><a class="reference internal" href="../models/entities.html">Using Entity Classes</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/forge.html">Database Manipulation with Database Forge</a></li>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/migration.html">Database Migrations</a></li>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/seeds.html">Database Seeding</a></li>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/db_commands.html">Database Commands</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Library Reference</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../libraries/caching.html">Caching Driver</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/cookies.html">Cookies</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/curlrequest.html">CURLRequest Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/email.html">Email Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">Encryption Service</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/files.html">Working with Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/files.html#file-collections">File Collections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/honeypot.html">Honeypot Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/images.html">Image Manipulation Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">Pagination</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/publisher.html">Publisher</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">Security</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/sessions.html">Session Library</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/throttler.html">Throttler</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/time.html">Times and Dates</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">Typography</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/uploaded_files.html">Working with Uploaded Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">Working with URIs</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/user_agent.html">User Agent Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/validation.html">Validation</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/filesystem_helper.html">Filesystem Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/test_helper.html">Test Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
</ul>
</li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Testing</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="overview.html">Getting Started</a></li>
<li class="toctree-l2"><a class="reference internal" href="database.html">Database</a></li>
<li class="toctree-l2"><a class="reference internal" href="fabricator.html">Generating Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="controllers.html">Controller Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="feature.html">HTTP Testing</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Testing Responses</a></li>
<li class="toctree-l2"><a class="reference internal" href="benchmark.html">Benchmarking</a></li>
<li class="toctree-l2"><a class="reference internal" href="debugging.html">Debugging Your Application</a></li>
<li class="toctree-l2"><a class="reference internal" href="mocking.html">Mocking</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_overview.html">CLI Overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_controllers.html">Running Controllers via CLI</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/spark_commands.html">Spark Commands</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_commands.html">Creating Spark Commands</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_generators.html">CLI Generators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_library.html">CLI Library</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_request.html">CLIRequest Class</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../extending/core_classes.html">Creating Core System Classes</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/common.html">Replacing Common Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/events.html">Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/basecontroller.html">Extending the Controller</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/authentication.html">Authentication</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/contributing.html">Contributing to CodeIgniter</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../libraries/official_packages.html">Official Packages</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" style="background: #DD4814" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">CodeIgniter</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<a class="btn btn-neutral float-right" href="https://github.com/codeigniter4/CodeIgniter4/edit/develop/user_guide_src/source/testing/response.rst">Edit this page</a>
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a></li>
<li class="breadcrumb-item"><a href="index.html">Testing</a></li>
<li class="breadcrumb-item active">Testing Responses</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<section id="testing-responses">
<h1>Testing Responses<a class="headerlink" href="#testing-responses" title="Permalink to this headline"></a></h1>
<p>The <code class="docutils literal notranslate"><span class="pre">TestResponse</span></code> class provides a number of helpful functions for parsing and testing responses
from your test cases. Usually a <code class="docutils literal notranslate"><span class="pre">TestResponse</span></code> will be provided for you as a result of your
<a class="reference internal" href="controllers.html"><span class="doc">Controller Tests</span></a> or <a class="reference internal" href="feature.html"><span class="doc">HTTP Feature Tests</span></a>, but you can always
create your own directly using any <code class="docutils literal notranslate"><span class="pre">ResponseInterface</span></code>:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$result</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\CodeIgniter\Test\TestResponse</span><span class="p">(</span><span class="nv">$response</span><span class="p">);</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertOK</span><span class="p">();</span>
</pre></div>
</div>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#testing-the-response" id="id1">Testing the Response</a></p>
<ul>
<li><p><a class="reference internal" href="#accessing-request-response" id="id2">Accessing Request/Response</a></p></li>
<li><p><a class="reference internal" href="#checking-response-status" id="id3">Checking Response Status</a></p></li>
<li><p><a class="reference internal" href="#session-assertions" id="id4">Session Assertions</a></p></li>
<li><p><a class="reference internal" href="#header-assertions" id="id5">Header Assertions</a></p></li>
<li><p><a class="reference internal" href="#cookie-assertions" id="id6">Cookie Assertions</a></p></li>
<li><p><a class="reference internal" href="#dom-helpers" id="id7">DOM Helpers</a></p></li>
<li><p><a class="reference internal" href="#dom-assertions" id="id8">DOM Assertions</a></p></li>
<li><p><a class="reference internal" href="#working-with-json" id="id9">Working With JSON</a></p></li>
<li><p><a class="reference internal" href="#working-with-xml" id="id10">Working With XML</a></p></li>
</ul>
</li>
</ul>
</div>
<section id="testing-the-response">
<h2><a class="toc-backref" href="#id1">Testing the Response</a><a class="headerlink" href="#testing-the-response" title="Permalink to this headline"></a></h2>
<p>Whether you have received a <code class="docutils literal notranslate"><span class="pre">TestResponse</span></code> as a result of your tests or created one yourself,
there are a number of new assertions that you can use in your tests.</p>
<section id="accessing-request-response">
<h3><a class="toc-backref" href="#id2">Accessing Request/Response</a><a class="headerlink" href="#accessing-request-response" title="Permalink to this headline"></a></h3>
<section id="request">
<h4>request()<a class="headerlink" href="#request" title="Permalink to this headline"></a></h4>
<p>You can access directly the Request object, if it was set during testing:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$request</span> <span class="o">=</span> <span class="nv">$results</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">();</span>
</pre></div>
</div>
</section>
<section id="response">
<h4>response()<a class="headerlink" href="#response" title="Permalink to this headline"></a></h4>
<p>This allows you direct access to the response object:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$results</span><span class="o">-&gt;</span><span class="na">response</span><span class="p">();</span>
</pre></div>
</div>
</section>
</section>
<section id="checking-response-status">
<h3><a class="toc-backref" href="#id3">Checking Response Status</a><a class="headerlink" href="#checking-response-status" title="Permalink to this headline"></a></h3>
<section id="isok">
<h4>isOK()<a class="headerlink" href="#isok" title="Permalink to this headline"></a></h4>
<p>Returns a boolean true/false based on whether the response is perceived to be “ok”. This is primarily determined by
a response status code in the 200 or 300s. An empty body is not considered valid, unless in redirects.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$result</span><span class="o">-&gt;</span><span class="na">isOK</span><span class="p">())</span> <span class="p">{</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
<section id="assertok">
<h4>assertOK()<a class="headerlink" href="#assertok" title="Permalink to this headline"></a></h4>
<p>This assertion simply uses the <code class="docutils literal notranslate"><span class="pre">isOK()</span></code> method to test a response. <code class="docutils literal notranslate"><span class="pre">assertNotOK()</span></code> is the inverse of this assertion.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertOK</span><span class="p">();</span>
</pre></div>
</div>
</section>
<section id="isredirect">
<h4>isRedirect()<a class="headerlink" href="#isredirect" title="Permalink to this headline"></a></h4>
<p>Returns a boolean true/false based on whether the response is a redirected response.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$result</span><span class="o">-&gt;</span><span class="na">isRedirect</span><span class="p">())</span> <span class="p">{</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
<section id="assertredirect">
<h4>assertRedirect()<a class="headerlink" href="#assertredirect" title="Permalink to this headline"></a></h4>
<p>Asserts that the Response is an instance of RedirectResponse. <code class="docutils literal notranslate"><span class="pre">assertNotRedirect()</span></code> is the inverse of this assertion.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertRedirect</span><span class="p">();</span>
</pre></div>
</div>
</section>
<section id="assertredirectto">
<h4>assertRedirectTo()<a class="headerlink" href="#assertredirectto" title="Permalink to this headline"></a></h4>
<p>Asserts that the Response is an instance of RedirectResponse and the destination
matches the uri given.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertRedirectTo</span><span class="p">(</span><span class="s1">&#39;foo/bar&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="getredirecturl">
<h4>getRedirectUrl()<a class="headerlink" href="#getredirecturl" title="Permalink to this headline"></a></h4>
<p>Returns the URL set for a RedirectResponse, or null for failure.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$url</span> <span class="o">=</span> <span class="nv">$result</span><span class="o">-&gt;</span><span class="na">getRedirectUrl</span><span class="p">();</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">assertEquals</span><span class="p">(</span><span class="nx">site_url</span><span class="p">(</span><span class="s1">&#39;foo/bar&#39;</span><span class="p">),</span> <span class="nv">$url</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="assertstatus-int-code">
<h4>assertStatus(int $code)<a class="headerlink" href="#assertstatus-int-code" title="Permalink to this headline"></a></h4>
<p>Asserts that the HTTP status code returned matches $code.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertStatus</span><span class="p">(</span><span class="mi">403</span><span class="p">);</span>
</pre></div>
</div>
</section>
</section>
<section id="session-assertions">
<h3><a class="toc-backref" href="#id4">Session Assertions</a><a class="headerlink" href="#session-assertions" title="Permalink to this headline"></a></h3>
<section id="assertsessionhas-string-key-value-null">
<h4>assertSessionHas(string $key, $value = null)<a class="headerlink" href="#assertsessionhas-string-key-value-null" title="Permalink to this headline"></a></h4>
<p>Asserts that a value exists in the resulting session. If $value is passed, will also assert that the variables value
matches what was specified.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertSessionHas</span><span class="p">(</span><span class="s1">&#39;logged_in&#39;</span><span class="p">,</span> <span class="mi">123</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="assertsessionmissing-string-key">
<h4>assertSessionMissing(string $key)<a class="headerlink" href="#assertsessionmissing-string-key" title="Permalink to this headline"></a></h4>
<p>Asserts that the resulting session does not include the specified $key.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertSessionMissing</span><span class="p">(</span><span class="s1">&#39;logged_in&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
</section>
<section id="header-assertions">
<h3><a class="toc-backref" href="#id5">Header Assertions</a><a class="headerlink" href="#header-assertions" title="Permalink to this headline"></a></h3>
<section id="assertheader-string-key-value-null">
<h4>assertHeader(string $key, $value = null)<a class="headerlink" href="#assertheader-string-key-value-null" title="Permalink to this headline"></a></h4>
<p>Asserts that a header named <code class="docutils literal notranslate"><span class="pre">$key</span></code> exists in the response. If <code class="docutils literal notranslate"><span class="pre">$value</span></code> is not empty, will also assert that
the values match.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertHeader</span><span class="p">(</span><span class="s1">&#39;Content-Type&#39;</span><span class="p">,</span> <span class="s1">&#39;text/html&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="assertheadermissing-string-key">
<h4>assertHeaderMissing(string $key)<a class="headerlink" href="#assertheadermissing-string-key" title="Permalink to this headline"></a></h4>
<p>Asserts that a header name <code class="docutils literal notranslate"><span class="pre">$key</span></code> does not exist in the response.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertHeader</span><span class="p">(</span><span class="s1">&#39;Accepts&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
</section>
<section id="cookie-assertions">
<h3><a class="toc-backref" href="#id6">Cookie Assertions</a><a class="headerlink" href="#cookie-assertions" title="Permalink to this headline"></a></h3>
<section id="assertcookie-string-key-value-null-string-prefix">
<h4>assertCookie(string $key, $value = null, string $prefix = )<a class="headerlink" href="#assertcookie-string-key-value-null-string-prefix" title="Permalink to this headline"></a></h4>
<p>Asserts that a cookie named <code class="docutils literal notranslate"><span class="pre">$key</span></code> exists in the response. If <code class="docutils literal notranslate"><span class="pre">$value</span></code> is not empty, will also assert that
the values match. You can set the cookie prefix, if needed, by passing it in as the third parameter.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertCookie</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="s1">&#39;bar&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="assertcookiemissing-string-key">
<h4>assertCookieMissing(string $key)<a class="headerlink" href="#assertcookiemissing-string-key" title="Permalink to this headline"></a></h4>
<p>Asserts that a cookie named <code class="docutils literal notranslate"><span class="pre">$key</span></code> does not exist in the response.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertCookieMissing</span><span class="p">(</span><span class="s1">&#39;ci_session&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="assertcookieexpired-string-key-string-prefix">
<h4>assertCookieExpired(string $key, string $prefix = )<a class="headerlink" href="#assertcookieexpired-string-key-string-prefix" title="Permalink to this headline"></a></h4>
<p>Asserts that a cookie named <code class="docutils literal notranslate"><span class="pre">$key</span></code> exists, but has expired. You can set the cookie prefix, if needed, by passing it
in as the second parameter.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertCookieExpired</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
</section>
<section id="dom-helpers">
<h3><a class="toc-backref" href="#id7">DOM Helpers</a><a class="headerlink" href="#dom-helpers" title="Permalink to this headline"></a></h3>
<p>The response you get back contains a number of helper methods to inspect the HTML output within the response. These
are useful for using within assertions in your tests.</p>
<section id="see">
<h4>see()<a class="headerlink" href="#see" title="Permalink to this headline"></a></h4>
<p>The <code class="docutils literal notranslate"><span class="pre">see()</span></code> method checks the text on the page to see if it exists either by itself, or more specifically within
a tag, as specified by type, class, or id:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Check that &quot;Hello World&quot; is on the page</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">see</span><span class="p">(</span><span class="s1">&#39;Hello World&#39;</span><span class="p">);</span>
<span class="c1">// Check that &quot;Hello World&quot; is within an h1 tag</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">see</span><span class="p">(</span><span class="s1">&#39;Hello World&#39;</span><span class="p">,</span> <span class="s1">&#39;h1&#39;</span><span class="p">);</span>
<span class="c1">// Check that &quot;Hello World&quot; is within an element with the &quot;notice&quot; class</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">see</span><span class="p">(</span><span class="s1">&#39;Hello World&#39;</span><span class="p">,</span> <span class="s1">&#39;.notice&#39;</span><span class="p">);</span>
<span class="c1">// Check that &quot;Hello World&quot; is within an element with id of &quot;title&quot;</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">see</span><span class="p">(</span><span class="s1">&#39;Hello World&#39;</span><span class="p">,</span> <span class="s1">&#39;#title&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">dontSee()</span></code> method is the exact opposite:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Checks that &quot;Hello World&quot; does NOT exist on the page</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">dontSee</span><span class="p">(</span><span class="s1">&#39;Hello World&#39;</span><span class="p">);</span>
<span class="c1">// Checks that &quot;Hellow World&quot; does NOT exist within any h1 tag</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">dontSee</span><span class="p">(</span><span class="s1">&#39;Hello World&#39;</span><span class="p">,</span> <span class="s1">&#39;h1&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="seeelement">
<h4>seeElement()<a class="headerlink" href="#seeelement" title="Permalink to this headline"></a></h4>
<p>The <code class="docutils literal notranslate"><span class="pre">seeElement()</span></code> and <code class="docutils literal notranslate"><span class="pre">dontSeeElement()</span></code> are very similar to the previous methods, but do not look at the
values of the elements. Instead, they simply check that the elements exist on the page:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Check that an element with class &#39;notice&#39; exists</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">seeElement</span><span class="p">(</span><span class="s1">&#39;.notice&#39;</span><span class="p">);</span>
<span class="c1">// Check that an element with id &#39;title&#39; exists</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">seeElement</span><span class="p">(</span><span class="s1">&#39;#title&#39;</span><span class="p">);</span>
<span class="c1">// Verify that an element with id &#39;title&#39; does NOT exist</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">dontSeeElement</span><span class="p">(</span><span class="s1">&#39;#title&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="seelink">
<h4>seeLink()<a class="headerlink" href="#seelink" title="Permalink to this headline"></a></h4>
<p>You can use <code class="docutils literal notranslate"><span class="pre">seeLink()</span></code> to ensure that a link appears on the page with the specified text:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Check that a link exists with &#39;Upgrade Account&#39; as the text::</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">seeLink</span><span class="p">(</span><span class="s1">&#39;Upgrade Account&#39;</span><span class="p">);</span>
<span class="c1">// Check that a link exists with &#39;Upgrade Account&#39; as the text, AND a class of &#39;upsell&#39;</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">seeLink</span><span class="p">(</span><span class="s1">&#39;Upgrade Account&#39;</span><span class="p">,</span> <span class="s1">&#39;.upsell&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="seeinfield">
<h4>seeInField()<a class="headerlink" href="#seeinfield" title="Permalink to this headline"></a></h4>
<p>The <code class="docutils literal notranslate"><span class="pre">seeInField()</span></code> method checks for any input tags exist with the name and value:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Check that an input exists named &#39;user&#39; with the value &#39;John Snow&#39;</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">seeInField</span><span class="p">(</span><span class="s1">&#39;user&#39;</span><span class="p">,</span> <span class="s1">&#39;John Snow&#39;</span><span class="p">);</span>
<span class="c1">// Check a multi-dimensional input</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">seeInField</span><span class="p">(</span><span class="s1">&#39;user[name]&#39;</span><span class="p">,</span> <span class="s1">&#39;John Snow&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="seecheckboxischecked">
<h4>seeCheckboxIsChecked()<a class="headerlink" href="#seecheckboxischecked" title="Permalink to this headline"></a></h4>
<p>Finally, you can check if a checkbox exists and is checked with the <code class="docutils literal notranslate"><span class="pre">seeCheckboxIsChecked()</span></code> method:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Check if checkbox is checked with class of &#39;foo&#39;</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">seeCheckboxIsChecked</span><span class="p">(</span><span class="s1">&#39;.foo&#39;</span><span class="p">);</span>
<span class="c1">// Check if checkbox with id of &#39;bar&#39; is checked</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">seeCheckboxIsChecked</span><span class="p">(</span><span class="s1">&#39;#bar&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
</section>
<section id="dom-assertions">
<h3><a class="toc-backref" href="#id8">DOM Assertions</a><a class="headerlink" href="#dom-assertions" title="Permalink to this headline"></a></h3>
<p>You can perform tests to see if specific elements/text/etc exist with the body of the response with the following
assertions.</p>
<section id="assertsee-string-search-null-string-element-null">
<h4>assertSee(string $search = null, string $element = null)<a class="headerlink" href="#assertsee-string-search-null-string-element-null" title="Permalink to this headline"></a></h4>
<p>Asserts that text/HTML is on the page, either by itself or - more specifically - within
a tag, as specified by type, class, or id:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Check that &quot;Hello World&quot; is on the page</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertSee</span><span class="p">(</span><span class="s1">&#39;Hello World&#39;</span><span class="p">);</span>
<span class="c1">// Check that &quot;Hello World&quot; is within an h1 tag</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertSee</span><span class="p">(</span><span class="s1">&#39;Hello World&#39;</span><span class="p">,</span> <span class="s1">&#39;h1&#39;</span><span class="p">);</span>
<span class="c1">// Check that &quot;Hello World&quot; is within an element with the &quot;notice&quot; class</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertSee</span><span class="p">(</span><span class="s1">&#39;Hello World&#39;</span><span class="p">,</span> <span class="s1">&#39;.notice&#39;</span><span class="p">);</span>
<span class="c1">// Check that &quot;Hello World&quot; is within an element with id of &quot;title&quot;</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertSee</span><span class="p">(</span><span class="s1">&#39;Hello World&#39;</span><span class="p">,</span> <span class="s1">&#39;#title&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="assertdontsee-string-search-null-string-element-null">
<h4>assertDontSee(string $search = null, string $element = null)<a class="headerlink" href="#assertdontsee-string-search-null-string-element-null" title="Permalink to this headline"></a></h4>
<p>Asserts the exact opposite of the <code class="docutils literal notranslate"><span class="pre">assertSee()</span></code> method:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Checks that &quot;Hello World&quot; does NOT exist on the page</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">dontSee</span><span class="p">(</span><span class="s1">&#39;Hello World&#39;</span><span class="p">);</span>
<span class="c1">// Checks that &quot;Hello World&quot; does NOT exist within any h1 tag</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">dontSee</span><span class="p">(</span><span class="s1">&#39;Hello World&#39;</span><span class="p">,</span> <span class="s1">&#39;h1&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="assertseeelement-string-search">
<h4>assertSeeElement(string $search)<a class="headerlink" href="#assertseeelement-string-search" title="Permalink to this headline"></a></h4>
<p>Similar to <code class="docutils literal notranslate"><span class="pre">assertSee()</span></code>, however this only checks for an existing element. It does not check for specific text:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Check that an element with class &#39;notice&#39; exists</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">seeElement</span><span class="p">(</span><span class="s1">&#39;.notice&#39;</span><span class="p">);</span>
<span class="c1">// Check that an element with id &#39;title&#39; exists</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">seeElement</span><span class="p">(</span><span class="s1">&#39;#title&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="assertdontseeelement-string-search">
<h4>assertDontSeeElement(string $search)<a class="headerlink" href="#assertdontseeelement-string-search" title="Permalink to this headline"></a></h4>
<p>Similar to <code class="docutils literal notranslate"><span class="pre">assertSee()</span></code>, however this only checks for an existing element that is missing. It does not check for
specific text:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Verify that an element with id &#39;title&#39; does NOT exist</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">dontSeeElement</span><span class="p">(</span><span class="s1">&#39;#title&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="assertseelink-string-text-string-details-null">
<h4>assertSeeLink(string $text, string $details = null)<a class="headerlink" href="#assertseelink-string-text-string-details-null" title="Permalink to this headline"></a></h4>
<p>Asserts that an anchor tag is found with matching <code class="docutils literal notranslate"><span class="pre">$text</span></code> as the body of the tag:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Check that a link exists with &#39;Upgrade Account&#39; as the text::</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">seeLink</span><span class="p">(</span><span class="s1">&#39;Upgrade Account&#39;</span><span class="p">);</span>
<span class="c1">// Check that a link exists with &#39;Upgrade Account&#39; as the text, AND a class of &#39;upsell&#39;</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">seeLink</span><span class="p">(</span><span class="s1">&#39;Upgrade Account&#39;</span><span class="p">,</span> <span class="s1">&#39;.upsell&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="assertseeinfield-string-field-string-value-null">
<h4>assertSeeInField(string $field, string $value = null)<a class="headerlink" href="#assertseeinfield-string-field-string-value-null" title="Permalink to this headline"></a></h4>
<p>Asserts that an input tag exists with the name and value:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Check that an input exists named &#39;user&#39; with the value &#39;John Snow&#39;</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">assertSeeInField</span><span class="p">(</span><span class="s1">&#39;user&#39;</span><span class="p">,</span> <span class="s1">&#39;John Snow&#39;</span><span class="p">);</span>
<span class="c1">// Check a multi-dimensional input</span>
<span class="nv">$results</span><span class="o">-&gt;</span><span class="na">assertSeeInField</span><span class="p">(</span><span class="s1">&#39;user[name]&#39;</span><span class="p">,</span> <span class="s1">&#39;John Snow&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
</section>
<section id="working-with-json">
<h3><a class="toc-backref" href="#id9">Working With JSON</a><a class="headerlink" href="#working-with-json" title="Permalink to this headline"></a></h3>
<p>Responses will frequently contain JSON responses, especially when working with API methods. The following methods
can help to test the responses.</p>
<section id="getjson">
<h4>getJSON()<a class="headerlink" href="#getjson" title="Permalink to this headline"></a></h4>
<p>This method will return the body of the response as a JSON string:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="cm">/*</span>
<span class="cm"> * Response body is this:</span>
<span class="cm"> * [&#39;foo&#39; =&gt; &#39;bar&#39;]</span>
<span class="cm"> */</span>
<span class="nv">$json</span> <span class="o">=</span> <span class="nv">$result</span><span class="o">-&gt;</span><span class="na">getJSON</span><span class="p">();</span>
<span class="cm">/*</span>
<span class="cm"> * $json is this:</span>
<span class="cm"> * {</span>
<span class="cm"> * &quot;foo&quot;: &quot;bar&quot;</span>
<span class="cm"> * }</span>
<span class="cm">`*/</span>
</pre></div>
</div>
<p>You can use this method to determine if <code class="docutils literal notranslate"><span class="pre">$response</span></code> actually holds JSON content:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Verify the response is JSON</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">assertTrue</span><span class="p">(</span><span class="nv">$result</span><span class="o">-&gt;</span><span class="na">getJSON</span><span class="p">()</span> <span class="o">!==</span> <span class="k">false</span><span class="p">);</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Be aware that the JSON string will be pretty-printed in the result.</p>
</div>
</section>
<section id="assertjsonfragment-array-fragment">
<h4>assertJSONFragment(array $fragment)<a class="headerlink" href="#assertjsonfragment-array-fragment" title="Permalink to this headline"></a></h4>
<p>Asserts that $fragment is found within the JSON response. It does not need to match the entire JSON value.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="cm">/*</span>
<span class="cm"> * Response body is this:</span>
<span class="cm"> * [</span>
<span class="cm"> * &#39;config&#39; =&gt; [&#39;key-a&#39;, &#39;key-b&#39;],</span>
<span class="cm"> * ]</span>
<span class="cm"> */</span>
<span class="c1">// Is true</span>
<span class="nv">$result</span><span class="o">-&gt;</span><span class="na">assertJSONFragment</span><span class="p">([</span><span class="s1">&#39;config&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="s1">&#39;key-a&#39;</span><span class="p">]]);</span>
</pre></div>
</div>
</section>
<section id="assertjsonexact-test">
<h4>assertJSONExact($test)<a class="headerlink" href="#assertjsonexact-test" title="Permalink to this headline"></a></h4>
<p>Similar to <code class="docutils literal notranslate"><span class="pre">assertJSONFragment()</span></code>, but checks the entire JSON response to ensure exact matches.</p>
</section>
</section>
<section id="working-with-xml">
<h3><a class="toc-backref" href="#id10">Working With XML</a><a class="headerlink" href="#working-with-xml" title="Permalink to this headline"></a></h3>
<section id="getxml">
<h4>getXML()<a class="headerlink" href="#getxml" title="Permalink to this headline"></a></h4>
<p>If your application returns XML, you can retrieve it through this method.</p>
</section>
</section>
</section>
</section>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="feature.html" class="btn btn-neutral float-left" title="HTTP Feature Testing" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="benchmark.html" class="btn btn-neutral float-right" title="Benchmarking" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>&#169; Copyright 2019-2023 CodeIgniter Foundation.
<span class="lastupdated">Last updated on Jan 29, 2023.
</span></p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(false);
});
</script>
</body>
</html>