/**
  * {@inheritdoc}
  */
 public function rollback()
 {
     if ($this->bindingDescriptor->isLoaded() || !$this->containingPackage || !$this->typeDescriptor) {
         return;
     }
     // never fails with the check before, given that the type name of
     // the description/type didn't changed, which is impossible since
     // they're immutable
     $this->bindingDescriptor->load($this->containingPackage, $this->typeDescriptor);
     if ($this->wasRemoved) {
         // never fails
         $this->bindingDescriptors->add($this->bindingDescriptor);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function execute()
 {
     // sanity check
     if ($this->bindingDescriptor->isLoaded()) {
         return;
     }
     $typeName = $this->bindingDescriptor->getTypeName();
     $typeDescriptor = $this->typeDescriptors->contains($typeName) ? $this->typeDescriptors->getFirst($typeName) : null;
     $this->bindingDescriptor->load($this->containingPackage, $typeDescriptor);
     $uuid = $this->bindingDescriptor->getUuid();
     if ($this->bindingDescriptors->contains($uuid)) {
         $this->previousDescriptor = $this->bindingDescriptors->get($uuid);
     }
     $this->bindingDescriptors->add($this->bindingDescriptor);
 }
 public function testRemoveRootAssetMapping()
 {
     $uuid = $this->binding1->getUuid();
     $this->bindingType->load($this->rootPackage);
     $this->binding1->load($this->rootPackage, $this->bindingType);
     $this->discoveryManager->expects($this->at(0))->method('removeRootBindings')->with($this->uuid($uuid));
     $this->manager->removeRootAssetMapping($uuid);
 }
 public function testRemoveRootAssetMappingDispatchesEvent()
 {
     $uuid = $this->binding1->getUuid();
     $mapping = new AssetMapping('/path', 'target1', '/css', $uuid);
     $event = new RemoveAssetMappingEvent($mapping);
     $this->bindingType->load($this->rootPackage);
     $this->binding1->load($this->rootPackage, $this->bindingType);
     $this->dispatcher->expects($this->any())->method('hasListeners')->with(PuliEvents::POST_REMOVE_ASSET_MAPPING)->willReturn(true);
     $this->dispatcher->expects($this->once())->method('dispatch')->with(PuliEvents::POST_REMOVE_ASSET_MAPPING, $event);
     $this->discoveryManager->expects($this->once())->method('findRootBindings')->with($this->uuid($uuid))->willReturn(array($this->binding1));
     $this->discoveryManager->expects($this->once())->method('removeRootBindings')->with($this->uuid($uuid));
     $this->manager->removeRootAssetMapping($uuid);
 }
 /**
  * Unloads and loads a binding descriptor.
  *
  * The descriptor is remembered and reloaded again in {@link postRollback()}
  * if the intercepted operation needs to be rolled back.
  *
  * @param BindingDescriptor $bindingDescriptor The descriptor to reload.
  */
 protected function reloadBindingDescriptor(BindingDescriptor $bindingDescriptor)
 {
     if (!$bindingDescriptor->isLoaded()) {
         return;
     }
     // Keep backup of containing package before calling unload()
     $containingPackage = $bindingDescriptor->getContainingPackage();
     $typeName = $bindingDescriptor->getTypeName();
     $typeDescriptor = $this->typeDescriptors->getEnabled($typeName);
     // never fails with the check in the beginning
     $bindingDescriptor->unload();
     // never fails after unloading, given that the type name matches
     // (which we can guarantee here)
     $bindingDescriptor->load($containingPackage, $typeDescriptor);
     $this->reloadedDescriptors[] = $bindingDescriptor;
 }
 public function testWritePackageFileWritesDefaultParameterValuesOfBindings()
 {
     $packageFile = new PackageFile();
     $package = new Package($packageFile, '/path', new InstallInfo('vendor/package', '/path'));
     // We need to create a type and a binding in state ENABLED
     $bindingType = new BindingTypeDescriptor('my/type', null, array(new BindingParameterDescriptor('param', BindingParameterDescriptor::OPTIONAL, 'default')));
     $bindingType->load($package);
     $binding = new BindingDescriptor('/app/config*.yml', 'my/type', array(), 'glob', Uuid::fromString(self::BINDING_UUID1));
     $binding->load($package, $bindingType);
     // The default value is accessible
     $this->assertSame('default', $binding->getParameterValue('param'));
     // But not written by the writer
     $packageFile->addBindingDescriptor($binding);
     $this->writer->writePackageFile($packageFile, $this->tempFile);
     $this->assertFileExists($this->tempFile);
     $this->assertJsonFileEquals(__DIR__ . '/Fixtures/json/binding-no-default-params.json', $this->tempFile);
 }
    public function testSerializePackageFileWritesDefaultParameterValuesOfBindings()
    {
        $packageFile = new PackageFile();
        $package = new Package($packageFile, '/path', new InstallInfo('vendor/package', '/path'));
        // We need to create a type and a binding in state ENABLED
        $bindingType = new BindingTypeDescriptor('my/type', null, array(new BindingParameterDescriptor('param', BindingParameterDescriptor::OPTIONAL, 'default')));
        $bindingType->load($package);
        $binding = new BindingDescriptor('/app/config*.yml', 'my/type', array(), 'glob', Uuid::fromString(self::BINDING_UUID1));
        $binding->load($package, $bindingType);
        // The default value is accessible
        $this->assertSame('default', $binding->getParameterValue('param'));
        // But not written by the serializer
        $packageFile->addBindingDescriptor($binding);
        $json = <<<JSON
{
    "version": "1.0",
    "bindings": {
        "2438256b-c2f5-4a06-a18f-f79755e027dd": {
            "query": "/app/config*.yml",
            "type": "my/type"
        }
    }
}

JSON;
        $this->assertJsonEquals($json, $this->serializer->serializePackageFile($packageFile));
    }
 public function testMatch()
 {
     $type = new BindingTypeDescriptor('vendor/type', null, array(new BindingParameterDescriptor('param')));
     $type->load($this->package);
     $uuid = Uuid::fromString('abcdb814-9dad-11d1-80b4-00c04fd430c8');
     $this->package->getInstallInfo()->addEnabledBindingUuid($uuid);
     $binding = new BindingDescriptor('/path', 'vendor/type', array('param' => 'value'), 'glob', $uuid);
     $binding->load($this->package, $type);
     $this->assertFalse($binding->match(Expr::same('foobar', BindingDescriptor::CONTAINING_PACKAGE)));
     $this->assertTrue($binding->match(Expr::same($this->package->getName(), BindingDescriptor::CONTAINING_PACKAGE)));
     $this->assertFalse($binding->match(Expr::same(BindingState::DISABLED, BindingDescriptor::STATE)));
     $this->assertTrue($binding->match(Expr::same(BindingState::ENABLED, BindingDescriptor::STATE)));
     $this->assertFalse($binding->match(Expr::startsWith('abce', BindingDescriptor::UUID)));
     $this->assertTrue($binding->match(Expr::startsWith('abcd', BindingDescriptor::UUID)));
     $this->assertFalse($binding->match(Expr::same('/path/nested', BindingDescriptor::QUERY)));
     $this->assertTrue($binding->match(Expr::same('/path', BindingDescriptor::QUERY)));
     $this->assertFalse($binding->match(Expr::same('xpath', BindingDescriptor::LANGUAGE)));
     $this->assertTrue($binding->match(Expr::same('glob', BindingDescriptor::LANGUAGE)));
     $this->assertFalse($binding->match(Expr::same('vendor/other', BindingDescriptor::TYPE_NAME)));
     $this->assertTrue($binding->match(Expr::same('vendor/type', BindingDescriptor::TYPE_NAME)));
     $this->assertFalse($binding->match(Expr::key(BindingDescriptor::PARAMETER_VALUES, Expr::key('param', Expr::same('foobar')))));
     $this->assertTrue($binding->match(Expr::key(BindingDescriptor::PARAMETER_VALUES, Expr::key('param', Expr::same('value')))));
 }
 /**
  * @expectedException \RuntimeException
  * @expectedExceptionMessage Cannot disable bindings in the package "vendor/root".
  */
 public function testDisableBindingFailsIfRoot()
 {
     $args = self::$disableCommand->parseArgs(new StringArgs('ab12'));
     $descriptor = new BindingDescriptor('/path', 'my/type', array(), 'glob');
     $descriptor->load($this->packages->getRootPackage());
     $this->discoveryManager->expects($this->once())->method('findBindings')->with($this->uuid('ab12'))->willReturn(array($descriptor));
     $this->discoveryManager->expects($this->never())->method('disableBinding');
     $this->handler->handleDisable($args, $this->io);
 }