/**
  * @test
  * it should allow replacing a trait instance method with a callback and pass arguments to it
  */
 public function it_should_allow_replacing_a_trait_instance_method_with_a_callback_and_pass_arguments_to_it()
 {
     $mock = Test::replace($this->sutClass . '::methodTwo', function ($one, $two) {
         return $one + $two;
     });
     $this->assertEquals(23, $mock->methodTwo(11, 12));
 }
 /**
  * @test
  * it should allow replacing an abstract class abstract instance method with a callback and pass arguments to it
  */
 public function it_should_allow_replacing_an_abstract_class_abstract_instance_method_with_a_callback_and_pass_arguments_to_it()
 {
     $mock = Test::replace($this->ns . '\\SomeClass::methodFour', function ($string, $int) {
         return 23 + strlen($string) + $int;
     });
     $this->assertEquals(28, $mock->methodFour('foo', 2));
 }
 /**
  * @test
  * it should allow to check method call args using PHPUnit constraints
  * @dataProvider callsAndConstraints
  */
 public function it_should_allow_to_check_method_call_args_using_php_unit_constraints($in, $exp, $shouldPass)
 {
     if (!$shouldPass) {
         $this->setExpectedException('\\PHPUnit_Framework_AssertionFailedError');
     }
     $sut = Sut::replace(__NAMESPACE__ . '\\ConstraintClass::methodOne');
     call_user_func_array([__NAMESPACE__ . '\\ConstraintClass', 'methodOne'], $in);
     $sut->wasCalledWithOnce($exp);
 }
 /**
  * @test
  * it should backup and restore a global variable value at tear down
  */
 public function it_should_backup_and_restore_a_global_variable_value_at_tear_down()
 {
     $GLOBALS['xyz'] = 200;
     Test::setGlobal('xyz', 23);
     global $xyz;
     $this->assertEquals(23, $xyz);
     Test::tearDown();
     $this->assertEquals(200, $GLOBALS['xyz']);
 }
 /**
  * @test
  * it should localize the script data
  */
 public function it_should_localize_the_script_data()
 {
     $sut = $this->make_instance();
     $this->plugin->dir_url('assets/js/dist/idlikethis-admin.js')->willReturn('foo.js');
     $data = ['some' => 'data'];
     $this->data_provider->get_data()->willReturn($data);
     $wp_localize_script = Test::replace('wp_localize_script');
     $sut->enqueue();
     $wp_localize_script->wasCalledWithOnce(['idlikethis-admin', 'idlikethisData', $data]);
 }
 /**
  * @test
  * it should allow verifying calls on spied function
  */
 public function it_should_allow_verifying_calls_on_spied_function()
 {
     $spy = FunctionMocker::replace(__NAMESPACE__ . '\\someFunction');
     someFunction(12);
     someFunction(11);
     $spy->wasCalledTimes(2);
     $spy->wasCalledWithTimes(array(12), 1);
     $spy->wasCalledWithTimes(array(11), 1);
     $spy->wasNotCalledWith(array(10));
     $this->setExpectedException('\\PHPUnit_Framework_AssertionFailedError');
     $spy->wasCalledTimes(0);
 }
 /**
  * @test
  * it should return forged FunctionMocker instance if FunctionMocker mock builder
  */
 public function it_should_return_forged_function_mocker_instance_if_function_mocker_mock_builder()
 {
     $object = revealOrReturn(FunctionMocker::replace('test\\TestTools\\A'));
     $this->assertInstanceOf('test\\TestTools\\A', $object);
 }
 /**
  * @test
  * it should allow mocking an interface method and make it return null using the chain
  */
 public function it_should_allow_mocking_an_interface_method_and_make_it_return_null_using_the_chain()
 {
     $mock = Test::replace(__NAMESPACE__ . '\\SomeI')->method('methodOne')->get();
     $this->assertNull($mock->methodOne());
 }
 /**
  * @test
  * it should allow wrapping own test case utility method
  */
 public function it_should_allow_wrapping_own_test_case_utility_method()
 {
     Test::setTestCase($this);
     $this->assertEquals(23, Test::aUtilityMethod(11, 12));
 }
Exemple #10
0
<?php

// This is global bootstrap for autoloading
use tad\FunctionMocker\FunctionMocker;
FunctionMocker::init();
 /**
  * @test
  * it should return ForgeStepInterface object
  */
 public function it_should_return_forge_step_interface_object()
 {
     $this->assertInstanceOf('tad\\FunctionMocker\\Forge\\StepInterface', Sut::forge('GlobalClass'));
 }
 /**
  * @test
  * it should allow start an instance method replacement chain trying to replace a class name only
  */
 public function it_should_allow_start_an_instance_method_replacement_chain_trying_to_replace_a_class_name_only()
 {
     $sut = FunctionMocker::replace($this->testClass);
     $this->assertInstanceOf('tad\\FunctionMocker\\Forge\\Step', $sut);
 }
 /**
  * Replaces/sets a global object with an instance replacement of the class.
  *
  * The $GLOBALS state will be reset at the next `FunctionMocker::tearDown` call.
  *
  * @param  string $globalHandle The key the value is associated to in the $GLOBALS array.
  * @param  string $functionName A `Class::method` format string
  * @param  mixed $returnValue The return value or callback, see `replace` method.
  *
  * @return mixed               The object that's been set in the $GLOBALS array.
  */
 public static function replaceGlobal($globalHandle, $functionName, $returnValue = null)
 {
     \Arg::_($globalHandle, 'Global var key')->is_string();
     self::backupGlobal($globalHandle);
     $replacement = FunctionMocker::_replace($functionName, $returnValue);
     $GLOBALS[$globalHandle] = $replacement;
     return $replacement;
 }
 /**
  * @test
  * it should allow replacing a class method having an aliased type hinted parameter
  */
 public function it_should_allow_replacing_a_class_method_having_an_aliased_type_hinted_parameter()
 {
     $class = 'Another\\Acme\\Class3';
     FunctionMocker::replace($class)->method('testMethod')->get();
 }
 /**
  * @test
  * it should spot missing required plugins
  * @dataProvider required_plugins
  */
 public function it_should_spot_missing_required_plugins($plugins, $expected)
 {
     Test::replace('wp_get_active_and_valid_plugins', [WP_PLUGIN_DIR . '/plugin-a/plugin-a.php', WP_PLUGIN_DIR . '/plugin-b/plugin-b.php']);
     Test::replace('get_plugin_data', function ($plugin) {
         $map = [WP_PLUGIN_DIR . '/plugin-a/plugin-a.php' => ['Name' => 'Plugin A'], WP_PLUGIN_DIR . '/plugin-b/plugin-b.php' => ['Name' => 'Plugin B']];
         return isset($map[$plugin]) ? $map[$plugin] : \Patchwork\Interceptor\callOriginal(func_get_args());
     });
     $sut = new Minimum_Requirements('5.2', '4.0', 'Some plugin', $plugins);
     $out = $sut->are_required_plugins_active();
     $this->assertEquals($expected, $out);
 }
 /**
  * @test
  * it should allow setting up conditional calls to the original static method
  */
 public function it_should_allow_setting_up_conditional_calls_to_the_original_static_method()
 {
     FunctionMocker::replace(__NAMESPACE__ . '\\PassingClass::passingStaticMethodWithArgs', function ($one, $two) {
         if ($one === 'some') {
             return 'test';
         }
         return FunctionMocker::callOriginal([$one, $two]);
     });
     $this->assertEquals('foo and baz', PassingClass::passingStaticMethodWithArgs('foo', 'baz'));
     $this->assertEquals('test', PassingClass::passingStaticMethodWithArgs('some', 'baz'));
 }
 /**
  * @test
  * it should allow getting a function name indexed list of functions when batch replacing
  */
 public function it_should_allow_getting_a_function_name_indexed_list_of_functions_when_batch_replacing()
 {
     $_functions = ['functionOne', 'functionTwo', 'functionThree'];
     $functions = array_map(function ($name) {
         return __NAMESPACE__ . '\\' . $name;
     }, $_functions);
     $replacedFunctions = FunctionMocker::replace($functions, 'foo');
     $this->assertInstanceOf('tad\\FunctionMocker\\Call\\Verifier\\FunctionCallVerifier', $replacedFunctions['functionOne']);
     $this->assertInstanceOf('tad\\FunctionMocker\\Call\\Verifier\\FunctionCallVerifier', $replacedFunctions['functionTwo']);
     $this->assertInstanceOf('tad\\FunctionMocker\\Call\\Verifier\\FunctionCallVerifier', $replacedFunctions['functionThree']);
 }