/**
  * @test
  */
 public function classAnnotatedWithSingletonWillOnlyBeCreatedOnce()
 {
     $binder = new Binder();
     $binder->bind(Number::class)->to(RandomSingleton::class);
     $slot = $binder->getInjector()->getInstance(SlotMachine::class);
     assert($slot->number1, isInstanceOf(RandomSingleton::class)->and(isSameAs($slot->number2)));
 }
 /**
  * @test
  */
 public function otherScopeIsUsedToCreateInstance()
 {
     $binder = new Binder();
     $instance = new \stdClass();
     $binder->bind(\stdClass::class)->to(\stdClass::class)->in(NewInstance::of(BindingScope::class)->mapCalls(['getInstance' => $instance]));
     assert($binder->getInjector()->getInstance(\stdClass::class), isSameAs($instance));
 }
 /**
  * @test
  */
 public function createsInstanceIfNotPresent()
 {
     $instance = new \stdClass();
     $this->prepareSession(['hasValue' => false]);
     $this->provider->mapCalls(['get' => $instance]);
     assert($this->sessionScope->getInstance(reflect(\stdClass::class), $this->provider), isSameAs($instance));
 }
 /**
  * @test
  */
 public function outputStreamGetsPrefix()
 {
     $outputStream = NewInstance::of(OutputStream::class);
     $this->streamFactory->mapCalls(['createOutputStream' => $outputStream]);
     assert($this->prefixedStreamFactory->createOutputStream('foo', ['bar' => 'baz']), isSameAs($outputStream));
     verify($this->streamFactory, 'createOutputStream')->received('prefix/foo', ['bar' => 'baz']);
 }
 /**
  * @test
  */
 public function injectWithProviderClass()
 {
     $binder = new Binder();
     $binder->bind(Answer::class)->toProviderClass(new \ReflectionClass(MyProviderClass::class));
     $question = $binder->getInjector()->getInstance(AnotherQuestion::class);
     assert($question->getAnswer(), isInstanceOf(Answer::class));
 }
 /**
  * @test
  */
 public function writesToStandardOutputBuffer()
 {
     $out = new StandardOutputStream();
     ob_start();
     $out->write('foo');
     assert(ob_get_contents(), equals('foo'));
     ob_end_clean();
 }
 /**
  * @test
  */
 public function canIterateOverNonSeekableInputStream()
 {
     $inputStream = NewInstance::of(InputStream::class)->mapCalls(['readLine' => onConsecutiveCalls('foo', 'bar', 'baz', ''), 'eof' => onConsecutiveCalls(false, false, false, true)]);
     $content = [];
     foreach (linesOf($inputStream) as $lineNumber => $line) {
         $content[$lineNumber] = $line;
     }
     assert($content, equals([1 => 'foo', 2 => 'bar', 3 => 'baz']));
 }
 /**
  * @test
  */
 public function injectWithClosure()
 {
     $binder = new Binder();
     $answer = new Answer();
     $binder->bind(Answer::class)->toClosure(function () use($answer) {
         return $answer;
     });
     $question = $binder->getInjector()->getInstance(AnotherQuestion::class);
     assert($question, isInstanceOf(AnotherQuestion::class));
     assert($question->getAnswer(), isSameAs($answer));
 }
Example #9
0
 /**
  * @test
  */
 public function keyConsumerIsNotCalledWhenNoKeyInForeachRequested()
 {
     $i = 0;
     $peek = new Peek(new \ArrayIterator(['foo' => 303, 'bar' => 404, 'baz' => 505]), function () {
     }, function () {
         fail('Key consumer is not expected to be called');
     });
     foreach ($peek as $value) {
         $i++;
     }
     assert($i, equals(3));
 }
 /**
  * @test
  */
 public function infiniteGeneratorDoesStopOnlyWhenBreakOutOfLoop()
 {
     $i = 0;
     foreach (Generator::infinite(0, function ($value) {
         return $value + 2;
     }) as $key => $value) {
         if (1000 > $key) {
             $i++;
         } else {
             break;
         }
     }
     assert($i, equals(1000));
 }
 /**
  * @test
  * @dataProvider  throwables
  */
 public function createsLogDirectoryWithChangedModeIfNotExists($throwable)
 {
     $this->exceptionLogger->setFilemode(0777)->log($throwable);
     assert($this->root->getChild(self::$logPath)->getPermissions(), equals(0777));
 }
 /**
  * @test
  */
 public function explicitBindingOverwritesProvidedByAnnotation()
 {
     $binder = new Binder();
     $binder->bind(Person2::class)->to(Mikey::class);
     assert($binder->getInjector()->getInstance(Person2::class), isInstanceOf(Mikey::class));
 }
 /**
  * @since   2.0.0
  * @test
  */
 public function deleteWritesCorrectRequestWithVersion()
 {
     $this->createHttpRequest()->delete(5, HttpVersion::HTTP_1_0);
     assert($this->memory, equals(Http::lines('DELETE /foo/resource HTTP/1.0', 'Host: example.com', 'X-Binford: 6100', '')));
 }
 /**
  * @test
  */
 public function toMapPassesKeyAndValueWhenNoSelectorProvided()
 {
     assert(Sequence::of($this->people)->collect()->inMap(), equals($this->people));
 }
 /**
  * @test
  * @since  4.1.3
  */
 public function propertyBindingUsedWhenParamHasTypeHintButIsAnnotated()
 {
     try {
         $binder = new Binder();
         $properties = new Properties(['config' => ['example.password' => 'somePassword']]);
         $binder->bindProperties($properties, 'PROD');
         $example = $binder->getInjector()->getInstance(Example::class);
         assert($example->password, isInstanceOf(Secret::class));
     } finally {
         // ensure all references are removed to clean up environment
         unset($properties);
         $example->password = null;
         unset($example);
         unset($binder);
         gc_collect_cycles();
     }
 }
 /**
  * @test
  */
 public function namedConstructorInjectionWithMultipleParamAndNamedParamGroup()
 {
     $binder = new Binder();
     $binder->bind(Employee::class)->named('schst')->to(Boss::class);
     $binder->bind(Employee::class)->to(TeamMember::class);
     $injector = $binder->getInjector();
     $group = $injector->getInstance(DevelopersMultipleConstructorParamsGroupedName::class);
     assert($group, equals(new DevelopersMultipleConstructorParamsGroupedName(new Boss(), new Boss())));
 }
 /**
  * @test
  */
 public function exportsAnyThingElseWithDefault()
 {
     assert(Provides::values([1])->describeValue(exporter(), 'foo'), equals('\'foo\''));
 }
Example #18
0
 /**
  * @test
  */
 public function parseRecognizesHttpsUris()
 {
     assert(Parse::toType('https://example.net/'), equals(HttpUri::fromString('https://example.net/')));
 }
 /**
  * @test
  * @since  6.0.0
  */
 public function usesImplementationSpecifiedForMode()
 {
     $binder = new Binder();
     assert($binder->setEnvironment('DEV')->getInjector()->getInstance(Person3::class), isInstanceOf(Mikey::class));
 }
 /**
  * Retrieve the purge pattern from the last purge request.
  *
  * @return string
  *   The purge pattern, which is a regular expression.
  */
 private function getPurgePatternFromLastRequest()
 {
     $requests = $this->getRequests();
     assert($requests, isOfSize(1));
     $purge_request = $requests->last();
     $purge_patterns = $purge_request->getHeader('X-Invalidate-Regexp')->toArray();
     $purge_pattern = reset($purge_patterns);
     return $purge_pattern;
 }
Example #21
0
 /**
  * @test
  */
 public function writesToResource()
 {
     assert($this->stream->write('yoyoyoyo'), equals(8));
     assert($this->file->getContent(), equals('yoyoyoyo'));
 }
 /**
  * @param  mixed   $value
  * @param  string  $expectedType
  * @test
  * @dataProvider  valueTypes
  * @since  7.0.0
  */
 public function typeOfOtherValuesReturnsNativeType($value, string $expectedType)
 {
     assert(typeOf($value), equals($expectedType));
 }
 /**
  * @test
  * @since  8.0.0
  */
 public function writeLinesReturnsOnlyAmountOfUnfilteredBytedWritten()
 {
     assert($this->filteredOutputStream->writeLines(['foo', 'bar']), equals(4));
 }
 /**
  * @test
  */
 public function mixedAnnotations()
 {
     $plugin = NewInstance::of(Plugin::class);
     $binder = new Binder();
     $binder->bindList('listConfig');
     $binder->bindMap('mapConfig');
     $binder->bind(Plugin::class)->named('foo')->toInstance($plugin);
     $binder->bindConstant('foo')->to(42);
     $binder->bindList('aList')->withValue(313);
     $binder->bindMap('aMap')->withEntry('tb', 303);
     assert($this->createPluginHandler($binder)->getArgs(), equals(['std' => $plugin, 'answer' => 42, 'list' => [313], 'map' => ['tb' => 303]]));
 }
 /**
  * @test
  */
 public function castFromStringReturnsInstance()
 {
     assert(HttpVersion::castFrom('HTTP/1.1'), equals(new HttpVersion(1, 1)));
 }
 /**
  * @since  3.1.0
  * @test
  */
 public function functionShortcut()
 {
     assert(\stubbles\peer\http('http://example.net/'), isInstanceOf(HttpConnection::class));
 }
 /**
  * @test
  * @group  ioc_constantprovider
  * @since  1.6.0
  */
 public function constantViaInjectionProviderClassName()
 {
     $binder = new Binder();
     $binder->bindConstant('answer')->toProviderClass(AnswerConstantProvider::class);
     $injector = $binder->getInjector();
     assertTrue($injector->hasConstant('answer'));
     assert($injector->getConstant('answer'), equals(42));
     $this->assertConstantInjection($binder->getInjector());
 }
Example #28
0
 /**
  *
  * @param  mixed   $expected
  * @param  string  $method
  * @test
  * @dataProvider  methods
  * @since  5.0.0
  */
 public function parseNullWithDefaultReturnsDefault($expected, string $method)
 {
     $parse = new Parse(null);
     assert($parse->defaultingTo('foo')->{$method}(), equals('foo'));
 }
Example #29
0
 /**
  * @test
  * @since  5.1.0
  */
 public function propertyInstanceIsBound()
 {
     assert($this->createInjector()->getInstance(Properties::class, 'config.ini'), isSameAs($this->properties));
 }
 /**
  * @test
  */
 public function handleSignalsResultOfResponsibleErrorHandlerIfErrorReportingEnabled()
 {
     $oldLevel = error_reporting(E_ALL);
     try {
         $this->errorHandler1->mapCalls(['isResponsible' => false]);
         $this->errorHandler2->mapCalls(['isResponsible' => true, 'isSupressable' => false, 'handle' => ErrorHandler::STOP_ERROR_HANDLING]);
         assert($this->errorHandlers->handle(1, 'foo'), equals(ErrorHandler::STOP_ERROR_HANDLING));
     } finally {
         error_reporting($oldLevel);
     }
 }