Skip to content

Instantly share code, notes, and snippets.

@teklakct
Created July 17, 2015 07:57
Show Gist options
  • Select an option

  • Save teklakct/35621464f4f1ab285d3b to your computer and use it in GitHub Desktop.

Select an option

Save teklakct/35621464f4f1ab285d3b to your computer and use it in GitHub Desktop.
Strings comparision and check variable is set
StringsCompare
52 - it compare strings using strcasecmp
expected false, but got true.
1 specs
4 examples (3 passed, 1 failed)
20ms
<?php
class StringsCompare
{
public function compareStringsStrtoupper($method, $options)
{
if (strtoupper($method) == 'POST'
&& !isset($options['body'])
&& !isset($options['json'])
) {
return true;
}
return false;
}
public function compareStringsStrcasecmp($method, $options)
{
if (!strcasecmp($method, 'post') && !isset($options['body'], $options['json'])) {
return true;
}
return false;
}
public function compareStringsStrcasecmpAlternative($method, $options)
{
if (!strcasecmp($method, 'post') && !isset($options['body']) && !isset($options['json'])) {
return true;
}
return false;
}
public function oldCompare($method, $options)
{
if ($method == 'POST'
&& !isset($options['body'])
&& !isset($options['json'])
) {
return true;
}
return false;
}
}
<?php
namespace spec;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
/**
* Class StringsCompareSpec
* @package spec
* @mixin \StringsCompare
*/
class StringsCompareSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('StringsCompare');
}
function it_compare_strings_using_strtoupper() {
$method = 'post';
$options = [];
$this->compareStringsStrtoupper($method, $options)->shouldBe(true);
$method = 'POST';
$options = [];
$this->compareStringsStrtoupper($method, $options)->shouldBe(true);
$method = 'PoST';
$options = [];
$this->compareStringsStrtoupper($method, $options)->shouldBe(true);
$method = 'POST';
$options = [
'body' => ''
];
$this->compareStringsStrtoupper($method, $options)->shouldBe(false);
$method = 'POST';
$options = [
'body' => '',
'json' => ''
];
$this->compareStringsStrtoupper($method, $options)->shouldBe(false);
}
function it_compare_strings_using_strcasecmp() {
$method = 'post';
$options = [];
$this->compareStringsStrcasecmp($method, $options)->shouldBe(true);
$method = 'POST';
$options = [];
$this->compareStringsStrcasecmp($method, $options)->shouldBe(true);
$method = 'PoST';
$options = [];
$this->compareStringsStrcasecmp($method, $options)->shouldBe(true);
$method = 'POST';
$options = [
'json' => ''
];
/**
* These won't work
*/
$this->compareStringsStrcasecmp($method, $options)->shouldBe(false);
$method = 'POST';
$options = [
'body' => '',
'json' => ''
];
$this->compareStringsStrcasecmp($method, $options)->shouldBe(false);
}
function it_compare_strings_using_strcasecmp_alternative() {
$method = 'post';
$options = [];
$this->compareStringsStrcasecmpAlternative($method, $options)->shouldBe(true);
$method = 'POST';
$options = [];
$this->compareStringsStrcasecmpAlternative($method, $options)->shouldBe(true);
$method = 'PoST';
$options = [];
$this->compareStringsStrcasecmpAlternative($method, $options)->shouldBe(true);
$method = 'POST';
$options = [
'json' => ''
];
$this->compareStringsStrcasecmpAlternative($method, $options)->shouldBe(false);
$method = 'POST';
$options = [
'body' => '',
'json' => ''
];
$this->compareStringsStrcasecmpAlternative($method, $options)->shouldBe(false);
}
function if_compare_string()
{
$method = 'POST';
$options = [
'body' => ''
];
$this->oldCompare($method, $options)->shouldBe(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment