it("doesn't stubs on unmatched parameter", function () { $foo = new Foo(); Stub::on($foo)->method('message')->with('Hello World!')->andReturn('Good Bye!'); expect($foo->message('Hello!'))->not->toBe('Good Bye!'); }); }); context("using the with() parameter and the argument matchers", function () { it("stubs on matched parameter", function () { $foo = new Foo(); Stub::on($foo)->method('message')->with(Arg::toBeA('string'))->andReturn('Good Bye!'); expect($foo->message('Hello World!'))->toBe('Good Bye!'); expect($foo->message('Hello'))->toBe('Good Bye!'); }); it("doesn't stubs on unmatched parameter", function () { $foo = new Foo(); Stub::on($foo)->method('message')->with(Arg::toBeA('string'))->andReturn('Good Bye!'); expect($foo->message(false))->not->toBe('Good Bye!'); expect($foo->message(['Hello World!']))->not->toBe('Good Bye!'); }); }); context("with multiple return values", function () { it("stubs a method", function () { $foo = new Foo(); Stub::on($foo)->method('message')->andReturn('Good Evening World!', 'Good Bye World!'); expect($foo->message())->toBe('Good Evening World!'); expect($foo->message())->toBe('Good Bye World!'); expect($foo->message())->toBe('Good Bye World!'); }); }); context("with ->methods()", function () { it("stubs methods using return values as an array", function () {
it('uses valid case insensitive operator in postgres', function () { $connection = Stub::create(['extends' => Connection::class, 'methods' => ['__construct']]); $grammar = new Illuminate\Database\Query\Grammars\PostgresGrammar(); $processor = new Illuminate\Database\Query\Processors\PostgresProcessor(); $sql = 'select * from (select "users".*, max(case when "users"."last_name" = ? then 150 else 0 end ' . '+ case when "users"."last_name" ilike ? then 50 else 0 end ' . '+ case when "users"."last_name" ilike ? then 10 else 0 end) ' . 'as relevance from "users" where ("users"."last_name" ilike ?) ' . 'group by "users"."id") as "users" where "relevance" >= 2.5 order by "relevance" desc'; $bindings = ['jarek', 'jarek%', '%jarek%', '%jarek%']; $query = (new Builder($connection, $grammar, $processor))->from('users')->search(' jarek ', ['last_name' => 10]); expect($query->toSql())->toBe($sql); expect($query->toBase()->getBindings())->toBe($bindings); }); it('supports length aware pagination', function () { $sql = 'select count(*) as aggregate from (select `users`.*, max(case when `users`.`last_name` = ? then 150 else 0 end ' . '+ case when `users`.`last_name` like ? then 50 else 0 end ' . '+ case when `users`.`last_name` like ? then 10 else 0 end) ' . 'as relevance from `users` where (`users`.`last_name` like ?) ' . 'group by `users`.`id`) as `users` where `relevance` >= 2.5'; $bindings = ['jarek', 'jarek%', '%jarek%', '%jarek%']; $query = $this->query->search(' jarek ', ['last_name' => 10]); Stub::on($query->getConnection())->method('select', []); expect($query->getConnection())->toReceive('select')->with($sql, $bindings, Arg::toBeA('boolean')); $query->getCountForPagination(); }); it('moves order clauses after the relevance ordering', function () { $sql = 'select * from (select `users`.*, max(case when `users`.`name` = ? then 15 else 0 end) as relevance ' . 'from `users` where (`users`.`name` like ?) group by `users`.`id`) as `users` ' . 'where `relevance` >= 1 order by `relevance` desc, `first_name` asc'; $bindings = ['jarek', 'jarek']; $query = $this->query->orderBy('first_name')->search('jarek', ['name'], false, 1); expect($query->toSql())->toBe($sql); expect($query->toBase()->getBindings())->toBe($bindings); }); it('doesn\'t split quoted string and treats it as a single keyword to search for', function () { $sql = 'select * from (select `users`.*, max(case when `users`.`first_name` = ? then 15 else 0 end ' . '+ case when `users`.`first_name` like ? then 5 else 0 end) as relevance from `users` ' . 'where (`users`.`first_name` like ?) group by `users`.`id`) ' . 'as `users` where `relevance` >= 0.25 order by `relevance` desc'; $bindings = ['jarek tkaczyk', 'jarek tkaczyk%', 'jarek tkaczyk%']; $query = $this->query->search('"jarek tkaczyk*"', ['first_name'], false); expect($query->toSql())->toBe($sql); expect($query->toBase()->getBindings())->toBe($bindings);
context("when using with() and matchers", function () { it("expects params match the toContain argument matcher", function () { $foo = new Foo(); expect($foo)->toReceive('message')->with(Arg::toContain('My Message')); $foo->message(['My Message', 'My Other Message']); }); it("expects params match the argument matchers", function () { $foo = new Foo(); expect($foo)->toReceive('message')->with(Arg::toBeA('boolean')); expect($foo)->toReceiveNext('message')->with(Arg::toBeA('string')); $foo->message(true); $foo->message('Hello World'); }); it("expects params to not match the toContain argument matcher", function () { $foo = new Foo(); expect($foo)->not->toReceive('message')->with(Arg::toContain('Message')); $foo->message(['My Message', 'My Other Message']); }); }); context("when using classname", function () { it("expects called method to be called", function () { $foo = new Foo(); expect('Kahlan\\Spec\\Fixture\\Plugin\\Pointcut\\Foo')->toReceive('message'); $foo->message(); }); it("expects uncalled method to be uncalled", function () { $foo = new Foo(); expect('Kahlan\\Spec\\Fixture\\Plugin\\Pointcut\\Foo')->not->toReceive('message'); }); it("expects called method to be uncalled using a wrong classname", function () { $foo = new Foo();
}); }); error_reporting(E_ALL ^ E_NOTICE); $this->suite->run(); error_reporting(E_ALL); expect($this->suite->passed())->toBe(true); }); }); describe("->reporters()", function () { it("returns the reporters", function () { $describe = $this->suite->describe("", function () { }); $reporters = Stub::create(); $this->suite->run(['reporters' => $reporters]); expect($this->suite->reporters())->toBe($reporters); }); }); describe("->stop()", function () { it("sends the stop event", function () { $describe = $this->suite->describe("", function () { }); $reporters = Stub::create(); expect($reporters)->toReceive('process')->with('stop', Arg::toMatch(function ($actual) { return isset($actual['specs']) && isset($actual['focuses']); })); $this->suite->run(['reporters' => $reporters]); $this->suite->stop(); expect($this->suite->reporters())->toBe($reporters); }); }); });
$this->expect($closure)->not->toThrow(); }); }); }); error_reporting(E_ALL ^ E_NOTICE); $this->suite->run(); error_reporting(E_ALL); expect($this->suite->passed())->toBe(true); }); }); describe("->reporters()", function () { it("returns the reporters", function () { $describe = $this->suite->describe("", function () { }); $reporters = Double::instance(); $this->suite->run(['reporters' => $reporters]); expect($this->suite->reporters())->toBe($reporters); }); }); describe("->stop()", function () { it("sends the stop event", function () { $describe = $this->suite->describe("", function () { }); $reporters = Double::instance(); expect($reporters)->toReceive('dispatch')->with('stop', Arg::toBeAnInstanceOf('Kahlan\\Summary')); $this->suite->run(['reporters' => $reporters]); $this->suite->stop(); expect($this->suite->reporters())->toBe($reporters); }); }); });
expect($arg->match(true))->not->toBe(true); expect($arg->match(true))->toBe(false); }); it("registers a matcher for a specific class", function () { Matcher::register('toEqualCustom', Stub::classname(['extends' => 'kahlan\\matcher\\ToEqual']), 'stdClass'); $arg = Arg::toEqualCustom(new stdClass()); expect($arg->match(new stdClass()))->toBe(true); $arg = Arg::toEqualCustom(new DateTime()); expect($arg->match(new stdClass()))->not->toBe(true); }); it("makes registered matchers for a specific class available for sub classes", function () { Matcher::register('toEqualCustom', Stub::classname(['extends' => 'kahlan\\matcher\\ToEqual']), 'SplHeap'); $arg = Arg::toEqualCustom(new SplMaxHeap()); expect($arg->match(new SplMaxHeap()))->toBe(true); }); it("throws an exception using an undefined matcher name", function () { $closure = function () { $arg = Arg::toHelloWorld(true); }; expect($closure)->toThrow(new Exception("Unexisting matchers attached to `'toHelloWorld'`.")); }); it("throws an exception using an matcher name which doesn't match actual", function () { Matcher::register('toEqualCustom', Stub::classname(['extends' => 'kahlan\\matcher\\ToEqual']), 'SplHeap'); $closure = function () { $arg = Arg::toEqualCustom(new SplMaxHeap()); $arg->match(true); }; expect($closure)->toThrow(new Exception("Unexisting matcher attached to `'toEqualCustom'` for `SplHeap`.")); }); }); });
expect($this->patchers->patchable('ClassName'))->toBe(false); }); }); describe("->process()", function () { it("runs a method on all patchers", function () { $stub1 = Double::instance(); $this->patchers->add('patcher1', $stub1); $stub2 = Double::instance(); $this->patchers->add('patcher2', $stub2); $path = 'tmp/hello_world.php'; $code = "<?php\necho 'Hello World!';\n"; $matcher = function ($actual) use($code) { return $code === (string) $actual; }; expect($stub1)->toReceive('process')->with(Arg::toMatch($matcher), $path); expect($stub2)->toReceive('process')->with(Arg::toMatch($matcher), $path); $this->patchers->process($code, $path); }); it("bails out if code to process is an empty string", function () { expect($this->patchers->process(''))->toBe(''); }); }); describe("->findFile()", function () { beforeEach(function () { $this->loader = Double::instance(); $this->class = Double::classname(); $this->file = 'some/path/file.php'; $this->stub1 = Double::instance(); $this->patchers->add('patcher1', $this->stub1); $this->stub2 = Double::instance(); $this->patchers->add('patcher2', $this->stub2);
expect('AMQPQueue')->toReceive('setFlags')->with(AMQP_DURABLE); $this->queue_builder->get(); }); it('performs the declaration', function () { expect('AMQPQueue')->toReceive('declareQueue'); $this->queue_builder->get(); }); }); context('Binding', function () { it('binds with the exchange name', function () { expect('AMQPQueue')->toReceive('bind')->with($this->exchange_name, Arg::toBeAny()); $this->queue_builder->get(); }); it('binds with the binding keys', function () { foreach ($this->binding_keys as $binding_key) { expect('AMQPQueue')->toReceive('bind')->with(Arg::toBeAny(), $binding_key); } $this->queue_builder->get(); }); }); context('Exceptional behavior', function () { beforeEach(function () { $this->expectInvalidPropertyException = function () { expect(function () { $this->queue_builder->get(); })->toThrow(new InvalidPropertyException()); }; }); it("throws an exception when a name hasn't been set", function () { $this->queue_builder->setName(null); $this->expectInvalidPropertyException();
$range = [Carbon::parse('-1 year')->startOfYear(), Carbon::parse('-1 year')->endOfYear()]; $this->query->lastYear(); expect($this->query)->toReceive('whereBetween')->with('created_at', Arg::toBeAn('array')); expect($this->query->getBindings())->toEqual($range); }); it('->lastMonth()', function () { $range = [Carbon::parse('-1 month')->startOfMonth(), Carbon::parse('-1 month')->endOfMonth()]; $this->query->lastMonth(); expect($this->query)->toReceive('whereBetween')->with('created_at', Arg::toBeAn('array')); expect($this->query->getBindings())->toEqual($range); }); it('->yesterday()', function () { $range = [Carbon::yesterday(), Carbon::yesterday()->endOfDay()]; $this->query->yesterday(); expect($this->query)->toReceive('whereBetween')->with('created_at', Arg::toBeAn('array')); expect($this->query->getBindings())->toEqual($range); }); it('->lastHour()', function () { $range = [Carbon::parse('-1 hour')->minute(0)->second(0), Carbon::parse('-1 hour')->minute(59)->second(59)]; $this->query->lastHour(); expect($this->query)->toReceive('whereBetween')->with('created_at', Arg::toBeAn('array')); expect($this->query->getBindings())->toEqual($range); }); it('->lastMinute()', function () { $range = [Carbon::parse('-1 minute')->second(0), Carbon::parse('-1 minute')->second(59)]; $this->query->lastMinute(); expect($this->query)->toReceive('whereBetween')->with('created_at', Arg::toBeAn('array')); expect($this->query->getBindings())->toEqual($range); }); }); });
it("doesn't stubs on unmatched arguments", function () { $foo = new Foo(); allow($foo)->toReceive('message')->with('Hello World!')->andReturn('Good Bye!'); expect($foo->message('Hello!'))->not->toBe('Good Bye!'); }); }); context("using the with() parameter and the argument matchers", function () { it("stubs on matched arguments", function () { $foo = new Foo(); allow($foo)->toReceive('message')->with(Arg::toBeA('string'))->andReturn('Good Bye!'); expect($foo->message('Hello World!'))->toBe('Good Bye!'); expect($foo->message('Hello'))->toBe('Good Bye!'); }); it("doesn't stubs on unmatched arguments", function () { $foo = new Foo(); allow($foo)->toReceive('message')->with(Arg::toBeA('string'))->andReturn('Good Bye!'); expect($foo->message(false))->not->toBe('Good Bye!'); expect($foo->message(['Hello World!']))->not->toBe('Good Bye!'); }); }); context("with multiple return values", function () { it("stubs a method", function () { $foo = new Foo(); allow($foo)->toReceive('message')->andReturn('Good Evening World!', 'Good Bye World!'); expect($foo->message())->toBe('Good Evening World!'); expect($foo->message())->toBe('Good Bye World!'); expect($foo->message())->toBe('Good Bye World!'); }); }); context("with chain of methods", function () { it("expects stubbed chain to be stubbed", function () {
Stub::on($this->subscriber)->method('getBindingKeys'); $this->expectImplementationException(); }); it('throws an exception when batch count is an empty value', function () { Stub::on($this->subscriber)->method('getBatchCount'); $this->expectImplementationException(); }); it('throws an exception when batch count is non-integer value', function () { Stub::on($this->subscriber)->method('getBatchCount', function () { return 'asdf'; }); $this->expectImplementationException(); }); }); it('sets processMessage as the callback', function () { expect($this->amqp_queue_spy)->toReceive('consume')->with([$this->subscriber, 'processMessage']); $this->subscriber->consume(); }); }); describe('->acknowledgeMessage', function () { it("calls ack on the queue, passing in the message's delivery tag", function () { $expected_delivery_tag = 'some_delivery_tag'; $message_spy = Helper::getAMQPEnvelope(); Stub::on($message_spy)->method('getDeliveryTag')->andReturn($expected_delivery_tag); Stub::on($this->amqp_queue_spy)->method('ack', function ($delivery_tag, $flags = AMQP_NOPARAM) { }); expect($this->amqp_queue_spy)->toReceive('ack')->with($expected_delivery_tag, Arg::toBeAny()); $this->subscriber->acknowledgeMessage($message_spy); }); }); });