public function testComposite() { $expect = "/root\n/root/usr\n/root/usr/B\n/root/A\n"; $rootDir = new Directory("root"); $usrDir = new Directory("usr"); $fileA = new File("A"); $rootDir->add($usrDir); $rootDir->add($fileA); $fileB = new File("B"); $usrDir->add($fileB); $result = $rootDir->show(""); $this->assertEquals($result, $expect); }
public function testAdd() { $d = new Directory('foo'); $d->content(); //force generation of files list, to be sure it's not cloned $d2 = $d->add($file = new File('foo', new StringStream('bar'))); $this->assertInstanceOf(DirectoryInterface::class, $d2); $this->assertNotSame($d, $d2); $this->assertSame($d->name(), $d2->name()); $this->assertNotSame($d->content(), $d2->content()); $this->assertSame('', (string) $d->content()); $this->assertSame('foo', (string) $d2->content()); $this->assertSame(0, $d->recordedEvents()->count()); $this->assertSame(1, $d2->recordedEvents()->count()); $this->assertInstanceOf(FileWasAdded::class, $d2->recordedEvents()->current()); $this->assertSame($file, $d2->recordedEvents()->current()->file()); }
<?php namespace MyDesignPattern\Practice01; //以下、メインルーチン //部署毎にファイルを保存しているイメージ $rootDir = new Directory('root'); $generalAccountingDir = new Directory('generalAccounting'); $humanResourcesDir = new Directory('humanResources'); $engineeringDir = new Directory('engineering'); $rootDir->add($generalAccountingDir); $rootDir->add($humanResourcesDir); $rootDir->add($engineeringDir); $gaDir1 = new Directory('gaDir1'); $generalAccountingDir->add($gaDir1); $gaFile1 = new File('gaFile1', 1024); $gaFile2 = new File('gaFile2', 512); $gaDir1->add($gaFile1); $gaDir1->add($gaFile2); $gaFile3 = new File('gaFile2', 1536); $generalAccountingDir->add($gaFile3); $hrDir1 = new Directory('hrDir1'); $humanResourcesDir->add($hrDir1); $enFile1 = new File('enFile1', 1024); $engineeringDir->add($enFile1); $enDir1 = new Directory('enDir1'); $engineeringDir->add($enDir1); $enFile2 = new File('enFile2', 1536); $enDir1->add($enFile2); $rootDir->printList(''); abstract class Entry
<?php namespace Visitor; require_once __DIR__ . '/../autoload.php'; printf("Making root entries...\n"); $rootDir = new Directory('root'); $binDir = new Directory('bin'); $tmpDir = new Directory('tmp'); $usrDir = new Directory('usr'); $rootDir->add($binDir)->add($tmpDir)->add($usrDir); $binDir->add(new File('vi', 10000)); $binDir->add(new File('latex', 20000)); $rootDir->accept(new ListVisitor()); printf("\n"); printf("Making user entries...\n"); $yuki = new Directory('yuki'); $hanako = new Directory('hanako'); $tomura = new Directory('tomura'); $usrDir->add($yuki)->add($hanako)->add($tomura); $yuki->add(new File('diary.html', 100))->add(new File('Composite.java', 200)); $hanako->add(new File('memo.tex', 300)); $tomura->add(new File('game.doc', 400))->add(new File('junk.mail', 500)); $rootDir->accept(new ListVisitor());
} public function add(Entry $entry) { if (!$entry instanceof Entry) { throw InvalidArgumentException('引数の型が不正です'); } $this->directory[] = $entry; $entry->setParent($this); } public function printList($prefix) { $output = $prefix . '/' . $this->name . ' ' . $this->getSize() . PHP_EOL; echo nl2br($output); if (empty($this->directory)) { return; } foreach ($this->directory as $entry) { $entry->printList($prefix . '/' . $this->name); } } } //以下、メインルーチン //部署毎にファイルを保存しているイメージ $rootDir = new Directory('root'); $generalAccountingDir = new Directory('generalAccounting'); $rootDir->add($generalAccountingDir); $gaDir1 = new Directory('gaDir1'); $generalAccountingDir->add($gaDir1); $gaFile1 = new File('gaFile1', 1024); $gaDir1->add($gaFile1); echo $gaFile1->getFullPath();